diff --git a/index.js b/index.js index db704a7..9ffbc1d 100755 --- a/index.js +++ b/index.js @@ -1,10 +1,11 @@ #!/usr/bin/env node const nexe = require('./lib/nexe') +const eol = require('os').EOL module.exports = nexe if (require.main === module) { nexe.compile(nexe.argv).catch((e) => { - process.stderr.write(e.stack, () => process.exit(1)) + process.stderr.write(eol + e.stack, () => process.exit(1)) }) } diff --git a/src/compiler.ts b/src/compiler.ts index dd797ad..35ac9c5 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -32,12 +32,13 @@ export class NexeCompiler { private env = { ...process.env } private compileStep: { modify: Function; log: Function } public log = new Logger(this.options.loglevel) - public src = join(this.options.temp, this.options.version) + public src: string public files: NexeFile[] = [] public input: string public bundledInput?: string public output: string | null public targets: NexeTarget[] + public target: NexeTarget public resources: { bundle: string; index: { [key: string]: number[] } } = { index: {}, bundle: '' @@ -46,14 +47,16 @@ export class NexeCompiler { public writeFileAsync: (file: string, contents: Buffer | string) => Promise public replaceInFileAsync: (file: string, replacer: any, replaceValue: string) => Promise public setFileContentsAsync: (file: string, contents: string | Buffer) => Promise - - private nodeSrcBinPath = isWindows - ? join(this.src, 'Release', 'node.exe') - : join(this.src, 'out', 'Release', 'node') + private nodeSrcBinPath: string constructor(public options: NexeOptions) { const { python } = (this.options = options) this.targets = options.targets as NexeTarget[] + this.target = this.targets[0] + this.src = join(this.options.temp, this.target.version) + this.nodeSrcBinPath = isWindows + ? join(this.src, 'Release', 'node.exe') + : join(this.src, 'out', 'Release', 'node') this.log.step('nexe ' + nexeVersion, 'info') if (python) { if (isWindows) { diff --git a/src/nexe.ts b/src/nexe.ts index 4c43466..e28ecaa 100644 --- a/src/nexe.ts +++ b/src/nexe.ts @@ -11,6 +11,7 @@ import { import cli from './steps/cli' import bundle from './steps/bundle' import download from './steps/download' +import shim from './steps/shim' import artifacts from './steps/artifacts' import patches from './patches' import { rimrafAsync } from './util' @@ -39,7 +40,7 @@ async function compile( const buildSteps = build ? [download, artifacts, ...patches, ...(options.patches as NexePatch[])] : [] - const nexe = compose(resource, bundle, cli, buildSteps) + const nexe = compose(resource, bundle, cli, buildSteps, shim) return callback ? void nexe(compiler).then( () => callback && callback(null), diff --git a/src/options.ts b/src/options.ts index 5beeb45..5888a2a 100644 --- a/src/options.ts +++ b/src/options.ts @@ -4,7 +4,7 @@ import { isWindows, padRight } from './util' import { basename, extname, join, isAbsolute, relative } from 'path' import { getTarget, NexeTarget } from './target' import { EOL } from 'os' -import * as chalk from 'chalk' +import * as c from 'chalk' export const nexeVersion = '2.0.0-rc.4' @@ -19,7 +19,6 @@ export interface NexeOptions { targets: (string | NexeTarget)[] name: string cwd: string - version: string flags: string[] configure: string[] vcBuild: string[] @@ -36,6 +35,7 @@ export interface NexeOptions { python?: string loglevel: 'info' | 'silent' | 'verbose' silent?: boolean + fakeArgv?: boolean verbose?: boolean info?: boolean ico?: string @@ -50,7 +50,6 @@ export interface NexeOptions { } const defaults = { - version: process.version.slice(1), flags: [], cwd: process.cwd(), configure: [], @@ -68,7 +67,6 @@ const alias = { o: 'output', t: 'target', n: 'name', - v: 'version', r: 'resource', a: 'resource', p: 'python', @@ -77,41 +75,50 @@ const alias = { m: 'make', s: 'snapshot', h: 'help', - l: 'loglevel' + l: 'loglevel', + 'fake-argv': 'fakeArgv' } const argv = parseArgv(process.argv, { alias, default: defaults }) -const g = chalk.gray +const g = c.gray const help = ` -nexe --help CLI OPTIONS +${c.bold('nexe [options]')} - -i --input ${g('=index.js')} -- application entry point - -o --output ${g('=my-app.exe')} -- path to output file - -t --target ${g('=mac-x64-8.4.0')} -- *target a prebuilt binary - -n --name ${g('=my-app')} -- main app module name - -v --version ${g(`=${padRight(process.version.slice(1), 23)}`)}-- node version - -p --python ${g('=/path/to/python2')} -- python executable - -f --flag ${g('="--expose-gc"')} -- *v8 flags to include during compilation - -c --configure ${g('="--with-dtrace"')} -- *pass arguments to the configure step - -m --make ${g('="--loglevel"')} -- *pass arguments to the make/build step - -s --snapshot ${g('=/path/to/snapshot')} -- build with warmup snapshot - -r --resource ${g('=./paths/**/*')} -- *embed file bytes within the binary - -b --build -- build from source - --bundle ${g('=./path/to/config')} -- pass a module path that exports nexeBundle - --temp ${g('=./path/to/temp')} -- default './nexe' - --no-bundle -- set when input is already bundled - --cwd -- set the current working directory for the command - --ico -- file name for alternate icon file (windows) - --rc-* -- populate rc file options (windows) - --clean -- force download of sources - --enableNodeCli -- enable node cli enforcement (blocks app cli) - --sourceUrl -- pass an alternate source (node.tar.gz) url - --silent -- disable logging - --verbose -- set logging to verbose + ${c.underline.bold('Options:')} - -* variable key name * option can be used more than once`.trim() + EOL + -i --input ${g('=index.js')} -- application entry point + -o --output ${g('=my-app.exe')} -- path to output file + -t --target ${g('=8.4.0-x64')} -- node version description + -n --name ${g('=my-app')} -- main app module name -function flattenFilter(...args: any[]): string[] { + -r --resource -- *embed files (glob) within the binary + -s --snapshot -- path to a warmup snapshot + + ${c.underline.bold('Building from source:')} + + -b --build -- build from source + -p --python -- python2 (as python) executable path + -f --flag -- *v8 flags to include during compilation + -c --configure -- *arguments to the configure step + -m --make -- *arguments to the make/build step + --ico -- file name for alternate icon file (windows) + --rc-* -- populate rc file options (windows) + --sourceUrl -- pass an alternate source (node.tar.gz) url + --enableNodeCli -- enable node cli enforcement (blocks app cli) + + ${c.underline.bold('Other options:')} + + --bundle -- custom bundling module with 'createBundle' export + --temp -- temp file storage default './nexe' + --cwd -- set the current working directory for the command + --fake-argv TODO -- fake argv[1] with entry file + --clean -- force download of sources + --silent -- disable logging + --verbose -- set logging to verbose + + -* variable key name * option can be used more than once`.trim() + EOL + +function flatten(...args: any[]): string[] { return ([] as string[]).concat(...args).filter(x => x) } @@ -195,15 +202,15 @@ function normalizeOptionsAsync(input?: Partial): Promise): Promise { if (typeof x === 'string') { return require(x).default diff --git a/src/patches/build-fixes.ts b/src/patches/build-fixes.ts index 6369ec3..217dfc1 100644 --- a/src/patches/build-fixes.ts +++ b/src/patches/build-fixes.ts @@ -1,7 +1,7 @@ import { NexeCompiler } from '../compiler' export default async function buildFixes(compiler: NexeCompiler, next: () => Promise) { - if (!compiler.options.version.startsWith('8.2')) { + if (!compiler.target.version.startsWith('8.2')) { return next() } diff --git a/src/steps/shim.ts b/src/steps/shim.ts new file mode 100644 index 0000000..ca1f0a8 --- /dev/null +++ b/src/steps/shim.ts @@ -0,0 +1,18 @@ +import { NexeCompiler } from '../compiler' + +export default function(compiler: NexeCompiler, next: () => Promise) { + if (!compiler.options.fakeArgv) { + return next() + } + + const nty = !process.stdin.isTTY + const input = nty ? '[stdin]' : compiler.options.input + + compiler.input = + `!(() => { + var r = require('path').resolve; + process.argv.splice(1,0, ${nty ? `'${input}'` : `r("${input}")`}); + })();` + compiler.input + + return next() +} diff --git a/test.js b/test.js index cea4f10..1e1a484 100644 --- a/test.js +++ b/test.js @@ -1 +1 @@ -console.log('hello world') +console.log(process.version, process.argv)