Created
March 1, 2021 05:22
-
-
Save doeringp/4bdffc94ce727abd469d33ea3956e2f4 to your computer and use it in GitHub Desktop.
Replaces the value of the <base href="..."> in a HTML document.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const args = process.argv.slice(2); | |
if (args.length < 2) | |
{ | |
console.log(`Replaces the value of the <base href="..."> in a HTML document. | |
USAGE: | |
node replaceBaseHref.js <html-file> <new-value> [encoding] | |
html-file - Path to the html file. | |
new-value - The new value for the <base href="..."> tag. | |
encoding - Optional. Default is utf8. | |
EXAMPLE: | |
node replaceBaseHref.js "index.html" "/myapp/" "utf8" | |
`); | |
process.exit(1); | |
} | |
const fileToReplace = args[0]; | |
const newValue = args[1]; | |
const encoding = args[2] || 'utf8'; | |
function exitWithError(error) { | |
console.trace(error); | |
process.exit(1); | |
} | |
fs.readFile(fileToReplace, encoding, function (error, data) { | |
if (error) | |
exitWithError(error); | |
var newContent = data.replace( | |
/<base\s+href\s*=\s*\"([^\"]*)"\s*>/g, | |
`<base href="${newValue}">`); | |
fs.writeFile(fileToReplace, newContent, encoding, function (error) { | |
if (error) exitWithError(error); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment