Skip to content

Commit a21f8b4

Browse files
committed
Add stdioString and stripAnsi options to spawnSync
1 parent 189f9c3 commit a21f8b4

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ function configs(sourceType) {
235235
...js.configs.recommended.languageOptions?.globals,
236236
...importFlatConfigs.recommended.languageOptions?.globals,
237237
...nodePluginConfigs.languageOptions?.globals,
238-
...nodeGlobalsConfig
238+
...nodeGlobalsConfig,
239+
NodeJS: false
239240
},
240241
sourceType: isEsm ? 'module' : 'script'
241242
},

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

registry/lib/spawn.d.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import {
33
SpawnOptions as BaseSpawnOptions,
44
ChildProcess,
5-
IOType,
6-
spawnSync as childProcessSpawnSync
5+
IOType
76
} from 'node:child_process'
87
import Stream from 'node:stream'
98

@@ -54,6 +53,22 @@ declare type SpawnOptions = Remap<
5453
stripAnsi?: boolean | undefined
5554
}
5655
>
56+
declare type SpawnSyncOptions = Omit<SpawnOptions, 'spinner'>
57+
declare interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
58+
encoding: NodeJS.BufferEncoding
59+
}
60+
declare interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
61+
encoding?: 'buffer' | null | undefined
62+
}
63+
declare interface SpawnSyncReturns<T> {
64+
pid: number
65+
output: Array<T | null>
66+
stdout: T
67+
stderr: T
68+
status: number | null
69+
signal: NodeJS.Signals | null
70+
error?: Error | undefined
71+
}
5772
declare type StdioType = IOType | 'ipc' | Array<IOType | 'ipc'>
5873
declare const Spawn: {
5974
isSpawnError(value: any): value is SpawnError
@@ -70,7 +85,35 @@ declare const Spawn: {
7085
O extends { stdioString: false } ? Buffer : string,
7186
typeof extra
7287
>
73-
spawnSync: typeof childProcessSpawnSync
88+
spawnSync(command: string): SpawnSyncReturns<Buffer>
89+
spawnSync(
90+
command: string,
91+
options: SpawnSyncOptionsWithStringEncoding
92+
): SpawnSyncReturns<string>
93+
spawnSync(
94+
command: string,
95+
options: SpawnSyncOptionsWithBufferEncoding
96+
): SpawnSyncReturns<Buffer>
97+
spawnSync(
98+
command: string,
99+
options?: SpawnSyncOptions
100+
): SpawnSyncReturns<string | Buffer>
101+
spawnSync(command: string, args: readonly string[]): SpawnSyncReturns<Buffer>
102+
spawnSync(
103+
command: string,
104+
args: readonly string[],
105+
options: SpawnSyncOptionsWithStringEncoding
106+
): SpawnSyncReturns<string>
107+
spawnSync(
108+
command: string,
109+
args: readonly string[],
110+
options: SpawnSyncOptionsWithBufferEncoding
111+
): SpawnSyncReturns<Buffer>
112+
spawnSync(
113+
command: string,
114+
args?: readonly string[],
115+
options?: SpawnSyncOptions
116+
): SpawnSyncReturns<string | Buffer>
74117
}
75118
declare namespace Spawn {
76119
export {

registry/lib/spawn.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,24 @@ function spawn(cmd, args, options, extra) {
119119
}
120120

121121
/*@__NO_SIDE_EFFECTS__*/
122-
function spawnSync(...args) {
123-
return getChildProcess().spawnSync(...args)
122+
function spawnSync(cmd, args, options) {
123+
const { stripAnsi: shouldStripAnsi = true, ...spawnOptions } = {
124+
__proto__: null,
125+
...options
126+
}
127+
const { stdioString = true } = spawnOptions
128+
const encoding = stdioString ? 'utf8' : 'buffer'
129+
const result = getChildProcess().spawnSync(cmd, args, {
130+
encoding,
131+
...spawnOptions
132+
})
133+
if (stdioString) {
134+
result.stdout = result.stdout.toString().trim()
135+
result.stderr = result.stderr.toString().trim()
136+
}
137+
return shouldStripAnsi && stdioString
138+
? stripAnsiFromSpawnResult(result)
139+
: result
124140
}
125141

126142
module.exports = {

registry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@
629629
"@socketregistry/is-unicode-supported": "1.0.5",
630630
"@socketregistry/packageurl-js": "1.0.8",
631631
"@socketregistry/yocto-spinner": "1.0.20",
632+
"@types/node": "24.1.0",
632633
"@types/pacote": "11.1.8",
633634
"@yarnpkg/extensions": "2.0.6",
634635
"ansi-regex": "6.1.0",

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"strict": true,
2020
"strictNullChecks": true,
2121
"target": "esnext",
22+
"types": ["node"],
2223
"useUnknownInCatchVariables": true,
2324

2425
/* New checks being tried out */

0 commit comments

Comments
 (0)