Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): return exit code 1 on incorrect inputs #826

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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