From 0dca0745eebd447008c6ffebdc748750dc1208dd Mon Sep 17 00:00:00 2001 From: calebboyd Date: Mon, 8 Apr 2019 22:14:27 -0500 Subject: [PATCH] feat(fs): add option to restore all but internal filesystem methods --- package-lock.json | 2 +- package.json | 2 +- src/fs/patch.ts | 26 +++++++++++++------------- src/options.ts | 2 ++ src/steps/shim.ts | 4 +++- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index d89a1b0..1b818dd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "nexe", - "version": "3.0.5", + "version": "3.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d9d0e3e..0d13477 100644 --- a/package.json +++ b/package.json @@ -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 (http://crcn.io)", "Jared Allard ", diff --git a/src/fs/patch.ts b/src/fs/patch.ts index 08a72b1..6c4d29b 100644 --- a/src/fs/patch.ts +++ b/src/fs/patch.ts @@ -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 } diff --git a/src/options.ts b/src/options.ts index a29abe5..2994385 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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: [], diff --git a/src/steps/shim.ts b/src/steps/shim.ts index fd225b8..e03ceb1 100644 --- a/src/steps/shim.ts +++ b/src/steps/shim.ts @@ -6,7 +6,9 @@ export default function(compiler: NexeCompiler, next: () => Promise) { 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(