Skip to content

Commit cc57e45

Browse files
feat: add reject option (#19)
* feat: add `reject` option * add tests * refactor: code style --------- Co-authored-by: Kiko Beats <[email protected]>
1 parent 83ca7ee commit cc57e45

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { ChildProcess, SpawnOptions } from 'child_process';
22

33
interface ExtendOptions extends SpawnOptions {
44
json?: boolean;
5+
reject?: boolean;
56
}
67

78
interface ResolvedSubprocess extends Omit<ChildProcess, 'stdout' | 'stderr'> {
89
stdout: string;
910
stderr: string;
11+
/** Only exists if `reject` was false and the child process exited with a non-zero code. */
12+
error?: Error;
1013
}
1114

1215
export interface SubprocessPromise extends Promise<ResolvedSubprocess>, ChildProcess {}

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ const extend = defaults => (input, args, options) => {
5858
get: parse(stdout, opts)
5959
})
6060
Object.defineProperty(childProcess, 'stderr', { get: parse(stderr) })
61-
return exitCode === 0
62-
? resolve(childProcess)
63-
: reject(createChildProcessError({ cmd, cmdArgs, childProcess }))
61+
if (exitCode !== 0) {
62+
const error = createChildProcessError({ cmd, cmdArgs, childProcess })
63+
if (opts.reject !== false) return reject(error)
64+
childProcess.error = error
65+
}
66+
return resolve(childProcess)
6467
})
6568
})
6669

test/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,17 @@ test('handle stdout/stderr as inherit', async t => {
168168
t.is(stdout, '')
169169
t.is(stderr, '')
170170
})
171+
172+
test('resolve even if exit code is not 0 if reject=false', async t => {
173+
const result = await $('exit 1', { reject: false })
174+
t.is(result.exitCode, 1)
175+
t.is(result.error instanceof Error, true)
176+
})
177+
178+
test('resolve even if killed if reject=false', async t => {
179+
const subprocess = $('sleep 1', { reject: false })
180+
subprocess.kill()
181+
const result = await subprocess
182+
t.is(result.signalCode, 'SIGTERM')
183+
t.is(result.error instanceof Error, true)
184+
})

0 commit comments

Comments
 (0)