Skip to content

Commit 681b93b

Browse files
authored
feat: add id to log entries (#1057)
1 parent 1edf36d commit 681b93b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/core.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
254254
this._pipedFrom?.run()
255255

256256
const self = this
257-
const $ = this._snapshot
257+
const $ = self._snapshot
258+
const id = self.id
258259
const sync = $[SYNC]
259260
const timeout = self._timeout ?? $.timeout
260261
const timeoutSignal = self._timeoutSignal ?? $.timeoutSignal
@@ -269,12 +270,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
269270
kind: 'cmd',
270271
cmd: self.cmd,
271272
verbose: self.isVerbose(),
273+
id,
272274
})
273275

274276
// prettier-ignore
275277
this._zurk = exec({
276278
sync,
277-
id: self.id,
279+
id,
278280
cmd: self.fullCmd,
279281
cwd: $.cwd ?? $[CWD],
280282
input: ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input,
@@ -297,11 +299,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
297299
stdout: (data) => {
298300
// If process is piped, don't print output.
299301
if (self._piped) return
300-
$.log({ kind: 'stdout', data, verbose: self.isVerbose() })
302+
$.log({ kind: 'stdout', data, verbose: self.isVerbose(), id })
301303
},
302304
stderr: (data) => {
303305
// Stderr should be printed regardless of piping.
304-
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
306+
$.log({ kind: 'stderr', data, verbose: !self.isQuiet(), id })
305307
},
306308
end: (data, c) => {
307309
self._resolved = true

src/util.ts

+2
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ export type LogEntry = {
146146
| {
147147
kind: 'cmd'
148148
cmd: string
149+
id: string
149150
}
150151
| {
151152
kind: 'stdout' | 'stderr'
152153
data: Buffer
154+
id: string
153155
}
154156
| {
155157
kind: 'cd'

test/core.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,28 @@ describe('core', () => {
356356
assert.equal((await fs.readFile(file)).toString(), 'foo\n')
357357
})
358358
})
359+
360+
it('uses custom `log` if specified', async () => {
361+
const entries = []
362+
const log = (entry) => entries.push(entry)
363+
const p = $({ log })`echo foo`
364+
const { id } = p
365+
await p
366+
367+
assert.equal(entries.length, 2)
368+
assert.deepEqual(entries[0], {
369+
kind: 'cmd',
370+
cmd: 'echo foo',
371+
verbose: false,
372+
id,
373+
})
374+
assert.deepEqual(entries[1], {
375+
kind: 'stdout',
376+
data: Buffer.from('foo\n'),
377+
verbose: false,
378+
id,
379+
})
380+
})
359381
})
360382

361383
describe('ProcessPromise', () => {

0 commit comments

Comments
 (0)