1
1
'use strict'
2
2
3
+ const { stripAnsi } = /*@__PURE__ */ require ( './strings' )
4
+
3
5
let _child_process
4
6
/*@__NO_SIDE_EFFECTS__ */
5
7
function getChildProcess ( ) {
@@ -20,33 +22,44 @@ function getSpawn() {
20
22
return _spawn
21
23
}
22
24
25
+ /*@__NO_SIDE_EFFECTS__ */
26
+ function isStdioType ( stdio , type ) {
27
+ return (
28
+ stdio === type ||
29
+ ( Array . isArray ( stdio ) &&
30
+ stdio . length > 2 &&
31
+ stdio [ 0 ] === type &&
32
+ stdio [ 1 ] === type &&
33
+ stdio [ 2 ] === type )
34
+ )
35
+ }
36
+
37
+ /*@__NO_SIDE_EFFECTS__ */
38
+ function stripAnsiFromSpawnResult ( result ) {
39
+ const { stderr, stdout } = result
40
+ if ( typeof stdout === 'string' ) {
41
+ result . stdout = stripAnsi ( stdout )
42
+ }
43
+ if ( typeof stderr === 'string' ) {
44
+ result . stderr = stripAnsi ( stderr )
45
+ }
46
+ return result
47
+ }
48
+
23
49
/*@__NO_SIDE_EFFECTS__ */
24
50
function spawn ( cmd , args , options , extra ) {
25
51
const {
26
52
spinner = /*@__PURE__ */ require ( './constants/spinner' ) ,
53
+ stripAnsi : shouldStripAnsi = true ,
27
54
...spawnOptions
28
55
} = { __proto__ : null , ...options }
29
56
const spawn = getSpawn ( )
30
57
const isSpinning = ! ! spinner ?. isSpinning
31
- const { env, stdio } = spawnOptions
58
+ const { env, stdio, stdioString = true } = spawnOptions
32
59
// The stdio option can be a string or an array.
33
60
// https://nodejs.org/api/child_process.html#optionsstdio
34
- const isStdioIgnored =
35
- stdio === 'ignore' ||
36
- ( Array . isArray ( stdio ) &&
37
- stdio . length > 2 &&
38
- stdio [ 0 ] === 'ignore' &&
39
- stdio [ 1 ] === 'ignore' &&
40
- stdio [ 2 ] === 'ignore' )
41
- const isStdioPiped =
42
- stdio === undefined ||
43
- stdio === 'pipe' ||
44
- ( Array . isArray ( stdio ) &&
45
- stdio . length > 2 &&
46
- stdio [ 0 ] === 'pipe' &&
47
- stdio [ 1 ] === 'pipe' &&
48
- stdio [ 2 ] === 'pipe' )
49
- const shouldPauseSpinner = ! isStdioIgnored && ! isStdioPiped
61
+ const shouldPauseSpinner =
62
+ ! isStdioType ( stdio , 'ignore' ) && ! isStdioType ( stdio , 'pipe' )
50
63
if ( shouldPauseSpinner ) {
51
64
spinner ?. stop ( )
52
65
}
@@ -69,14 +82,20 @@ function spawn(cmd, args, options, extra) {
69
82
} ,
70
83
extra
71
84
)
72
- if ( shouldPauseSpinner && isSpinning ) {
73
- const oldSpawnPromise = spawnPromise
85
+ const oldSpawnPromise = spawnPromise
86
+ if ( shouldStripAnsi && stdioString ) {
87
+ spawnPromise = spawnPromise
88
+ . then ( stripAnsiFromSpawnResult )
89
+ . catch ( stripAnsiFromSpawnResult )
90
+ }
91
+ if ( spinner && shouldPauseSpinner && isSpinning ) {
74
92
spawnPromise = spawnPromise . finally ( ( ) => {
75
93
spinner ?. start ( )
76
94
} )
77
- spawnPromise . process = oldSpawnPromise . process
78
- spawnPromise . stdin = oldSpawnPromise . stdin
79
95
}
96
+ spawnPromise . process = oldSpawnPromise . process
97
+ spawnPromise . stdin = oldSpawnPromise . stdin
98
+
80
99
return spawnPromise
81
100
}
82
101
0 commit comments