Skip to content

Commit

Permalink
Add signal field to ProcessOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 27, 2022
1 parent bf88f50 commit 53a215e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -131,6 +131,7 @@ class ProcessOutput {
readonly stdout: string
readonly stderr: string
readonly exitCode: number
readonly signal: 'SIGTERM' | 'SIGKILL' | ...
toString(): string
}
```
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Expand Up @@ -45,7 +45,8 @@ export interface ProcessPromise<T> extends Promise<T> {
}

export class ProcessOutput {
readonly exitCode: number
readonly exitCode: number | null
readonly signal: NodeJS.Signals | null
readonly stdout: string
readonly stderr: string

Expand Down
36 changes: 25 additions & 11 deletions index.mjs
Expand Up @@ -88,15 +88,22 @@ export function $(pieces, ...args) {
maxBuffer: 200 * 1024 * 1024, // 200 MiB
})

child.on('exit', code => {
child.on('close', () => {
let output = new ProcessOutput({
code, stdout, stderr, combined,
message: `${stderr || '\n'} at ${__from}\n exit code: ${code}` + (exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : '')
});
(code === 0 || promise._nothrow ? resolve : reject)(output)
promise._resolved = true
})
child.on('close', (code, signal) => {
let message = `${stderr || '\n'} at ${__from}`
message += `\n exit code: ${code}${exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : ''}`
if (signal !== null) {
message += `\n signal: ${signal}`
}
let output = new ProcessOutput({
code,
signal,
stdout,
stderr,
combined,
message,
});
(code === 0 || promise._nothrow ? resolve : reject)(output)
promise._resolved = true
})

let stdout = '', stderr = '', combined = ''
Expand Down Expand Up @@ -257,14 +264,16 @@ export class ProcessPromise extends Promise {
}

export class ProcessOutput extends Error {
#code = 0
#code = null
#signal = null
#stdout = ''
#stderr = ''
#combined = ''

constructor({code, stdout, stderr, combined, message}) {
constructor({code, signal, stdout, stderr, combined, message}) {
super(message)
this.#code = code
this.#signal = signal
this.#stdout = stdout
this.#stderr = stderr
this.#combined = combined
Expand All @@ -286,11 +295,16 @@ export class ProcessOutput extends Error {
return this.#code
}

get signal() {
return this.#signal
}

[inspect.custom]() {
let stringify = (s, c) => s.length === 0 ? '\'\'' : c(inspect(s))
return `ProcessOutput {
stdout: ${stringify(this.stdout, chalk.green)},
stderr: ${stringify(this.stderr, chalk.red)},
signal: ${inspect(this.signal)},
exitCode: ${(this.exitCode === 0 ? chalk.green : chalk.red)(this.exitCode)}${(exitCodeInfo(this.exitCode) ? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')') : '')}
}`
}
Expand Down
12 changes: 12 additions & 0 deletions test.mjs
Expand Up @@ -222,6 +222,18 @@ if (test('The kill() method works')) {
await p
}

if (test('The signal is passed with kill() method')) {
let p = $`while true; do :; done`
setTimeout(() => p.kill('SIGKILL'), 100)
let signal
try {
await p
} catch (p) {
signal = p.signal
}
assert.equal(signal, 'SIGKILL')
}

if (test('YAML works')) {
assert.deepEqual(YAML.parse(YAML.stringify({foo: 'bar'})), {foo: 'bar'})
console.log(chalk.greenBright('YAML works'))
Expand Down

0 comments on commit 53a215e

Please sign in to comment.