Skip to content

Commit

Permalink
refactor: simplify ProcessOutput internals
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Feb 9, 2025
1 parent 327dd7a commit 9239536
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 86 deletions.
128 changes: 43 additions & 85 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,8 @@ type ProcessDto = {
store: TSpawnStore
}

type ProcessOutputDto = ProcessDto & {
stdout: string
stderr: string
stdall: string
message: string
}

export class ProcessOutput extends Error {
private readonly _dto: ProcessOutputDto
private readonly _dto: ProcessDto
constructor(dto: ProcessDto)
constructor(
code: number | null,
Expand All @@ -683,44 +676,65 @@ export class ProcessOutput extends Error {
message: string,
duration?: number
)
// prettier-ignore
constructor(
code: number | null | ProcessDto,
signal: NodeJS.Signals | null = null,
stdout: string = '',
stderr: string = '',
stdall: string = '',
message: string = '',
duration: number = 0
duration: number = 0,
error: any = null,
from: string = '',
store: TSpawnStore = { stdout: [stdout], stderr: [stderr], stdall: [stdall], }
) {
super(message)
Reflect.deleteProperty(this, 'message')
this._dto =
code !== null && typeof code === 'object'
? ProcessOutput.createLazyDto(code)
: {
code,
signal,
duration,
stdout,
stderr,
stdall,
error: null,
from: '',
message,
store: { stdout: [], stderr: [], stdall: [] },
}
const dto = this._dto = code !== null && typeof code === 'object'
? code
: { code, signal, duration, error, from, store }

Object.defineProperties(this,
{
stdout: { get: once(() => bufArrJoin(dto.store.stdout)) },
stderr: { get: once(() => bufArrJoin(dto.store.stderr)) },
stdall: { get: once(() => bufArrJoin(dto.store.stdall)) },
message: { get: once(() =>
message || dto.error
? ProcessOutput.getErrorMessage(dto.error, dto.from)
: ProcessOutput.getExitMessage(dto.code, dto.signal, this.stderr, dto.from)
),
},
}
)
}
message!: string
stdout!: string
stderr!: string
stdall!: string

get exitCode(): number | null {
return this._dto.code
}

get signal(): NodeJS.Signals | null {
return this._dto.signal
}

get duration(): number {
return this._dto.duration
}

toString(): string {
return this._dto.stdall
return this.stdall
}

json<T = any>(): T {
return JSON.parse(this._dto.stdall)
return JSON.parse(this.stdall)
}

buffer(): Buffer {
return Buffer.from(this._dto.stdall)
return Buffer.from(this.stdall)
}

blob(type = 'text/plain'): Blob {
Expand All @@ -742,63 +756,7 @@ export class ProcessOutput extends Error {
}

valueOf(): string {
return this._dto.stdall.trim()
}

get stdout(): string {
return this._dto.stdout
}

get stderr(): string {
return this._dto.stderr
}

get exitCode(): number | null {
return this._dto.code
}

get signal(): NodeJS.Signals | null {
return this._dto.signal
}

get duration(): number {
return this._dto.duration
}

get message(): string {
return this._dto.message
}

private static createLazyDto({
code,
signal,
duration,
store: { stdout, stderr, stdall },
from,
error,
}: ProcessDto): ProcessOutputDto {
const dto = Object.defineProperties(
{
code,
signal,
duration,
},
{
stdout: { get: once(() => bufArrJoin(stdout)) },
stderr: { get: once(() => bufArrJoin(stderr)) },
stdall: { get: once(() => bufArrJoin(stdall)) },
message: {
get: once(() =>
ProcessOutput.getExitMessage(code, signal, dto.stderr, from)
),
},
...(error && {
message: { get: () => ProcessOutput.getErrorMessage(error, from) },
}),
}
) as ProcessOutputDto

return dto
return this.stdall.trim()
}

static getExitMessage = formatExitMessage
Expand Down
2 changes: 1 addition & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ describe('core', () => {
await p
})

test('throws if the signal was previously aborted', async () => {
test.only('throws if the signal was previously aborted', async () => {
const ac = new AbortController()
const { signal } = ac
ac.abort('reason')
Expand Down

0 comments on commit 9239536

Please sign in to comment.