From b38972e8001782f88a04feabeb89271523654e3f Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 17 Mar 2024 23:27:19 +0300 Subject: [PATCH] feat: provide `input` option (#736) * chore(deps): update yaml to 2.4.1 * feat: provide `input` option closes #720 --- package-lock.json | 52 ++++++++++++++++++++++++++--------------------- package.json | 4 ++-- src/core.ts | 12 ++++++++--- test/core.test.js | 16 ++++++++++++++- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ecfde83fe..92e9992ea4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,11 +32,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" }, "engines": { @@ -1413,10 +1413,13 @@ } }, "node_modules/@tsd/typescript": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/@tsd/typescript/-/typescript-5.0.4.tgz", - "integrity": "sha512-YQi2lvZSI+xidKeUjlbv6b6Zw7qB3aXHw5oGJLs5OOGAEqKIOvz5UIAkWyg0bJbkSUWPBEtaOHpVxU4EYBO1Jg==", - "dev": true + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/@tsd/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-CQlfzol0ldaU+ftWuG52vH29uRoKboLinLy84wS8TQOu+m+tWoaUfk4svL4ij2V8M5284KymJBlHUusKj6k34w==", + "dev": true, + "engines": { + "node": ">=14.17" + } }, "node_modules/@types/eslint": { "version": "7.29.0", @@ -6403,12 +6406,12 @@ } }, "node_modules/tsd": { - "version": "0.28.1", - "resolved": "https://registry.npmjs.org/tsd/-/tsd-0.28.1.tgz", - "integrity": "sha512-FeYrfJ05QgEMW/qOukNCr4fAJHww4SaKnivAXRv4g5kj4FeLpNV7zH4dorzB9zAfVX4wmA7zWu/wQf7kkcvfbw==", + "version": "0.30.7", + "resolved": "https://registry.npmjs.org/tsd/-/tsd-0.30.7.tgz", + "integrity": "sha512-oTiJ28D6B/KXoU3ww/Eji+xqHJojiuPVMwA12g4KYX1O72N93Nb6P3P3h2OAhhf92Xl8NIhb/xFmBZd5zw/xUw==", "dev": true, "dependencies": { - "@tsd/typescript": "~5.0.2", + "@tsd/typescript": "~5.3.3", "eslint-formatter-pretty": "^4.1.0", "globby": "^11.0.1", "jest-diff": "^29.0.3", @@ -6749,10 +6752,13 @@ "dev": true }, "node_modules/yaml": { - "version": "2.3.4", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", - "integrity": "sha512-8aAvwVUSHpfEqTQ4w/KMlf3HcRdt50E5ODIQJBw1fQ5RL34xabzxtUlzTXVqc4rkZsPbvrXKWnABCD7kWSmocA==", + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.1.tgz", + "integrity": "sha512-pIXzoImaqmfOrL7teGUBt/T7ZDnyeGBWyXQBvOVhLkWLN37GXv8NMLK406UY6dS51JfcQHsmcW5cJ441bHg6Lg==", "dev": true, + "bin": { + "yaml": "bin.mjs" + }, "engines": { "node": ">= 14" } @@ -7755,9 +7761,9 @@ } }, "@tsd/typescript": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/@tsd/typescript/-/typescript-5.0.4.tgz", - "integrity": "sha512-YQi2lvZSI+xidKeUjlbv6b6Zw7qB3aXHw5oGJLs5OOGAEqKIOvz5UIAkWyg0bJbkSUWPBEtaOHpVxU4EYBO1Jg==", + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/@tsd/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-CQlfzol0ldaU+ftWuG52vH29uRoKboLinLy84wS8TQOu+m+tWoaUfk4svL4ij2V8M5284KymJBlHUusKj6k34w==", "dev": true }, "@types/eslint": { @@ -11360,12 +11366,12 @@ } }, "tsd": { - "version": "0.28.1", - "resolved": "https://registry.npmjs.org/tsd/-/tsd-0.28.1.tgz", - "integrity": "sha512-FeYrfJ05QgEMW/qOukNCr4fAJHww4SaKnivAXRv4g5kj4FeLpNV7zH4dorzB9zAfVX4wmA7zWu/wQf7kkcvfbw==", + "version": "0.30.7", + "resolved": "https://registry.npmjs.org/tsd/-/tsd-0.30.7.tgz", + "integrity": "sha512-oTiJ28D6B/KXoU3ww/Eji+xqHJojiuPVMwA12g4KYX1O72N93Nb6P3P3h2OAhhf92Xl8NIhb/xFmBZd5zw/xUw==", "dev": true, "requires": { - "@tsd/typescript": "~5.0.2", + "@tsd/typescript": "~5.3.3", "eslint-formatter-pretty": "^4.1.0", "globby": "^11.0.1", "jest-diff": "^29.0.3", @@ -11611,9 +11617,9 @@ "dev": true }, "yaml": { - "version": "2.3.4", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", - "integrity": "sha512-8aAvwVUSHpfEqTQ4w/KMlf3HcRdt50E5ODIQJBw1fQ5RL34xabzxtUlzTXVqc4rkZsPbvrXKWnABCD7kWSmocA==", + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.1.tgz", + "integrity": "sha512-pIXzoImaqmfOrL7teGUBt/T7ZDnyeGBWyXQBvOVhLkWLN37GXv8NMLK406UY6dS51JfcQHsmcW5cJ441bHg6Lg==", "dev": true }, "yargs": { diff --git a/package.json b/package.json index d299972c72..8b3c075af8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/core.ts b/src/core.ts index 5d84151200..da1c77edcf 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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 @@ -187,11 +188,15 @@ export class ProcessPromise extends Promise { } 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, @@ -199,6 +204,7 @@ export class ProcessPromise extends Promise { }) this.zurk = exec({ + input, cmd: $.prefix + this._command, cwd: $.cwd ?? $[processCwd], ac: $.ac, diff --git a/test/core.test.js b/test/core.test.js index b04a5b9285..383ac7f331 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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' @@ -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"}'`)