Skip to content

Commit

Permalink
feat: add stdio option
Browse files Browse the repository at this point in the history
closes #771
  • Loading branch information
antongolub committed Apr 7, 2024
1 parent 5ad9ce3 commit 0468172
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"typescript": "^5.4.4",
"which": "^4.0.0",
"yaml": "^2.4.1",
"zurk": "^0.1.0"
"zurk": "^0.1.2"
},
"publishConfig": {
"registry": "https://wombat-dressing-room.appspot.com"
Expand Down
17 changes: 11 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import assert from 'node:assert'
import { spawn, spawnSync, StdioNull, StdioPipe } from 'node:child_process'
import { spawn, spawnSync, StdioOptions, IOType } from 'node:child_process'
import { AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'
import { Readable, Writable } from 'node:stream'
import { inspect } from 'node:util'
Expand Down Expand Up @@ -59,6 +59,7 @@ export interface Options {
ac?: AbortController
signal?: AbortSignal
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
stdio: StdioOptions
verbose: boolean
sync: boolean
env: NodeJS.ProcessEnv
Expand Down Expand Up @@ -95,6 +96,7 @@ export const defaults: Options = {
env: process.env,
sync: false,
shell: true,
stdio: ['inherit', 'pipe', 'pipe'],
nothrow: false,
quiet: false,
prefix: '',
Expand Down Expand Up @@ -192,15 +194,14 @@ try {
} catch (err) {}

type Resolve = (out: ProcessOutput) => void
type IO = StdioPipe | StdioNull

export class ProcessPromise extends Promise<ProcessOutput> {
private _command = ''
private _from = ''
private _resolve: Resolve = noop
private _reject: Resolve = noop
private _snapshot = getStore()
private _stdio: [IO, IO, IO] = ['inherit', 'pipe', 'pipe']
private _stdio?: StdioOptions
private _nothrow?: boolean
private _quiet?: boolean
private _timeout?: number
Expand Down Expand Up @@ -245,15 +246,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {

this._zurk = exec({
input,
cmd: $.prefix + this._command + $.postfix,
cmd: $.prefix + self._command + $.postfix,
cwd: $.cwd ?? $[processCwd],
ac: $.ac,
signal: $.signal,
shell: typeof $.shell === 'string' ? $.shell : true,
env: $.env,
spawn: $.spawn,
spawnSync: $.spawnSync,
stdio: this._stdio as any,
stdio: self._stdio ?? $.stdio,
sync: $[syncExec],
detached: !isWin,
run: (cb) => cb(),
Expand Down Expand Up @@ -427,7 +428,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return $.kill(this.child.pid, signal)
}

stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {
stdio(
stdin: IOType,
stdout: IOType = 'pipe',
stderr: IOType = 'pipe'
): ProcessPromise {
this._stdio = [stdin, stdout, stderr]
return this
}
Expand Down
6 changes: 6 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ describe('core', () => {
assert.equal((await b).stdout, 'bar')
})

test('stdio as option', async () => {
let p = $({ stdio: 'ignore' })`echo foo`

assert.equal((await p).stdout, '')
})

test('snapshots works', async () => {
await within(async () => {
$.prefix += 'echo success;'
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/js-project/package-lock.json

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

0 comments on commit 0468172

Please sign in to comment.