Skip to content

Commit 0468172

Browse files
committed
feat: add stdio option
closes google#771
1 parent 5ad9ce3 commit 0468172

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"typescript": "^5.4.4",
8181
"which": "^4.0.0",
8282
"yaml": "^2.4.1",
83-
"zurk": "^0.1.0"
83+
"zurk": "^0.1.2"
8484
},
8585
"publishConfig": {
8686
"registry": "https://wombat-dressing-room.appspot.com"

src/core.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import { spawn, spawnSync, StdioNull, StdioPipe } from 'node:child_process'
16+
import { spawn, spawnSync, StdioOptions, IOType } from 'node:child_process'
1717
import { AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'
1818
import { Readable, Writable } from 'node:stream'
1919
import { inspect } from 'node:util'
@@ -59,6 +59,7 @@ export interface Options {
5959
ac?: AbortController
6060
signal?: AbortSignal
6161
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
62+
stdio: StdioOptions
6263
verbose: boolean
6364
sync: boolean
6465
env: NodeJS.ProcessEnv
@@ -95,6 +96,7 @@ export const defaults: Options = {
9596
env: process.env,
9697
sync: false,
9798
shell: true,
99+
stdio: ['inherit', 'pipe', 'pipe'],
98100
nothrow: false,
99101
quiet: false,
100102
prefix: '',
@@ -192,15 +194,14 @@ try {
192194
} catch (err) {}
193195

194196
type Resolve = (out: ProcessOutput) => void
195-
type IO = StdioPipe | StdioNull
196197

197198
export class ProcessPromise extends Promise<ProcessOutput> {
198199
private _command = ''
199200
private _from = ''
200201
private _resolve: Resolve = noop
201202
private _reject: Resolve = noop
202203
private _snapshot = getStore()
203-
private _stdio: [IO, IO, IO] = ['inherit', 'pipe', 'pipe']
204+
private _stdio?: StdioOptions
204205
private _nothrow?: boolean
205206
private _quiet?: boolean
206207
private _timeout?: number
@@ -245,15 +246,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {
245246

246247
this._zurk = exec({
247248
input,
248-
cmd: $.prefix + this._command + $.postfix,
249+
cmd: $.prefix + self._command + $.postfix,
249250
cwd: $.cwd ?? $[processCwd],
250251
ac: $.ac,
251252
signal: $.signal,
252253
shell: typeof $.shell === 'string' ? $.shell : true,
253254
env: $.env,
254255
spawn: $.spawn,
255256
spawnSync: $.spawnSync,
256-
stdio: this._stdio as any,
257+
stdio: self._stdio ?? $.stdio,
257258
sync: $[syncExec],
258259
detached: !isWin,
259260
run: (cb) => cb(),
@@ -427,7 +428,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
427428
return $.kill(this.child.pid, signal)
428429
}
429430

430-
stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {
431+
stdio(
432+
stdin: IOType,
433+
stdout: IOType = 'pipe',
434+
stderr: IOType = 'pipe'
435+
): ProcessPromise {
431436
this._stdio = [stdin, stdout, stderr]
432437
return this
433438
}

test/core.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ describe('core', () => {
451451
assert.equal((await b).stdout, 'bar')
452452
})
453453

454+
test('stdio as option', async () => {
455+
let p = $({ stdio: 'ignore' })`echo foo`
456+
457+
assert.equal((await p).stdout, '')
458+
})
459+
454460
test('snapshots works', async () => {
455461
await within(async () => {
456462
$.prefix += 'echo success;'

test/fixtures/js-project/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)