Skip to content

Commit d5a6dff

Browse files
authored
refactor: simplify ProcessOutput internals (#1096)
* refactor: simplify `ProcessOutput` internals * chore: formatting * chore: reenable tests
1 parent 327dd7a commit d5a6dff

File tree

1 file changed

+41
-85
lines changed

1 file changed

+41
-85
lines changed

src/core.ts

+41-85
Original file line numberDiff line numberDiff line change
@@ -664,15 +664,8 @@ type ProcessDto = {
664664
store: TSpawnStore
665665
}
666666

667-
type ProcessOutputDto = ProcessDto & {
668-
stdout: string
669-
stderr: string
670-
stdall: string
671-
message: string
672-
}
673-
674667
export class ProcessOutput extends Error {
675-
private readonly _dto: ProcessOutputDto
668+
private readonly _dto: ProcessDto
676669
constructor(dto: ProcessDto)
677670
constructor(
678671
code: number | null,
@@ -683,44 +676,63 @@ export class ProcessOutput extends Error {
683676
message: string,
684677
duration?: number
685678
)
679+
// prettier-ignore
686680
constructor(
687681
code: number | null | ProcessDto,
688682
signal: NodeJS.Signals | null = null,
689683
stdout: string = '',
690684
stderr: string = '',
691685
stdall: string = '',
692686
message: string = '',
693-
duration: number = 0
687+
duration: number = 0,
688+
error: any = null,
689+
from: string = '',
690+
store: TSpawnStore = { stdout: [stdout], stderr: [stderr], stdall: [stdall], }
694691
) {
695692
super(message)
696-
Reflect.deleteProperty(this, 'message')
697-
this._dto =
698-
code !== null && typeof code === 'object'
699-
? ProcessOutput.createLazyDto(code)
700-
: {
701-
code,
702-
signal,
703-
duration,
704-
stdout,
705-
stderr,
706-
stdall,
707-
error: null,
708-
from: '',
709-
message,
710-
store: { stdout: [], stderr: [], stdall: [] },
711-
}
693+
const dto = this._dto = code !== null && typeof code === 'object'
694+
? code
695+
: { code, signal, duration, error, from, store }
696+
697+
Object.defineProperties(this, {
698+
stdout: { get: once(() => bufArrJoin(dto.store.stdout)) },
699+
stderr: { get: once(() => bufArrJoin(dto.store.stderr)) },
700+
stdall: { get: once(() => bufArrJoin(dto.store.stdall)) },
701+
message: { get: once(() =>
702+
message || dto.error
703+
? ProcessOutput.getErrorMessage(dto.error, dto.from)
704+
: ProcessOutput.getExitMessage(dto.code, dto.signal, this.stderr, dto.from)
705+
),
706+
},
707+
})
708+
}
709+
message!: string
710+
stdout!: string
711+
stderr!: string
712+
stdall!: string
713+
714+
get exitCode(): number | null {
715+
return this._dto.code
716+
}
717+
718+
get signal(): NodeJS.Signals | null {
719+
return this._dto.signal
720+
}
721+
722+
get duration(): number {
723+
return this._dto.duration
712724
}
713725

714726
toString(): string {
715-
return this._dto.stdall
727+
return this.stdall
716728
}
717729

718730
json<T = any>(): T {
719-
return JSON.parse(this._dto.stdall)
731+
return JSON.parse(this.stdall)
720732
}
721733

722734
buffer(): Buffer {
723-
return Buffer.from(this._dto.stdall)
735+
return Buffer.from(this.stdall)
724736
}
725737

726738
blob(type = 'text/plain'): Blob {
@@ -742,63 +754,7 @@ export class ProcessOutput extends Error {
742754
}
743755

744756
valueOf(): string {
745-
return this._dto.stdall.trim()
746-
}
747-
748-
get stdout(): string {
749-
return this._dto.stdout
750-
}
751-
752-
get stderr(): string {
753-
return this._dto.stderr
754-
}
755-
756-
get exitCode(): number | null {
757-
return this._dto.code
758-
}
759-
760-
get signal(): NodeJS.Signals | null {
761-
return this._dto.signal
762-
}
763-
764-
get duration(): number {
765-
return this._dto.duration
766-
}
767-
768-
get message(): string {
769-
return this._dto.message
770-
}
771-
772-
private static createLazyDto({
773-
code,
774-
signal,
775-
duration,
776-
store: { stdout, stderr, stdall },
777-
from,
778-
error,
779-
}: ProcessDto): ProcessOutputDto {
780-
const dto = Object.defineProperties(
781-
{
782-
code,
783-
signal,
784-
duration,
785-
},
786-
{
787-
stdout: { get: once(() => bufArrJoin(stdout)) },
788-
stderr: { get: once(() => bufArrJoin(stderr)) },
789-
stdall: { get: once(() => bufArrJoin(stdall)) },
790-
message: {
791-
get: once(() =>
792-
ProcessOutput.getExitMessage(code, signal, dto.stderr, from)
793-
),
794-
},
795-
...(error && {
796-
message: { get: () => ProcessOutput.getErrorMessage(error, from) },
797-
}),
798-
}
799-
) as ProcessOutputDto
800-
801-
return dto
757+
return this.stdall.trim()
802758
}
803759

804760
static getExitMessage = formatExitMessage

0 commit comments

Comments
 (0)