Commit 90a4f24

Anton Golub <antongolub@antongolub.com>
2024-12-25 20:05:56
feat: expose ProcessPromise `fullCmd` and unique `id` (#1035)
* feat: expose ProcessPromise `fullCmd` and unique `id` relates #1028 * chore: size limit
1 parent 775b4b4
src/core.ts
@@ -56,6 +56,7 @@ import {
   quote,
   quotePowerShell,
   toCamelCase,
+  randomId,
 } from './util.js'
 
 export { log, type LogEntry } from './util.js'
@@ -209,6 +210,7 @@ type PipeMethod = {
 }
 
 export class ProcessPromise extends Promise<ProcessOutput> {
+  private _id = randomId()
   private _command = ''
   private _from = ''
   private _snapshot = getStore()
@@ -251,9 +253,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     this._run = true
     this._pipedFrom?.run()
 
-    const $ = this._snapshot
     const self = this
-    const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input
+    const $ = this._snapshot
 
     if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
     if ($.preferLocal) {
@@ -268,22 +269,24 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       verbose: self.isVerbose(),
     })
 
+    // prettier-ignore
     this._zurk = exec({
-      input,
-      cmd: $.prefix + self._command + $.postfix,
-      cwd: $.cwd ?? $[CWD],
-      ac: $.ac,
-      signal: $.signal,
-      shell: isString($.shell) ? $.shell : true,
-      env: $.env,
-      spawn: $.spawn,
-      spawnSync: $.spawnSync,
-      store: $.store,
-      stdin: self._stdin,
-      stdio: self._stdio ?? $.stdio,
-      sync: $[SYNC],
+      id:       self.id,
+      cmd:      self.fullCmd,
+      cwd:      $.cwd ?? $[CWD],
+      input:    ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input,
+      ac:       $.ac,
+      signal:   $.signal,
+      shell:    isString($.shell) ? $.shell : true,
+      env:      $.env,
+      spawn:    $.spawn,
+      spawnSync:$.spawnSync,
+      store:    $.store,
+      stdin:    self._stdin,
+      stdio:    self._stdio ?? $.stdio,
+      sync:     $[SYNC],
       detached: $.detached,
-      ee: self._ee,
+      ee:       self._ee,
       run: (cb) => cb(),
       on: {
         start: () => {
@@ -298,13 +301,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
           // Stderr should be printed regardless of piping.
           $.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
         },
-        // prettier-ignore
         end: (data, c) => {
           self._resolved = true
           const { error, status, signal, duration, ctx } = data
           const { stdout, stderr, stdall } = ctx.store
           const dto: ProcessOutputLazyDto = {
-            // Lazy getters
             code: () => status,
             signal: () => signal,
             duration: () => duration,
@@ -439,6 +440,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
   }
 
   // Getters
+  get id() {
+    return this._id
+  }
+
   get pid(): number | undefined {
     return this.child?.pid
   }
@@ -447,6 +452,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     return this._command
   }
 
+  get fullCmd(): string {
+    return this._snapshot.prefix + this.cmd + this._snapshot.postfix
+  }
+
   get child(): ChildProcess | undefined {
     return this._zurk?.child
   }
test/core.test.js
@@ -392,11 +392,13 @@ describe('core', () => {
       const baz = 1
       const p = $`echo ${foo} --t ${baz}`
       assert.equal(p.cmd, "echo $'#bar' --t 1")
+      assert.equal(p.fullCmd, "set -euo pipefail;echo $'#bar' --t 1")
     })
 
-    test('exposes pid', () => {
+    test('exposes pid & id', () => {
       const p = $`echo foo`
       assert.ok(p.pid > 0)
+      assert.ok(typeof p.id === 'string')
     })
 
     test('stdio() works', async () => {
.size-limit.json
@@ -16,7 +16,7 @@
   {
     "name": "dts libdefs",
     "path": "build/*.d.ts",
-    "limit": "37.1 kB",
+    "limit": "37.5 kB",
     "brotli": false,
     "gzip": false
   },