@@ -203,9 +203,7 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
203
203
} ,
204
204
}
205
205
)
206
- /**
207
- * State machine stages
208
- */
206
+
209
207
type ProcessStage = 'initial ' | 'halted' | 'running' | 'fulfilled' | 'rejected'
210
208
211
209
type Resolve = ( out : ProcessOutput ) => void
@@ -312,35 +310,23 @@ export class ProcessPromise extends Promise<ProcessOutput> {
312
310
$ . log ( { kind : 'stderr' , data, verbose : ! self . isQuiet ( ) , id } )
313
311
} ,
314
312
end : ( data , c ) => {
315
- const { error, status, signal, duration, ctx } = data
316
- const { stdout, stderr, stdall } = ctx . store
317
- const dto : ProcessOutputLazyDto = {
318
- code : ( ) => status ,
319
- signal : ( ) => signal ,
320
- duration : ( ) => duration ,
321
- stdout : once ( ( ) => bufArrJoin ( stdout ) ) ,
322
- stderr : once ( ( ) => bufArrJoin ( stderr ) ) ,
323
- stdall : once ( ( ) => bufArrJoin ( stdall ) ) ,
324
- message : once ( ( ) => ProcessOutput . getExitMessage (
325
- status ,
326
- signal ,
327
- dto . stderr ( ) ,
328
- self . _from
329
- ) ) ,
330
- ...error && {
331
- code : ( ) => null ,
332
- signal : ( ) => null ,
333
- message : ( ) => ProcessOutput . getErrorMessage ( error , self . _from )
334
- }
335
- }
313
+ const { error, status, signal, duration, ctx : { store} } = data
314
+ const { stdout, stderr } = store
315
+ const output = self . _output = new ProcessOutput ( {
316
+ code : status ,
317
+ signal,
318
+ error,
319
+ duration,
320
+ store,
321
+ from : self . _from ,
322
+ } )
323
+
324
+ $ . log ( { kind : 'end' , signal, exitCode : status , duration, error, verbose : self . isVerbose ( ) , id } )
336
325
337
326
// Ensures EOL
338
327
if ( stdout . length && getLast ( getLast ( stdout ) ) !== BR_CC ) c . on . stdout ! ( EOL , c )
339
328
if ( stderr . length && getLast ( getLast ( stderr ) ) !== BR_CC ) c . on . stderr ! ( EOL , c )
340
329
341
- $ . log ( { kind : 'end' , signal, exitCode : status , duration, error, verbose : self . isVerbose ( ) , id } )
342
- const output = self . _output = new ProcessOutput ( dto )
343
-
344
330
if ( error || status !== 0 && ! self . isNothrow ( ) ) {
345
331
self . _stage = 'rejected'
346
332
self . _reject ( output )
@@ -669,76 +655,72 @@ export class ProcessPromise extends Promise<ProcessOutput> {
669
655
}
670
656
}
671
657
672
- type GettersRecord < T extends Record < any , any > > = { [ K in keyof T ] : ( ) => T [ K ] }
673
-
674
- type ProcessOutputLazyDto = GettersRecord < {
658
+ type ProcessDto = {
675
659
code : number | null
676
660
signal : NodeJS . Signals | null
661
+ duration : number
662
+ error : any
663
+ from : string
664
+ store : TSpawnStore
665
+ }
666
+
667
+ type ProcessOutputDto = ProcessDto & {
677
668
stdout : string
678
669
stderr : string
679
670
stdall : string
680
671
message : string
681
- duration : number
682
- } >
672
+ }
683
673
684
674
export class ProcessOutput extends Error {
685
- private readonly _code : number | null = null
686
- private readonly _signal : NodeJS . Signals | null
687
- private readonly _stdout : string
688
- private readonly _stderr : string
689
- private readonly _combined : string
690
- private readonly _duration : number
691
-
692
- constructor ( dto : ProcessOutputLazyDto )
675
+ private readonly _dto : ProcessOutputDto
676
+ constructor ( dto : ProcessDto )
693
677
constructor (
694
678
code : number | null ,
695
679
signal : NodeJS . Signals | null ,
696
680
stdout : string ,
697
681
stderr : string ,
698
- combined : string ,
682
+ stdall : string ,
699
683
message : string ,
700
684
duration ?: number
701
685
)
702
686
constructor (
703
- code : number | null | ProcessOutputLazyDto ,
687
+ code : number | null | ProcessDto ,
704
688
signal : NodeJS . Signals | null = null ,
705
689
stdout : string = '' ,
706
690
stderr : string = '' ,
707
- combined : string = '' ,
691
+ stdall : string = '' ,
708
692
message : string = '' ,
709
693
duration : number = 0
710
694
) {
711
695
super ( message )
712
- this . _signal = signal
713
- this . _stdout = stdout
714
- this . _stderr = stderr
715
- this . _combined = combined
716
- this . _duration = duration
717
- if ( code !== null && typeof code === 'object' ) {
718
- Object . defineProperties ( this , {
719
- _code : { get : code . code } ,
720
- _signal : { get : code . signal } ,
721
- _duration : { get : code . duration } ,
722
- _stdout : { get : code . stdout } ,
723
- _stderr : { get : code . stderr } ,
724
- _combined : { get : code . stdall } ,
725
- message : { get : code . message } ,
726
- } )
727
- } else {
728
- this . _code = code
729
- }
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
+ }
730
712
}
731
713
732
714
toString ( ) : string {
733
- return this . _combined
715
+ return this . _dto . stdall
734
716
}
735
717
736
718
json < T = any > ( ) : T {
737
- return JSON . parse ( this . _combined )
719
+ return JSON . parse ( this . _dto . stdall )
738
720
}
739
721
740
722
buffer ( ) : Buffer {
741
- return Buffer . from ( this . _combined )
723
+ return Buffer . from ( this . _dto . stdall )
742
724
}
743
725
744
726
blob ( type = 'text/plain' ) : Blob {
@@ -760,27 +742,63 @@ export class ProcessOutput extends Error {
760
742
}
761
743
762
744
valueOf ( ) : string {
763
- return this . _combined . trim ( )
745
+ return this . _dto . stdall . trim ( )
764
746
}
765
747
766
748
get stdout ( ) : string {
767
- return this . _stdout
749
+ return this . _dto . stdout
768
750
}
769
751
770
752
get stderr ( ) : string {
771
- return this . _stderr
753
+ return this . _dto . stderr
772
754
}
773
755
774
756
get exitCode ( ) : number | null {
775
- return this . _code
757
+ return this . _dto . code
776
758
}
777
759
778
760
get signal ( ) : NodeJS . Signals | null {
779
- return this . _signal
761
+ return this . _dto . signal
780
762
}
781
763
782
764
get duration ( ) : number {
783
- return this . _duration
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
784
802
}
785
803
786
804
static getExitMessage = formatExitMessage
0 commit comments