Commit 97b36cc

Anton Golub <antongolub@antongolub.com>
2025-07-20 09:46:31
refactor: rm redundant internal vars (#1278)
* refactor: rm redundant internal vars * refactor: optimize `ProcessPromise[Symbol.asyncIterator]` finalization
1 parent 553d5e2
build/core.cjs
@@ -438,9 +438,8 @@ var $ = new Proxy(
     const snapshot = getStore();
     if (!Array.isArray(pieces)) {
       return function(...args2) {
-        const self = this;
         return within(
-          () => Object.assign($, snapshot, pieces).apply(self, args2)
+          () => Object.assign($, snapshot, pieces).apply(this, args2)
         );
       };
     }
@@ -542,13 +541,15 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
       ee: self._ee,
       run(cb, ctx) {
         var _a2, _b2;
-        ((_b2 = (_a2 = self.cmd).then) == null ? void 0 : _b2.call(_a2, (_cmd) => {
-          self._command = _cmd;
-          ctx.cmd = self.fullCmd;
-          cb();
-        }, (error) => {
-          ctx.on.end({ error, status: null, signal: null, duration: 0, ctx }, ctx);
-        })) || cb();
+        ((_b2 = (_a2 = self.cmd).then) == null ? void 0 : _b2.call(
+          _a2,
+          (_cmd) => {
+            self._command = _cmd;
+            ctx.cmd = self.fullCmd;
+            cb();
+          },
+          (error) => ctx.on.end({ error, status: null, signal: null, duration: 0, ctx }, ctx)
+        )) || cb();
       },
       on: {
         start: () => {
@@ -576,12 +577,12 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
           $2.log({ kind: "end", signal, exitCode: status, duration, error, verbose: self.isVerbose(), id });
           if (stdout.length && (0, import_util.getLast)((0, import_util.getLast)(stdout)) !== BR_CC) c.on.stdout(EOL, c);
           if (stderr.length && (0, import_util.getLast)((0, import_util.getLast)(stderr)) !== BR_CC) c.on.stderr(EOL, c);
-          if (!output.ok && !self.isNothrow()) {
-            self._stage = "rejected";
-            self._reject(output);
-          } else {
+          if (output.ok || self.isNothrow()) {
             self._stage = "fulfilled";
             self._resolve(output);
+          } else {
+            self._stage = "rejected";
+            self._reject(output);
           }
         }
       }
@@ -808,8 +809,7 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
         }
       }
       if (memo[0]) yield memo[0];
-      if (this.isNothrow()) return;
-      if ((yield new __await(this.exitCode)) !== 0) throw this._output;
+      yield new __await(this);
     });
   }
   emit(event, ...args) {
build/util.cjs
@@ -82,11 +82,7 @@ function parseDuration(d) {
 var once = (fn) => {
   let called = false;
   let result;
-  return (...args) => {
-    if (called) return result;
-    called = true;
-    return result = fn(...args);
-  };
+  return (...args) => called ? result : (called = true, result = fn(...args));
 };
 var proxyOverride = (origin, ...fallbacks) => new Proxy(origin, {
   get(target, key) {
src/core.ts
@@ -174,9 +174,8 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
     const snapshot = getStore()
     if (!Array.isArray(pieces)) {
       return function (this: any, ...args: any) {
-        const self = this
         return within(() =>
-          Object.assign($, snapshot, pieces).apply(self, args)
+          Object.assign($, snapshot, pieces).apply(this, args)
         )
       }
     }
@@ -310,13 +309,14 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       detached: $.detached,
       ee:       self._ee,
       run(cb, ctx){
-        (self.cmd as unknown as Promise<string>).then?.(_cmd => {
-          self._command = _cmd
-          ctx.cmd = self.fullCmd
-          cb()
-        }, error => {
-          ctx.on.end!({error, status: null, signal: null, duration: 0, ctx} as TSpawnResult, ctx)
-        }) || cb()
+        (self.cmd as unknown as Promise<string>).then?.(
+          _cmd => {
+            self._command = _cmd
+            ctx.cmd = self.fullCmd
+            cb()
+          },
+          error => ctx.on.end!({ error, status: null, signal: null, duration: 0, ctx } as TSpawnResult, ctx)
+        ) || cb()
       },
       on: {
         start: () => {
@@ -350,12 +350,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
           if (stdout.length && getLast(getLast(stdout)) !== BR_CC) c.on.stdout!(EOL, c)
           if (stderr.length && getLast(getLast(stderr)) !== BR_CC) c.on.stderr!(EOL, c)
 
-          if (!output.ok && !self.isNothrow()) {
-            self._stage = 'rejected'
-            self._reject(output)
-          } else {
+          if (output.ok || self.isNothrow()) {
             self._stage = 'fulfilled'
             self._resolve(output)
+          } else {
+            self._stage = 'rejected'
+            self._reject(output)
           }
         },
       },
@@ -660,9 +660,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
 
     if (memo[0]) yield memo[0]
 
-    if (this.isNothrow()) return
-
-    if ((await this.exitCode) !== 0) throw this._output
+    await this
   }
 
   // Stream-like API
src/util.ts
@@ -119,11 +119,9 @@ export function parseDuration(d: Duration) {
 export const once = <T extends (...args: any[]) => any>(fn: T) => {
   let called = false
   let result: ReturnType<T>
-  return (...args: Parameters<T>): ReturnType<T> => {
-    if (called) return result
-    called = true
-    return (result = fn(...args))
-  }
+
+  return (...args: Parameters<T>): ReturnType<T> =>
+    called ? result : ((called = true), (result = fn(...args)))
 }
 
 export const proxyOverride = <T extends object>(
test/util.test.js
@@ -17,6 +17,8 @@ import { test, describe } from 'node:test'
 import {
   isString,
   isStringLiteral,
+  identity,
+  once,
   noop,
   parseDuration,
   parseBool,
@@ -41,6 +43,14 @@ describe('util', () => {
     assert.ok(noop() === undefined)
   })
 
+  test('once()', () => {
+    const fn = once(identity)
+    assert.equal(identity(1), 1)
+    assert.equal(identity(2), 2)
+    assert.equal(fn(1), 1)
+    assert.equal(fn(2), 1)
+  })
+
   test('isString()', () => {
     assert.ok(isString('string'))
     assert.ok(!isString(1))
.size-limit.json
@@ -15,7 +15,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "122.05 kB",
+    "limit": "121.95 kB",
     "brotli": false,
     "gzip": false
   },
@@ -29,7 +29,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "812.80 kB",
+    "limit": "812.70 kB",
     "brotli": false,
     "gzip": false
   },
@@ -62,7 +62,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "868.90 kB",
+    "limit": "868.80 kB",
     "brotli": false,
     "gzip": false
   }