Created
May 28, 2020 16:20
-
-
Save kylecordes/b0427bbe6a1f201b016c9bf06161749e to your computer and use it in GitHub Desktop.
Post process Scully output to insert top level script tags
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
// Post-process Scully output to insert top-level third party script tags. | |
const globby = require('globby'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const myArgs = process.argv.slice(2); | |
if (myArgs.length !== 2) { | |
console.error('Usage: <distDir> <fragmentFile>'); | |
console.error( | |
'Fragment will be inserted at the start of the <body> in each index.html' | |
); | |
process.exit(1); | |
} | |
const [distDir, fragmentFile] = myArgs; | |
const htmls = globby.sync(path.join(distDir, '**/index.html')); | |
const utf8 = { encoding: 'UTF-8' }; | |
const fragment = fs.readFileSync(fragmentFile, utf8); | |
htmls.forEach(fn => { | |
const contents = fs.readFileSync(fn, utf8); | |
const fixed = contents.replace(/<body.*>/, match => match + fragment); | |
fs.writeFileSync(fn, fixed, utf8); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why don't you use a render-plugin for this?
Something like this: