Skip to content

Commit 981f50c

Browse files
committed
feat: provide input option
closes google#720
1 parent 0b0821c commit 981f50c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/core.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ const processCwd = Symbol('processCwd')
4848
export interface Options {
4949
[processCwd]: string
5050
cwd?: string
51-
verbose: boolean
5251
ac?: AbortController
52+
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
53+
verbose: boolean
5354
env: NodeJS.ProcessEnv
5455
shell: string | boolean
5556
nothrow: boolean
@@ -187,18 +188,23 @@ export class ProcessPromise extends Promise<ProcessOutput> {
187188
}
188189

189190
run(): ProcessPromise {
190-
const $ = this._snapshot
191-
const self = this
192191
if (this.child) return this // The _run() can be called from a few places.
193192
this._prerun() // In case $1.pipe($2), the $2 returned, and on $2._run() invoke $1._run().
194193

194+
const $ = this._snapshot
195+
const self = this
196+
const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input
197+
198+
if (input) this.stdio('pipe')
199+
195200
$.log({
196201
kind: 'cmd',
197202
cmd: this._command,
198203
verbose: self.isVerbose(),
199204
})
200205

201206
this.zurk = exec({
207+
input,
202208
cmd: $.prefix + this._command,
203209
cwd: $.cwd ?? $[processCwd],
204210
ac: $.ac,

test/core.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import assert from 'node:assert'
1616
import { test, describe, beforeEach } from 'node:test'
1717
import { inspect } from 'node:util'
18-
import { Writable } from 'node:stream'
18+
import { Readable, Writable } from 'node:stream'
1919
import { Socket } from 'node:net'
2020
import { ProcessPromise, ProcessOutput } from '../build/index.js'
2121
import '../build/globals.js'
@@ -106,6 +106,20 @@ describe('core', () => {
106106
}
107107
})
108108

109+
test('handles `input` option', async () => {
110+
const p1 = $({ input: 'foo' })`cat`
111+
const p2 = $({ input: Readable.from('bar') })`cat`
112+
const p3 = $({ input: Buffer.from('baz') })`cat`
113+
const p4 = $({ input: p3 })`cat`
114+
const p5 = $({ input: await p3 })`cat`
115+
116+
assert.equal((await p1).stdout, 'foo')
117+
assert.equal((await p2).stdout, 'bar')
118+
assert.equal((await p3).stdout, 'baz')
119+
assert.equal((await p4).stdout, 'baz')
120+
assert.equal((await p5).stdout, 'baz')
121+
})
122+
109123
test('pipes are working', async () => {
110124
let { stdout } = await $`echo "hello"`
111125
.pipe($`awk '{print $1" world"}'`)

0 commit comments

Comments
 (0)