next-htaccess-generator
Advanced tools
Comparing version
38
index.js
@@ -13,8 +13,10 @@ #!/usr/bin/env node | ||
function parseSlug(inputString) { | ||
return inputString.replace(/\[(.*?)\]/g, '([^/]+)'); | ||
const fileNameWithoutExt = path.parse(inputString).name; | ||
const dirName = path.posix.dirname(inputString); | ||
const dirNameWithoutLeadingDot = dirName.replace(/^\.\//, ''); | ||
return `${dirNameWithoutLeadingDot}/${fileNameWithoutExt}`.replace(/\[(.*?)\]/g, '([^/]+)'); | ||
} | ||
// Define the folder path you want to scan | ||
const folderPath = config.buildPath; | ||
// console.log("folder", folderPath) | ||
// Function to recursively list HTML files | ||
@@ -43,4 +45,2 @@ function listHtmlFiles(dir, fileList) { | ||
let htaccess = `<IfModule mod_rewrite.c>\n RewriteEngine On\n | ||
@@ -52,23 +52,19 @@ RewriteBase / \n`; | ||
if (!config.include404 && path.includes("404")) { | ||
return | ||
} | ||
const parsedSlug = parseSlug(path) | ||
const parts = parsedSlug.split('/'); | ||
const folder = parts.slice(0, -1).join('/'); | ||
const rule = `RewriteRule ^${folder}/$ ${path} [L]\n`; | ||
htaccess += ` #${folder}\n`; | ||
htaccess += ` ${rule}`; | ||
}); | ||
return | ||
} | ||
const parsedSlug = parseSlug(path) | ||
htaccess += ` #${path}\n`; | ||
htaccess += ` RewriteRule ^${(parsedSlug.replace("./", '')).replace("/index", "")}/$ ${path} [L]\n`; | ||
}); | ||
htaccess += ` RewriteCond %{REQUEST_FILENAME} !-f \n | ||
RewriteCond %{REQUEST_FILENAME} !-d \n | ||
RewriteRule ^(.*)$ /404/404.html [L] \n</IfModule>`; | ||
htaccess += ` RewriteCond %{REQUEST_FILENAME} !-f \n | ||
RewriteCond %{REQUEST_FILENAME} !-d \n | ||
RewriteRule ^(.*)$ /404/404.html [L] \n</IfModule>`; | ||
// Write the rules to .htaccess file | ||
// Write the rules to .htaccess file | ||
fs.writeFileSync(path.join(config.outputPath,'.htaccess'), htaccess); | ||
fs.writeFileSync(path.join(config.outputPath,'.htaccess'), htaccess); | ||
console.log('Successfully generated .htaccess file'); | ||
console.log('Successfully generated .htaccess file'); | ||
} | ||
generateHtaccess() |
{ | ||
"name": "next-htaccess-generator", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "This is a simple package to generate htaccess file from next build.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
10391
1.3%75
-1.32%