Commit 4081a9f

Anton Golub <antongolub@antongolub.com>
2024-09-15 14:28:44
feat: add duration to `ProcessOutput` (#892)
* feat: add duration to `ProcessOutput` * test: check p duration after kill --------- Co-authored-by: Anton Medvedev <anton@medv.io>
1 parent 5d78a26
Changed files (2)
src/core.ts
@@ -319,7 +319,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
           // Stderr should be printed regardless of piping.
           $.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
         },
-        end: ({ error, stdout, stderr, stdall, status, signal }, c) => {
+        end: (
+          { error, stdout, stderr, stdall, status, signal, duration },
+          c
+        ) => {
           self._resolved = true
 
           // Ensures EOL
@@ -336,7 +339,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
               stdout,
               stderr,
               stdall,
-              message
+              message,
+              duration
             )
             self._output = output
             self._reject(output)
@@ -353,7 +357,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
               stdout,
               stderr,
               stdall,
-              message
+              message,
+              duration
             )
             self._output = output
             if (status === 0 || self.isNothrow()) {
@@ -567,6 +572,7 @@ export class ProcessOutput extends Error {
   private readonly _stdout: string
   private readonly _stderr: string
   private readonly _combined: string
+  private readonly _duration: number
 
   constructor(
     code: number | null,
@@ -574,7 +580,8 @@ export class ProcessOutput extends Error {
     stdout: string,
     stderr: string,
     combined: string,
-    message: string
+    message: string,
+    duration: number = 0
   ) {
     super(message)
     this._code = code
@@ -582,6 +589,7 @@ export class ProcessOutput extends Error {
     this._stdout = stdout
     this._stderr = stderr
     this._combined = combined
+    this._duration = duration
   }
 
   toString() {
@@ -634,6 +642,10 @@ export class ProcessOutput extends Error {
     return this._signal
   }
 
+  get duration() {
+    return this._duration
+  }
+
   static getExitMessage(
     code: number | null,
     signal: NodeJS.Signals | null,
@@ -674,7 +686,8 @@ export class ProcessOutput extends Error {
     exitCodeInfo(this.exitCode)
       ? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')')
       : ''
-  }
+  },
+  duration: ${this.duration}
 }`
   }
 }
@@ -698,7 +711,7 @@ export function cd(dir: string | ProcessOutput) {
 }
 
 export async function kill(pid: number, signal = $.killSignal) {
-  let children = await ps.tree({ pid, recursive: true })
+  const children = await ps.tree({ pid, recursive: true })
   for (const p of children) {
     try {
       process.kill(+p.pid, signal)
test/core.test.js
@@ -464,11 +464,13 @@ describe('core', () => {
 
     describe('kill()', () => {
       test('just works', async () => {
-        let p = $`sleep 999`.nothrow()
+        const p = $`sleep 999`.nothrow()
         setTimeout(() => {
           p.kill()
         }, 100)
-        await p
+        const o = await p
+        assert.equal(o.signal, 'SIGTERM')
+        assert.ok(o.duration >= 100 && o.duration < 1000)
       })
 
       test('a signal is passed with kill() method', async () => {
@@ -608,6 +610,16 @@ describe('core', () => {
   })
 
   describe('ProcessOutput', () => {
+    test('getters', async () => {
+      const o = new ProcessOutput(-1, 'SIGTERM', '', '', 'foo\n', 'msg', 20)
+
+      assert.equal(o.stdout, '')
+      assert.equal(o.stderr, '')
+      assert.equal(o.signal, 'SIGTERM')
+      assert.equal(o.exitCode, -1)
+      assert.equal(o.duration, 20)
+    })
+
     test('toString()', async () => {
       const o = new ProcessOutput(null, null, '', '', 'foo\n')
       assert.equal(o.toString(), 'foo\n')