Rigidify code replacement by using Lodash templates.
This commit is contained in:
+8
-15
@@ -1,11 +1,10 @@
|
||||
import { writeFileSync, readFileSync } from 'fs'
|
||||
import { template } from 'lodash'
|
||||
|
||||
/**
|
||||
* post build step to insert code files into code files (naively).
|
||||
* '{{replace:path/to/file}}' => "file contents"
|
||||
* post build step to insert code files into code files.
|
||||
* And the package.json version.
|
||||
*/
|
||||
//inject('plugins/nexe-daemon/lib/index.js')
|
||||
cp('src/fs/package.json', 'lib/fs/package.json')
|
||||
cp('src/fs/bootstrap.js', 'lib/fs/bootstrap.js')
|
||||
cp('src/fs/README.md', 'lib/fs/README.md')
|
||||
@@ -14,18 +13,12 @@ inject('lib/patches/third-party-main.js')
|
||||
inject('lib/steps/shim.js')
|
||||
inject('lib/options.js', JSON.stringify(require('../package.json').version))
|
||||
|
||||
function inject(filename: string, ...replacements: string[]) {
|
||||
let contents = readFileSync(filename, 'utf8')
|
||||
contents = contents.replace(/('{{(.*)}}')/g, (substring: string, ...matches: string[]) => {
|
||||
if (!matches || !matches[1]) {
|
||||
return substring
|
||||
}
|
||||
const [replace, file] = matches[1].split(':')
|
||||
if (replace !== 'replace') {
|
||||
return substring
|
||||
}
|
||||
console.log('Replacing: ', substring)
|
||||
return replacements[+file] ? replacements[+file] : JSON.stringify(readFileSync(file, 'utf8'))
|
||||
function inject(filename: string, version?: string) {
|
||||
const contents = template(readFileSync(filename, 'utf8'), {
|
||||
interpolate: /'\{\{([\s\S]*?)\}\}'/,
|
||||
})({
|
||||
file: (path: string) => JSON.stringify(readFileSync(path, 'utf8')),
|
||||
version,
|
||||
})
|
||||
writeFileSync(filename, contents)
|
||||
console.log(`Wrote: ${filename}`)
|
||||
|
||||
Reference in New Issue
Block a user