Skip to content

Commit

Permalink
Fix exit code override
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jun 22, 2022
1 parent cc9be7f commit 0584dcb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ await (async function main() {
if (process.argv.length == 3) {
if (['--version', '-v', '-V'].includes(process.argv[2])) {
console.log(getVersion())
return (process.exitCode = 0)
return
}
if (['--help', '-h'].includes(process.argv[2])) {
printUsage()
return (process.exitCode = 0)
return
}
if (['--interactive', '-i'].includes(process.argv[2])) {
startRepl()
return (process.exitCode = 0)
return
}
}
if (argv.eval || argv.e) {
const script = (argv.eval || argv.e).toString()
await runScript(script)
return (process.exitCode = 0)
return
}
let firstArg = process.argv.slice(2).find((a) => !a.startsWith('--'))
if (typeof firstArg === 'undefined' || firstArg === '-') {
Expand All @@ -78,7 +78,7 @@ await (async function main() {
updateArgv({ sliceAt: 3 })
await importPath(filepath)
}
return (process.exitCode = 0)
return
})().catch((err) => {
if (err instanceof ProcessOutput) {
console.error('Error:', err.message)
Expand Down
5 changes: 5 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,9 @@ test('argv works with zx and node', async () => {
)
})

test('exit code can be set', async () => {
let p = await $`node build/cli.js test/fixtures/exit-code.mjs`.nothrow()
assert.is(p.exitCode, 42)
})

test.run()
15 changes: 15 additions & 0 deletions test/fixtures/exit-code.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

process.exitCode = 42

0 comments on commit 0584dcb

Please sign in to comment.