Commit 5d193c0

Anton Golub <antongolub@antongolub.com>
2025-01-05 23:03:39
feat: introduce `end` event for logger (#1058)
1 parent 681b93b
src/core.ts
@@ -333,6 +333,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
           if (stdout.length && !stdout[stdout.length - 1]!.toString().endsWith('\n')) c.on.stdout!(EOL, c)
           if (stderr.length && !stderr[stderr.length - 1]!.toString().endsWith('\n')) c.on.stderr!(EOL, c)
 
+          $.log({ kind: 'end', signal, exitCode: status, duration, error, verbose: self.isVerbose(), id })
           const output = self._output = new ProcessOutput(dto)
 
           if (error || status !== 0 && !self.isNothrow()) {
src/util.ts
@@ -153,6 +153,14 @@ export type LogEntry = {
       data: Buffer
       id: string
     }
+  | {
+      kind: 'end'
+      exitCode: number | null
+      signal: NodeJS.Signals | null
+      duration: number
+      error: null | Error
+      id: string
+    }
   | {
       kind: 'cd'
       dir: string
test/core.test.js
@@ -362,9 +362,9 @@ describe('core', () => {
       const log = (entry) => entries.push(entry)
       const p = $({ log })`echo foo`
       const { id } = p
-      await p
+      const { duration } = await p
 
-      assert.equal(entries.length, 2)
+      assert.equal(entries.length, 3)
       assert.deepEqual(entries[0], {
         kind: 'cmd',
         cmd: 'echo foo',
@@ -377,6 +377,15 @@ describe('core', () => {
         verbose: false,
         id,
       })
+      assert.deepEqual(entries[2], {
+        kind: 'end',
+        duration,
+        exitCode: 0,
+        signal: null,
+        error: null,
+        verbose: false,
+        id,
+      })
     })
   })
 
.size-limit.json
@@ -16,7 +16,7 @@
   {
     "name": "dts libdefs",
     "path": "build/*.d.ts",
-    "limit": "37.5 kB",
+    "limit": "38 kB",
     "brotli": false,
     "gzip": false
   },