feat(fs): add option to restore all but internal filesystem methods

This commit is contained in:
calebboyd
2019-04-08 22:14:27 -05:00
parent 448f4270ea
commit ff612d610e
5 changed files with 20 additions and 16 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "nexe",
"version": "3.0.5",
"version": "3.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "nexe",
"description": "Create a single executable out of your Node.js application",
"license": "MIT",
"version": "3.0.5",
"version": "3.1.0",
"contributors": [
"Craig Condon <craig.j.condon@gmail.com> (http://crcn.io)",
"Jared Allard <jaredallard@outlook.com>",
+13 -13
View File
@@ -13,24 +13,13 @@ export interface NexeBinary {
}
let originalFsMethods: any = null
let nexeBinary: NexeBinary | null = null
function restoreFs(fs: any = require('fs')): NexeBinary | false {
if (!nexeBinary) {
return false
}
const source = nexeBinary
Object.assign(fs, originalFsMethods)
nexeBinary = originalFsMethods = null
return source
}
let lazyRestoreFs = () => {}
function shimFs(binary: NexeBinary, fs: any = require('fs')) {
if (originalFsMethods !== null) {
return
}
originalFsMethods = Object.assign({}, fs)
nexeBinary = binary
const { blobPath, resources: manifest } = binary,
{ resourceStart, stat } = binary.layout,
directories: { [key: string]: { [key: string]: boolean } } = {},
@@ -41,7 +30,7 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
path = require('path'),
baseDir = path.dirname(process.execPath)
let log = (text: string) => true
let log = (_: string) => true
if ((process.env.DEBUG || '').toLowerCase().includes('nexe:require')) {
log = (text: string) => process.stderr.write('[nexe] - ' + text + '\n')
}
@@ -330,7 +319,18 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
}
}
Object.assign(fs, nfs)
lazyRestoreFs = () => {
Object.keys(nfs).forEach(key => {
fs[key] = originalFsMethods[key]
})
lazyRestoreFs = () => {}
}
return true
}
function restoreFs() {
lazyRestoreFs()
}
export { shimFs, restoreFs }
+2
View File
@@ -23,6 +23,7 @@ export interface NexeOptions {
name: string
asset: string
cwd: string
fs: boolean | string[]
flags: string[]
configure: string[]
vcBuild: string[]
@@ -59,6 +60,7 @@ export interface NexeOptions {
const defaults = {
flags: [],
cwd: process.cwd(),
fs: true,
configure: [],
mangle: true,
make: [],
+3 -1
View File
@@ -6,7 +6,9 @@ export default function(compiler: NexeCompiler, next: () => Promise<void>) {
wrap(
`process.__nexe = ${JSON.stringify(compiler.binaryConfiguration)};\n` +
'{{replace:lib/fs/patch.js}}' +
'\nshimFs(process.__nexe)'
'\nshimFs(process.__nexe)' +
`\n${compiler.options.fs ? '' : 'restoreFs()'}`
//TODO support only restoring specific methods
)
)
compiler.shims.push(