Skip to content

Commit

Permalink
feat: provide input option (#736)
Browse files Browse the repository at this point in the history
* chore(deps): update yaml to 2.4.1

* feat: provide `input` option

closes #720
  • Loading branch information
antongolub authored Mar 17, 2024
1 parent fe0356f commit b38972e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
52 changes: 29 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@
"node-fetch-native": "^1.6.2",
"prettier": "^2.8.8",
"ps-tree": "^1.2.0",
"tsd": "^0.28.1",
"tsd": "^0.30.7",
"typescript": "^5.0.4",
"webpod": "^0",
"which": "^3.0.0",
"yaml": "^2.3.4",
"yaml": "^2.4.1",
"zurk": "^0.0.31"
},
"publishConfig": {
Expand Down
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 b38972e

Please sign in to comment.