Skip to content

Commit d96d745

Browse files
committed
Add stripAnsi option to spawn
1 parent b82fc3f commit d96d745

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

registry/lib/spawn.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare type SpawnOptions = Remap<
2323
BuiltinSpawnOptions & {
2424
spinner?: Spinner | undefined
2525
stdioString?: boolean | undefined
26+
stripAnsi?: boolean | undefined
2627
}
2728
>
2829
declare type SpawnExtra = Record<any, any>

registry/lib/spawn.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { stripAnsi } = /*@__PURE__*/ require('./strings')
4+
35
let _child_process
46
/*@__NO_SIDE_EFFECTS__*/
57
function getChildProcess() {
@@ -20,33 +22,44 @@ function getSpawn() {
2022
return _spawn
2123
}
2224

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+
2349
/*@__NO_SIDE_EFFECTS__*/
2450
function spawn(cmd, args, options, extra) {
2551
const {
2652
spinner = /*@__PURE__*/ require('./constants/spinner'),
53+
stripAnsi: shouldStripAnsi = true,
2754
...spawnOptions
2855
} = { __proto__: null, ...options }
2956
const spawn = getSpawn()
3057
const isSpinning = !!spinner?.isSpinning
31-
const { env, stdio } = spawnOptions
58+
const { env, stdio, stdioString = true } = spawnOptions
3259
// The stdio option can be a string or an array.
3360
// 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')
5063
if (shouldPauseSpinner) {
5164
spinner?.stop()
5265
}
@@ -69,14 +82,20 @@ function spawn(cmd, args, options, extra) {
6982
},
7083
extra
7184
)
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) {
7492
spawnPromise = spawnPromise.finally(() => {
7593
spinner?.start()
7694
})
77-
spawnPromise.process = oldSpawnPromise.process
78-
spawnPromise.stdin = oldSpawnPromise.stdin
7995
}
96+
spawnPromise.process = oldSpawnPromise.process
97+
spawnPromise.stdin = oldSpawnPromise.stdin
98+
8099
return spawnPromise
81100
}
82101

0 commit comments

Comments
 (0)