From 2a2526d3bca85d6070fdab725bf2777305fc867c Mon Sep 17 00:00:00 2001 From: calebboyd Date: Fri, 29 Sep 2017 17:49:17 -0500 Subject: [PATCH] fix: disable stdin for programatic usage --- package.json | 2 +- src/options.ts | 3 ++- src/steps/cli.ts | 27 +++++++++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 7a160a8..1c202a4 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": "2.0.0-rc.9", + "version": "2.0.0-rc.10", "contributors": [ "Craig Condon (http://crcn.io)", "Jared Allard ", diff --git a/src/options.ts b/src/options.ts index 2365551..700d269 100644 --- a/src/options.ts +++ b/src/options.ts @@ -34,6 +34,7 @@ export interface NexeOptions { native: any empty: boolean sourceUrl?: string + enableStdIn?: boolean python?: string loglevel: 'info' | 'silent' | 'verbose' silent?: boolean @@ -82,7 +83,7 @@ const alias = { l: 'loglevel', 'fake-argv': 'fakeArgv' } -const argv = parseArgv(process.argv, { alias, default: defaults }) +const argv = parseArgv(process.argv, { alias, default: { ...defaults, enableStdIn: true } }) const g = c.gray let help = ` ${c.bold('nexe [options]')} diff --git a/src/steps/cli.ts b/src/steps/cli.ts index 503c06e..471038d 100644 --- a/src/steps/cli.ts +++ b/src/steps/cli.ts @@ -5,15 +5,18 @@ import { readFileAsync, dequote, isWindows } from '../util' import { NexeCompiler } from '../compiler' import { NexeTarget } from '../target' -function readStreamAsync(stream: NodeJS.ReadableStream): PromiseLike { +function getStdIn(stdin: NodeJS.ReadStream): Promise { return new Promise(resolve => { - let input = '' - stream.setEncoding('utf-8') - stream.on('data', (x: string) => { - input += x - }) - stream.once('end', () => resolve(dequote(input))) - stream.resume && stream.resume() + let out = '' + stdin + .setEncoding('utf8') + .on('readable', () => { + let current + while ((current = stdin.read())) { + out += current + } + }) + .on('end', () => resolve(out)) }) } @@ -34,9 +37,9 @@ function readStreamAsync(stream: NodeJS.ReadableStream): PromiseLike { export default async function cli(compiler: NexeCompiler, next: () => Promise) { const { log } = compiler let stdInUsed = false - if (!process.stdin.isTTY) { + if (!process.stdin.isTTY && compiler.options.enableStdIn) { stdInUsed = true - compiler.input = await readStreamAsync(process.stdin) + compiler.input = await getStdIn(process.stdin) } await next() @@ -53,10 +56,10 @@ export default async function cli(compiler: NexeCompiler, next: () => Promise