Skip to content

Commit 03f3fc1

Browse files
committed
feat: expose ProcessPromise fullCmd and unique id
relates #1028
1 parent 775b4b4 commit 03f3fc1

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/core.ts

+27-18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
quote,
5757
quotePowerShell,
5858
toCamelCase,
59+
randomId,
5960
} from './util.js'
6061

6162
export { log, type LogEntry } from './util.js'
@@ -209,6 +210,7 @@ type PipeMethod = {
209210
}
210211

211212
export class ProcessPromise extends Promise<ProcessOutput> {
213+
private _id = randomId()
212214
private _command = ''
213215
private _from = ''
214216
private _snapshot = getStore()
@@ -251,9 +253,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
251253
this._run = true
252254
this._pipedFrom?.run()
253255

254-
const $ = this._snapshot
255256
const self = this
256-
const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input
257+
const $ = this._snapshot
257258

258259
if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
259260
if ($.preferLocal) {
@@ -268,22 +269,24 @@ export class ProcessPromise extends Promise<ProcessOutput> {
268269
verbose: self.isVerbose(),
269270
})
270271

272+
// prettier-ignore
271273
this._zurk = exec({
272-
input,
273-
cmd: $.prefix + self._command + $.postfix,
274-
cwd: $.cwd ?? $[CWD],
275-
ac: $.ac,
276-
signal: $.signal,
277-
shell: isString($.shell) ? $.shell : true,
278-
env: $.env,
279-
spawn: $.spawn,
280-
spawnSync: $.spawnSync,
281-
store: $.store,
282-
stdin: self._stdin,
283-
stdio: self._stdio ?? $.stdio,
284-
sync: $[SYNC],
274+
id: self.id,
275+
cmd: self.fullCmd,
276+
cwd: $.cwd ?? $[CWD],
277+
input: ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input,
278+
ac: $.ac,
279+
signal: $.signal,
280+
shell: isString($.shell) ? $.shell : true,
281+
env: $.env,
282+
spawn: $.spawn,
283+
spawnSync:$.spawnSync,
284+
store: $.store,
285+
stdin: self._stdin,
286+
stdio: self._stdio ?? $.stdio,
287+
sync: $[SYNC],
285288
detached: $.detached,
286-
ee: self._ee,
289+
ee: self._ee,
287290
run: (cb) => cb(),
288291
on: {
289292
start: () => {
@@ -298,13 +301,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
298301
// Stderr should be printed regardless of piping.
299302
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
300303
},
301-
// prettier-ignore
302304
end: (data, c) => {
303305
self._resolved = true
304306
const { error, status, signal, duration, ctx } = data
305307
const { stdout, stderr, stdall } = ctx.store
306308
const dto: ProcessOutputLazyDto = {
307-
// Lazy getters
308309
code: () => status,
309310
signal: () => signal,
310311
duration: () => duration,
@@ -439,6 +440,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
439440
}
440441

441442
// Getters
443+
get id() {
444+
return this._id
445+
}
446+
442447
get pid(): number | undefined {
443448
return this.child?.pid
444449
}
@@ -447,6 +452,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
447452
return this._command
448453
}
449454

455+
get fullCmd(): string {
456+
return this._snapshot.prefix + this.cmd + this._snapshot.postfix
457+
}
458+
450459
get child(): ChildProcess | undefined {
451460
return this._zurk?.child
452461
}

test/core.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,13 @@ describe('core', () => {
392392
const baz = 1
393393
const p = $`echo ${foo} --t ${baz}`
394394
assert.equal(p.cmd, "echo $'#bar' --t 1")
395+
assert.equal(p.fullCmd, "set -euo pipefail;echo $'#bar' --t 1")
395396
})
396397

397-
test('exposes pid', () => {
398+
test('exposes pid & id', () => {
398399
const p = $`echo foo`
399400
assert.ok(p.pid > 0)
401+
assert.ok(typeof p.id === 'string')
400402
})
401403

402404
test('stdio() works', async () => {

0 commit comments

Comments
 (0)