Rigidify code replacement by using Lodash templates.

This commit is contained in:
Bryce Gibson
2021-09-18 11:20:05 +10:00
parent bbaa63767d
commit fc09a7d903
6 changed files with 1287 additions and 1549 deletions
+1271 -1528
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -41,12 +41,14 @@
},
"dependencies": {
"@calebboyd/semaphore": "^1.3.1",
"@types/lodash": "^4.14.173",
"app-builder": "^7.0.4",
"caw": "^2.0.1",
"chalk": "^2.4.2",
"download": "^8.0.0",
"globby": "^11.0.2",
"got": "^11.8.2",
"lodash": "^4.17.21",
"meriyah": "^4.3.3",
"minimist": "^1.2.6",
"mkdirp": "^1.0.4",
+1 -1
View File
@@ -9,7 +9,7 @@ import { resolveSync } from 'resolve-dependencies'
const caw = require('caw')
const c = process.platform === 'win32' ? chalk.constructor({ enabled: false }) : chalk
export const version = '{{replace:0}}'
export const version = '{{ version }}'
export interface NexePatch {
(compiler: NexeCompiler, next: () => Promise<void>): Promise<void>
+4 -4
View File
@@ -58,7 +58,7 @@ export default async function main(compiler: NexeCompiler, next: () => Promise<v
fileLines.splice(
location.start.line,
0,
'{{replace:lib/fs/bootstrap.js}}' +
'{{ file("lib/fs/bootstrap.js") }}' +
'\n' +
(semverGt(version, '11.99') ? 'expandArgv1 = false;\n' : '')
)
@@ -69,13 +69,13 @@ export default async function main(compiler: NexeCompiler, next: () => Promise<v
await compiler.replaceInFileAsync(
bootFile,
'initializeFrozenIntrinsics();',
'initializeFrozenIntrinsics();\n' + wrap('{{replace:lib/patches/boot-nexe.js}}')
'initializeFrozenIntrinsics();\n' + wrap('{{ file("lib/patches/boot-nexe.js") }}')
)
} else {
await compiler.replaceInFileAsync(
bootFile,
'initializePolicy();',
'initializePolicy();\n' + wrap('{{replace:lib/patches/boot-nexe.js}}')
'initializePolicy();\n' + wrap('{{ file("lib/patches/boot-nexe.js") }}')
)
}
await compiler.replaceInFileAsync(
@@ -102,7 +102,7 @@ export default async function main(compiler: NexeCompiler, next: () => Promise<v
} else {
await compiler.setFileContentsAsync(
'lib/_third_party_main.js',
'{{replace:lib/patches/boot-nexe.js}}'
'{{ file("lib/patches/boot-nexe.js") }}'
)
}
return next()
+1 -1
View File
@@ -6,7 +6,7 @@ export default async function (compiler: NexeCompiler, next: () => Promise<void>
compiler.shims.push(
wrap(
'' +
'{{replace:lib/fs/patch.js}}' +
'{{ file("lib/fs/patch.js") }}' +
'\nshimFs(process.__nexe)' +
`\n${compiler.options.fs ? '' : 'restoreFs()'}`
//TODO support only restoring specific methods
+8 -15
View File
@@ -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}`)