Skip to content

Commit

Permalink
fix(cli): return exit code 1 on incorrect inputs (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 27, 2024
1 parent 851e50f commit 80f39ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ function printUsage() {
--version, -v print current zx version
--help, -h print help
--repl start repl
--experimental enables experimental features (deprecated)
${chalk.italic('Full documentation:')} ${chalk.underline('https://google.github.io/zx/')}
`)
}

const argv = minimist(process.argv.slice(2), {
string: ['shell', 'prefix', 'postfix', 'eval', 'cwd'],
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl'],
boolean: [
'version',
'help',
'quiet',
'verbose',
'install',
'repl',
'experimental',
],
alias: { e: 'eval', i: 'install', v: 'version', h: 'help' },
stopEarly: true,
})
Expand Down Expand Up @@ -87,7 +98,10 @@ const argv = minimist(process.argv.slice(2), {
updateArgv(argv._.slice(firstArg === undefined ? 0 : 1))
if (!firstArg || firstArg === '-') {
const success = await scriptFromStdin()
if (!success) printUsage()
if (!success) {
printUsage()
process.exitCode = 1
}
return
}
if (/^https?:/.test(firstArg)) {
Expand Down
9 changes: 7 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ describe('cli', () => {
test('zx prints usage if no param passed', async () => {
let p = $`node build/cli.js`
p.stdin.end()
let out = await p
assert.match(out.stdout, /A tool for writing better scripts/)
try {
await p
assert.fail('must throw')
} catch (out) {
assert.match(out.stdout, /A tool for writing better scripts/)
assert.equal(out.exitCode, 1)
}
})

test('starts repl with --repl', async () => {
Expand Down

0 comments on commit 80f39ef

Please sign in to comment.