Compare commits

...

2 Commits

Author SHA1 Message Date
calebboyd ff612d610e feat(fs): add option to restore all but internal filesystem methods 2019-04-08 22:14:27 -05:00
calebboyd 448f4270ea fix: set entrypoint correctly when outside a tty 2019-04-08 16:23:21 -05:00
7 changed files with 31 additions and 23 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "nexe", "name": "nexe",
"version": "3.0.4", "version": "3.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "nexe", "name": "nexe",
"description": "Create a single executable out of your Node.js application", "description": "Create a single executable out of your Node.js application",
"license": "MIT", "license": "MIT",
"version": "3.0.4", "version": "3.1.0",
"contributors": [ "contributors": [
"Craig Condon <craig.j.condon@gmail.com> (http://crcn.io)", "Craig Condon <craig.j.condon@gmail.com> (http://crcn.io)",
"Jared Allard <jaredallard@outlook.com>", "Jared Allard <jaredallard@outlook.com>",
+13 -13
View File
@@ -13,24 +13,13 @@ export interface NexeBinary {
} }
let originalFsMethods: any = null let originalFsMethods: any = null
let nexeBinary: NexeBinary | null = null let lazyRestoreFs = () => {}
function restoreFs(fs: any = require('fs')): NexeBinary | false {
if (!nexeBinary) {
return false
}
const source = nexeBinary
Object.assign(fs, originalFsMethods)
nexeBinary = originalFsMethods = null
return source
}
function shimFs(binary: NexeBinary, fs: any = require('fs')) { function shimFs(binary: NexeBinary, fs: any = require('fs')) {
if (originalFsMethods !== null) { if (originalFsMethods !== null) {
return return
} }
originalFsMethods = Object.assign({}, fs) originalFsMethods = Object.assign({}, fs)
nexeBinary = binary
const { blobPath, resources: manifest } = binary, const { blobPath, resources: manifest } = binary,
{ resourceStart, stat } = binary.layout, { resourceStart, stat } = binary.layout,
directories: { [key: string]: { [key: string]: boolean } } = {}, directories: { [key: string]: { [key: string]: boolean } } = {},
@@ -41,7 +30,7 @@ function shimFs(binary: NexeBinary, fs: any = require('fs')) {
path = require('path'), path = require('path'),
baseDir = path.dirname(process.execPath) baseDir = path.dirname(process.execPath)
let log = (text: string) => true let log = (_: string) => true
if ((process.env.DEBUG || '').toLowerCase().includes('nexe:require')) { if ((process.env.DEBUG || '').toLowerCase().includes('nexe:require')) {
log = (text: string) => process.stderr.write('[nexe] - ' + text + '\n') 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) Object.assign(fs, nfs)
lazyRestoreFs = () => {
Object.keys(nfs).forEach(key => {
fs[key] = originalFsMethods[key]
})
lazyRestoreFs = () => {}
}
return true return true
} }
function restoreFs() {
lazyRestoreFs()
}
export { shimFs, restoreFs } export { shimFs, restoreFs }
+2
View File
@@ -23,6 +23,7 @@ export interface NexeOptions {
name: string name: string
asset: string asset: string
cwd: string cwd: string
fs: boolean | string[]
flags: string[] flags: string[]
configure: string[] configure: string[]
vcBuild: string[] vcBuild: string[]
@@ -59,6 +60,7 @@ export interface NexeOptions {
const defaults = { const defaults = {
flags: [], flags: [],
cwd: process.cwd(), cwd: process.cwd(),
fs: true,
configure: [], configure: [],
mangle: true, mangle: true,
make: [], make: [],
+2 -1
View File
@@ -45,10 +45,11 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
if (input === STDIN_FLAG) { if (input === STDIN_FLAG) {
const maybeInput = resolveFileNameSync(cwd, '.') const maybeInput = resolveFileNameSync(cwd, '.')
if (!maybeInput) { if (!maybeInput || !maybeInput.absPath) {
throw new NexeError('No valid input detected') throw new NexeError('No valid input detected')
} }
input = maybeInput.absPath input = maybeInput.absPath
compiler.entrypoint = './' + relative(cwd, input)
} }
const { files, warnings } = await resolveFiles( const { files, warnings } = await resolveFiles(
+9 -6
View File
@@ -1,4 +1,4 @@
import { dirname, normalize, relative } from 'path' import { dirname, normalize, relative, resolve } from 'path'
import { createWriteStream, chmodSync, statSync } from 'fs' import { createWriteStream, chmodSync, statSync } from 'fs'
import { NexeCompiler } from '../compiler' import { NexeCompiler } from '../compiler'
import { NexeTarget } from '../target' import { NexeTarget } from '../target'
@@ -24,18 +24,21 @@ export default async function cli(compiler: NexeCompiler, next: () => Promise<vo
mkdirp.sync(dirname(output)) mkdirp.sync(dirname(output))
return new Promise((resolve, reject) => { return new Promise((res, rej) => {
const step = log.step('Writing result to file') const step = log.step('Writing result to file')
deliverable deliverable
.pipe(createWriteStream(output)) .pipe(createWriteStream(output))
.on('error', reject) .on('error', rej)
.once('close', (e: Error) => { .once('close', (e: Error) => {
if (e) { if (e) {
reject(e) rej(e)
} else if (compiler.output) { } else if (compiler.output) {
const output = compiler.output, const output = compiler.output,
mode = statSync(output).mode | 0o111, mode = statSync(output).mode | 0o111,
inputFileLogOutput = relative(process.cwd(), compiler.options.input), inputFileLogOutput = relative(
process.cwd(),
resolve(compiler.options.cwd, compiler.entrypoint || compiler.options.input)
),
outputFileLogOutput = relative(process.cwd(), output) outputFileLogOutput = relative(process.cwd(), output)
chmodSync(output, mode.toString(8).slice(-3)) chmodSync(output, mode.toString(8).slice(-3))
@@ -48,7 +51,7 @@ export default async function cli(compiler: NexeCompiler, next: () => Promise<vo
: inputFileLogOutput : inputFileLogOutput
}' written to: ${outputFileLogOutput}` }' written to: ${outputFileLogOutput}`
) )
resolve(compiler.quit()) res(compiler.quit())
} }
}) })
}) })
+3 -1
View File
@@ -6,7 +6,9 @@ export default function(compiler: NexeCompiler, next: () => Promise<void>) {
wrap( wrap(
`process.__nexe = ${JSON.stringify(compiler.binaryConfiguration)};\n` + `process.__nexe = ${JSON.stringify(compiler.binaryConfiguration)};\n` +
'{{replace:lib/fs/patch.js}}' + '{{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( compiler.shims.push(