From 681b93bc5271edcd1bb33b3b53c675753e0d94e1 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 6 Jan 2025 00:35:34 +0300 Subject: [PATCH] feat: add `id` to log entries (#1057) --- src/core.ts | 10 ++++++---- src/util.ts | 2 ++ test/core.test.js | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core.ts b/src/core.ts index 7876dabce6..04aec7443c 100644 --- a/src/core.ts +++ b/src/core.ts @@ -254,7 +254,8 @@ export class ProcessPromise extends Promise { 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 @@ -269,12 +270,13 @@ export class ProcessPromise extends Promise { 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, @@ -297,11 +299,11 @@ export class ProcessPromise extends Promise { 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 diff --git a/src/util.ts b/src/util.ts index 3d8ecdf24e..390a5b826c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -146,10 +146,12 @@ export type LogEntry = { | { kind: 'cmd' cmd: string + id: string } | { kind: 'stdout' | 'stderr' data: Buffer + id: string } | { kind: 'cd' diff --git a/test/core.test.js b/test/core.test.js index 8c4d7cc49b..63bcf82d44 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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', () => {