fix: load implicit input if stdin is empty without a tty

This commit is contained in:
calebboyd
2019-04-04 23:54:06 -05:00
parent 255f3887c7
commit d2756a5f75
3 changed files with 49 additions and 29 deletions
+13 -5
View File
@@ -1,7 +1,7 @@
import { NexeCompiler, NexeError } from '../compiler'
import { resolve, relative } from 'path'
import { each } from '@calebboyd/semaphore'
import resolveFiles from 'resolve-dependencies'
import resolveFiles, { resolveFileNameSync } from 'resolve-dependencies'
import { dequote, STDIN_FLAG } from '../util'
import { Readable } from 'stream'
@@ -16,12 +16,13 @@ function getStdIn(stdin: Readable): Promise<string> {
out += current
}
})
.on('end', () => resolve(out))
.on('end', () => resolve(out.trim()))
})
}
export default async function bundle(compiler: NexeCompiler, next: any) {
const { bundle, cwd, input } = compiler.options
const { bundle, cwd, input: inputPath } = compiler.options
let input = inputPath
compiler.entrypoint = './' + relative(cwd, input)
compiler.startup = ';require("module").runMain();'
@@ -35,14 +36,21 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
code = await require(bundle).createBundle(compiler.options)
}
if (input === STDIN_FLAG) {
if (input === STDIN_FLAG && (code = code || dequote(await getStdIn(process.stdin)))) {
compiler.stdinUsed = true
compiler.entrypoint = './__nexe_stdin.js'
code = code || dequote(await getStdIn(process.stdin))
await compiler.addResource(resolve(cwd, compiler.entrypoint), code)
return next()
}
if (input === STDIN_FLAG) {
const maybeInput = resolveFileNameSync(cwd, '.')
if (!maybeInput) {
throw new NexeError('No valid input detected')
}
input = maybeInput.absPath
}
const { files, warnings } = await resolveFiles(
input,
...Object.keys(compiler.bundle.index).filter(x => x.endsWith('.js')),