Commit 0584dcb

Anton Medvedev <anton@medv.io>
2022-06-22 21:31:14
Fix exit code override
1 parent cc9be7f
Changed files (3)
src/cli.ts
@@ -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 === '-') {
@@ -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)
test/fixtures/exit-code.mjs
@@ -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
test/cli.test.js
@@ -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()