Skip to content

Commit

Permalink
feat: provide input option
Browse files Browse the repository at this point in the history
closes #720
  • Loading branch information
antongolub committed Mar 17, 2024
1 parent 0b0821c commit 981f50c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ const processCwd = Symbol('processCwd')
export interface Options {
[processCwd]: string
cwd?: string
verbose: boolean
ac?: AbortController
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
verbose: boolean
env: NodeJS.ProcessEnv
shell: string | boolean
nothrow: boolean
Expand Down Expand Up @@ -187,18 +188,23 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

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

const $ = this._snapshot
const self = this
const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input

if (input) this.stdio('pipe')

$.log({
kind: 'cmd',
cmd: this._command,
verbose: self.isVerbose(),
})

this.zurk = exec({
input,
cmd: $.prefix + this._command,
cwd: $.cwd ?? $[processCwd],
ac: $.ac,
Expand Down
16 changes: 15 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import assert from 'node:assert'
import { test, describe, beforeEach } from 'node:test'
import { inspect } from 'node:util'
import { Writable } from 'node:stream'
import { Readable, Writable } from 'node:stream'
import { Socket } from 'node:net'
import { ProcessPromise, ProcessOutput } from '../build/index.js'
import '../build/globals.js'
Expand Down Expand Up @@ -106,6 +106,20 @@ describe('core', () => {
}
})

test('handles `input` option', async () => {
const p1 = $({ input: 'foo' })`cat`
const p2 = $({ input: Readable.from('bar') })`cat`
const p3 = $({ input: Buffer.from('baz') })`cat`
const p4 = $({ input: p3 })`cat`
const p5 = $({ input: await p3 })`cat`

assert.equal((await p1).stdout, 'foo')
assert.equal((await p2).stdout, 'bar')
assert.equal((await p3).stdout, 'baz')
assert.equal((await p4).stdout, 'baz')
assert.equal((await p5).stdout, 'baz')
})

test('pipes are working', async () => {
let { stdout } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
Expand Down

0 comments on commit 981f50c

Please sign in to comment.