Commit 681b93b

Anton Golub <antongolub@antongolub.com>
2025-01-05 22:35:34
feat: add `id` to log entries (#1057)
1 parent 1edf36d
Changed files (3)
src/core.ts
@@ -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
@@ -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,
@@ -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
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'
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', () => {