Skip to content

Commit

Permalink
feat: add id to log entries (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Jan 5, 2025
1 parent 1edf36d commit 681b93b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
this._pipedFrom?.run()

const self = this
const $ = this._snapshot
const $ = self._snapshot
const id = self.id
const sync = $[SYNC]
const timeout = self._timeout ?? $.timeout
const timeoutSignal = self._timeoutSignal ?? $.timeoutSignal
Expand All @@ -269,12 +270,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
kind: 'cmd',
cmd: self.cmd,
verbose: self.isVerbose(),
id,
})

// prettier-ignore
this._zurk = exec({
sync,
id: self.id,
id,
cmd: self.fullCmd,
cwd: $.cwd ?? $[CWD],
input: ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input,
Expand All @@ -297,11 +299,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
stdout: (data) => {
// If process is piped, don't print output.
if (self._piped) return
$.log({ kind: 'stdout', data, verbose: self.isVerbose() })
$.log({ kind: 'stdout', data, verbose: self.isVerbose(), id })
},
stderr: (data) => {
// Stderr should be printed regardless of piping.
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
$.log({ kind: 'stderr', data, verbose: !self.isQuiet(), id })
},
end: (data, c) => {
self._resolved = true
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ export type LogEntry = {
| {
kind: 'cmd'
cmd: string
id: string
}
| {
kind: 'stdout' | 'stderr'
data: Buffer
id: string
}
| {
kind: 'cd'
Expand Down
22 changes: 22 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ describe('core', () => {
assert.equal((await fs.readFile(file)).toString(), 'foo\n')
})
})

it('uses custom `log` if specified', async () => {
const entries = []
const log = (entry) => entries.push(entry)
const p = $({ log })`echo foo`
const { id } = p
await p

assert.equal(entries.length, 2)
assert.deepEqual(entries[0], {
kind: 'cmd',
cmd: 'echo foo',
verbose: false,
id,
})
assert.deepEqual(entries[1], {
kind: 'stdout',
data: Buffer.from('foo\n'),
verbose: false,
id,
})
})
})

describe('ProcessPromise', () => {
Expand Down

0 comments on commit 681b93b

Please sign in to comment.