Commit 7737fc6

Anton Golub <antongolub@antongolub.com>
2025-08-02 08:57:13
refactor: simplify `pipe()` declaration (#1304)
1 parent 45a78ff
build/cli.js
build/core.cjs
@@ -762,6 +762,15 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
   isRunning() {
     return this.stage === "running";
   }
+  // Piping
+  // prettier-ignore
+  get pipe() {
+    const getPipeMethod = (kind) => this._pipe.bind(this, kind);
+    const stdout = getPipeMethod("stdout");
+    const stderr = getPipeMethod("stderr");
+    const stdall = getPipeMethod("stdall");
+    return Object.assign(stdout, { stdout, stderr, stdall });
+  }
   unpipe(to) {
     _ProcessPromise.bus.unpipe(this, to);
     return this;
@@ -901,16 +910,6 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
     });
   }
 };
-Object.defineProperty(_ProcessPromise.prototype, "pipe", { get() {
-  const self = this;
-  const getPipeMethod = (kind) => function(dest, ...args) {
-    return self._pipe.call(self, kind, dest, ...args);
-  };
-  const stdout = getPipeMethod("stdout");
-  const stderr = getPipeMethod("stderr");
-  const stdall = getPipeMethod("stdall");
-  return Object.assign(stdout, { stderr, stdout, stdall });
-} });
 // prettier-ignore
 _ProcessPromise.bus = {
   refs: /* @__PURE__ */ new Map(),
build/core.d.ts
@@ -144,7 +144,7 @@ export declare class ProcessPromise extends Promise<ProcessOutput> {
     isHalted(): boolean;
     private isSettled;
     private isRunning;
-    pipe: PipeMethod & {
+    get pipe(): PipeMethod & {
         [key in keyof TSpawnStore]: PipeMethod;
     };
     unpipe(to?: PipeAcceptor): this;
src/core.ts
@@ -590,8 +590,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {
   }
 
   // Piping
-  pipe!: PipeMethod & {
+  // prettier-ignore
+  get pipe(): PipeMethod & {
     [key in keyof TSpawnStore]: PipeMethod
+  } {
+    const getPipeMethod = (kind: keyof TSpawnStore) => this._pipe.bind(this, kind) as PipeMethod
+    const stdout = getPipeMethod('stdout')
+    const stderr = getPipeMethod('stderr')
+    const stdall = getPipeMethod('stdall')
+    return Object.assign(stdout, { stdout, stderr, stdall })
   }
 
   unpipe(to?: PipeAcceptor): this {
@@ -599,17 +606,6 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     return this
   }
 
-  // prettier-ignore
-  static {
-    Object.defineProperty(this.prototype, 'pipe', { get() {
-        const self = this
-        const getPipeMethod = (kind: keyof TSpawnStore): PipeMethod => function (dest: PipeDest, ...args: any[]) { return self._pipe.call(self, kind, dest, ...args) }
-        const stdout = getPipeMethod('stdout')
-        const stderr = getPipeMethod('stderr')
-        const stdall = getPipeMethod('stdall')
-        return Object.assign(stdout, { stderr, stdout, stdall })
-      }})
-  }
   // prettier-ignore
   private _pipe(source: keyof TSpawnStore, dest: PipeDest, ...args: any[]): PromisifiedStream | ProcessPromise {
     if (isString(dest))
test/core.test.js
@@ -973,9 +973,7 @@ describe('core', () => {
         p1.pipe(p3)
         p2.pipe(p3)
 
-        setTimeout(() => {
-          p1.unpipe(p3)
-        }, 150)
+        setTimeout(() => p1.unpipe(p3), 180)
 
         const { stdout } = await p3
         assert.equal(stdout, 'foo\n1\nbar\n2\n3\n')
.size-limit.json
@@ -29,7 +29,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "815.20 kB",
+    "limit": "815.15 kB",
     "brotli": false,
     "gzip": false
   },