Skip to content

Commit

Permalink
style: simplify process end handler
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 18, 2024
1 parent 25b1bae commit a7da0b7
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,59 +321,50 @@ export class ProcessPromise extends Promise<ProcessOutput> {
// Stderr should be printed regardless of piping.
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
},
// prettier-ignore
end: (data, c) => {
self._resolved = true
const { error, status, signal, duration, ctx } = data
const { stdout, stderr, stdall } = ctx.store

// Lazy getters
const _stdout = once(() => stdout.join(''))
const _stderr = once(() => stderr.join(''))
const _stdall = once(() => stdall.join(''))
const _duration = () => duration
let _code = () => status
let _signal = () => signal
let _message = once(() => ProcessOutput.getExitMessage(
status,
signal,
_stderr(),
self._from
))

// Ensures EOL
// prettier-ignore
if (stdout.length && !stdout[stdout.length - 1]?.toString().endsWith('\n')) c.on.stdout?.(eol, c)
// prettier-ignore
if (stderr.length && !stderr[stderr.length - 1]?.toString().endsWith('\n')) c.on.stderr?.(eol, c)
// prettier-ignore
if (error) {
// Should we enable this?
// (nothrow ? self._resolve : self._reject)(

const message = ProcessOutput.getErrorMessage(error, self._from)
const output = new ProcessOutput({
code: null,
signal: null,
get stdout() { return _stdout() },
get stderr() { return _stderr() },
get stdall() { return _stdall() },
message,
duration
})
self._output = output
_code = () => null
_signal = () => null
_message = () => ProcessOutput.getErrorMessage(error, self._from)
}

const output = new ProcessOutput({
code: _code,
signal: _signal,
stdout: _stdout,
stderr: _stderr,
stdall: _stdall,
message: _message,
duration: _duration
})
self._output = output

if (error || status !== 0 && !self.isNothrow()) {
self._reject(output)
} else {
const _message = once(() => ProcessOutput.getExitMessage(
status,
signal,
_stderr(),
self._from
))
const output = new ProcessOutput({
code: status,
signal,
get message() { return _message() },
get stdout() { return _stdout() },
get stderr() { return _stderr() },
get stdall() { return _stdall() },
duration
})

self._output = output
if (status === 0 || self.isNothrow()) {
self._resolve(output)
} else {
self._reject(output)
}
self._resolve(output)
}
},
},
Expand Down Expand Up @@ -580,15 +571,17 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}
}

export type ProcessOutputDto = {
type GettersRecord<T extends Record<any, any>> = { [K in keyof T]: () => T[K] }

type ProcessOutputLazyDto = GettersRecord<{
code: number | null
signal: NodeJS.Signals | null
stdout: string
stderr: string
stdall: string
duration: number
message: string
}
duration: number
}>

export class ProcessOutput extends Error {
private readonly _code: number | null
Expand All @@ -599,7 +592,7 @@ export class ProcessOutput extends Error {
private readonly _duration: number

constructor(
code: number | null | ProcessOutputDto,
code: number | null | ProcessOutputLazyDto,
signal: NodeJS.Signals | null = null,
stdout: string = '',
stderr: string = '',
Expand All @@ -614,15 +607,16 @@ export class ProcessOutput extends Error {
this._combined = combined
this._duration = duration
if (code !== null && typeof code === 'object') {
this._code = code.code
this._signal = code.signal
this._duration = code.duration
this._code = null
// prettier-ignore
Object.defineProperties(this, {
message: { get() { return code.message }},
_stdout: { get() { return code.stdout }},
_stderr: { get() { return code.stderr }},
_combined: { get() { return code.stdall }},
_code: { get: code.code},
_signal: { get: code.signal},
_duration: { get: code.duration},
message: { get: code.message},
_stdout: { get: code.stdout},
_stderr: { get: code.stderr },
_combined: { get: code.stdall },
})
} else {
this._code = code
Expand Down

0 comments on commit a7da0b7

Please sign in to comment.