From 03f3fc199becf1341387b6c573b069eb10e0c360 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 25 Dec 2024 21:28:32 +0300 Subject: [PATCH] feat: expose ProcessPromise `fullCmd` and unique `id` relates #1028 --- src/core.ts | 45 +++++++++++++++++++++++++++------------------ test/core.test.js | 4 +++- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/core.ts b/src/core.ts index 50c87a76c5..fad5c96008 100644 --- a/src/core.ts +++ b/src/core.ts @@ -56,6 +56,7 @@ import { quote, quotePowerShell, toCamelCase, + randomId, } from './util.js' export { log, type LogEntry } from './util.js' @@ -209,6 +210,7 @@ type PipeMethod = { } export class ProcessPromise extends Promise { + private _id = randomId() private _command = '' private _from = '' private _snapshot = getStore() @@ -251,9 +253,8 @@ export class ProcessPromise extends Promise { this._run = true this._pipedFrom?.run() - const $ = this._snapshot const self = this - const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input + const $ = this._snapshot if ($.timeout) this.timeout($.timeout, $.timeoutSignal) if ($.preferLocal) { @@ -268,22 +269,24 @@ export class ProcessPromise extends Promise { verbose: self.isVerbose(), }) + // prettier-ignore this._zurk = exec({ - input, - cmd: $.prefix + self._command + $.postfix, - cwd: $.cwd ?? $[CWD], - ac: $.ac, - signal: $.signal, - shell: isString($.shell) ? $.shell : true, - env: $.env, - spawn: $.spawn, - spawnSync: $.spawnSync, - store: $.store, - stdin: self._stdin, - stdio: self._stdio ?? $.stdio, - sync: $[SYNC], + id: self.id, + cmd: self.fullCmd, + cwd: $.cwd ?? $[CWD], + input: ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input, + ac: $.ac, + signal: $.signal, + shell: isString($.shell) ? $.shell : true, + env: $.env, + spawn: $.spawn, + spawnSync:$.spawnSync, + store: $.store, + stdin: self._stdin, + stdio: self._stdio ?? $.stdio, + sync: $[SYNC], detached: $.detached, - ee: self._ee, + ee: self._ee, run: (cb) => cb(), on: { start: () => { @@ -298,13 +301,11 @@ export class ProcessPromise extends Promise { // Stderr should be printed regardless of piping. $.log({ kind: 'stderr', data, verbose: !self.isQuiet() }) }, - // prettier-ignore end: (data, c) => { self._resolved = true const { error, status, signal, duration, ctx } = data const { stdout, stderr, stdall } = ctx.store const dto: ProcessOutputLazyDto = { - // Lazy getters code: () => status, signal: () => signal, duration: () => duration, @@ -439,6 +440,10 @@ export class ProcessPromise extends Promise { } // Getters + get id() { + return this._id + } + get pid(): number | undefined { return this.child?.pid } @@ -447,6 +452,10 @@ export class ProcessPromise extends Promise { return this._command } + get fullCmd(): string { + return this._snapshot.prefix + this.cmd + this._snapshot.postfix + } + get child(): ChildProcess | undefined { return this._zurk?.child } diff --git a/test/core.test.js b/test/core.test.js index b0eb9e7254..02feb5828a 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -392,11 +392,13 @@ describe('core', () => { const baz = 1 const p = $`echo ${foo} --t ${baz}` assert.equal(p.cmd, "echo $'#bar' --t 1") + assert.equal(p.fullCmd, "set -euo pipefail;echo $'#bar' --t 1") }) - test('exposes pid', () => { + test('exposes pid & id', () => { const p = $`echo foo` assert.ok(p.pid > 0) + assert.ok(typeof p.id === 'string') }) test('stdio() works', async () => {