Skip to content

Commit 4d1a448

Browse files
KJ7LNWEric Wheeler
andauthored
lib: fix: propagate exit codes from spawned processes (#3)
The run-shared-scripts tool spawned child processes but never waited for them to complete or captured their exit codes, causing all scripts to appear successful even when they failed. This made it impossible for CI systems and build pipelines to detect failures. The run() function now returns a Promise that resolves with the child process exit code, and the bin script awaits this result and sets process.exitCode accordingly. - Modified lib/index.js: wrap spawn in Promise, resolve with exit code - Modified bin/rss.js: await run() and set process.exitCode - Return 0 for successful execution and dry-run mode - Return 1 when script name not found in rss configuration - Added example test scripts to verify exit code propagation Signed-off-by: Eric Wheeler <[email protected]> Co-authored-by: Eric Wheeler <[email protected]>
1 parent 1a2c49c commit 4d1a448

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

bin/rss.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ const cli = meow(
4141

4242
const cmd = cli.input[0] || process.env.npm_lifecycle_event
4343

44-
run(cmd, args, cli.flags)
44+
const exitCode = await run(cmd, args, cli.flags)
45+
process.exitCode = exitCode

example/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"test": {
2020
"file": "./scripts/test.js"
2121
},
22+
"test-success": {
23+
"file": "./scripts/test-success.js"
24+
},
25+
"test-failure": {
26+
"file": "./scripts/test-failure.js"
27+
},
2228
"pretest": "run-s -s clean:coverage",
2329
"start:1": "server --port {1}",
2430
"start:2": "server -a {1} --port {2}",

example/scripts/test-failure.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
console.log('test-failure: exiting with code 42')
4+
process.exit(42)

example/scripts/test-success.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
console.log('test-success: exiting with code 0')
4+
process.exit(0)

lib/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,20 @@ export async function run(task, args, options) {
131131
? fixFilePath(root, main)
132132
: await makeExecFile(root, task, main)
133133

134-
const child = spawn(exe, isFile ? args : [], { stdio: 'inherit' })
135-
136-
if (!isFile) {
137-
child.on('exit', () => { fse.rm(exe) })
138-
}
134+
return new Promise((resolve) => {
135+
const child = spawn(exe, isFile ? args : [], { stdio: 'inherit' })
136+
137+
child.on('exit', (code) => {
138+
if (!isFile) {
139+
fse.rm(exe)
140+
}
141+
resolve(code ?? 0)
142+
})
143+
})
139144
}
140-
} else {
141-
console.error(`unknown script: [${task}]`)
145+
return 0
142146
}
147+
148+
console.error(`unknown script: [${task}]`)
149+
return 1
143150
}

0 commit comments

Comments
 (0)