Commit 56172ba

Anton Golub <antongolub@antongolub.com>
2025-07-15 20:12:41
chore: bytes shrinking (#1272)
1 parent d3774b2
build/cli.cjs
@@ -32,20 +32,17 @@ var import_index = require("./index.cjs");
 var import_deps = require("./deps.cjs");
 
 // src/repl.ts
-var import_node_os = __toESM(require("os"), 1);
-var import_node_path = __toESM(require("path"), 1);
 var import_node_process = __toESM(require("process"), 1);
 var import_node_repl = __toESM(require("repl"), 1);
 var import_node_util = require("util");
 var import_core = require("./core.cjs");
-var import_vendor_core = require("./vendor-core.cjs");
 var _a;
-var HISTORY = (_a = import_node_process.default.env.ZX_REPL_HISTORY) != null ? _a : import_node_path.default.join(import_node_os.default.homedir(), ".zx_repl_history");
+var HISTORY = (_a = import_node_process.default.env.ZX_REPL_HISTORY) != null ? _a : import_core.path.join(import_core.os.homedir(), ".zx_repl_history");
 function startRepl() {
   return __async(this, arguments, function* (history = HISTORY) {
     import_core.defaults.verbose = false;
     const r = import_node_repl.default.start({
-      prompt: import_vendor_core.chalk.greenBright.bold("\u276F "),
+      prompt: import_core.chalk.greenBright.bold("\u276F "),
       useGlobal: true,
       preview: false,
       writer(output) {
@@ -220,12 +217,13 @@ function main() {
     yield runScript(script, scriptPath, tempPath);
   });
 }
+var rmrf = (p) => p && import_index.fs.rmSync(p, { force: true, recursive: true });
 function runScript(script, scriptPath, tempPath) {
   return __async(this, null, function* () {
     let nmLink = "";
     const rmTemp = () => {
-      import_index.fs.rmSync(tempPath, { force: true, recursive: true });
-      nmLink && import_index.fs.rmSync(nmLink, { force: true, recursive: true });
+      rmrf(tempPath);
+      rmrf(nmLink);
     };
     try {
       if (tempPath) {
build/cli.js
build/core.cjs
@@ -500,13 +500,13 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
       const [cmd, from, snapshot] = boundCtxs.pop();
       this._command = cmd;
       this._from = from;
+      this._snapshot = __spreadValues({ ac: new AbortController() }, snapshot);
       this._resolve = resolve;
       this._reject = (v) => {
         reject(v);
         if (snapshot[SYNC]) throw v;
       };
-      this._snapshot = __spreadValues({ ac: new AbortController() }, snapshot);
-      if (this._snapshot.halt) this._stage = "halted";
+      if (snapshot.halt) this._stage = "halted";
     } else _ProcessPromise.disarm(this);
   }
   run() {
@@ -897,12 +897,6 @@ var _ProcessOutput = class _ProcessOutput extends Error {
   get ok() {
     return !this._dto.error && this.exitCode === 0;
   }
-  [Symbol.toPrimitive]() {
-    return this.valueOf();
-  }
-  toString() {
-    return this.stdall;
-  }
   json() {
     return JSON.parse(this.stdall);
   }
@@ -923,9 +917,15 @@ var _ProcessOutput = class _ProcessOutput extends Error {
     delimiters.push(delimiter);
     return [...this];
   }
+  toString() {
+    return this.stdall;
+  }
   valueOf() {
     return this.stdall.trim();
   }
+  [Symbol.toPrimitive]() {
+    return this.valueOf();
+  }
   *[Symbol.iterator]() {
     const memo = [];
     const dlmtr = delimiters.pop() || this._dto.delimiter || $.delimiter || DLMTR;
build/core.d.ts
@@ -166,14 +166,14 @@ export declare class ProcessOutput extends Error {
     get duration(): number;
     get [Symbol.toStringTag](): string;
     get ok(): boolean;
-    [Symbol.toPrimitive](): string;
-    toString(): string;
     json<T = any>(): T;
     buffer(): Buffer;
     blob(type?: string): Blob;
     text(encoding?: Encoding): string;
     lines(delimiter?: string | RegExp): string[];
+    toString(): string;
     valueOf(): string;
+    [Symbol.toPrimitive](): string;
     [Symbol.iterator](): Iterator<string>;
     [inspect.custom](): string;
     static getExitMessage: (code: number | null, signal: NodeJS.Signals | null, stderr: string, from: string, details?: string) => string;
build/globals.d.ts
@@ -20,6 +20,7 @@ declare global {
     var minimist: typeof _.minimist;
     var nothrow: typeof _.nothrow;
     var os: typeof _.os;
+    var parseArgv: typeof _.parseArgv;
     var path: typeof _.path;
     var ps: typeof _.ps;
     var question: typeof _.question;
build/index.cjs
@@ -54,21 +54,18 @@ var import_node_buffer = require("buffer");
 var import_node_process = __toESM(require("process"), 1);
 var import_node_readline = require("readline");
 var import_node_stream = require("stream");
-var import_node_fs = __toESM(require("fs"), 1);
-var import_node_path = __toESM(require("path"), 1);
-var import_node_os = __toESM(require("os"), 1);
 var import_core = require("./core.cjs");
 var import_util = require("./util.cjs");
 var import_vendor = require("./vendor.cjs");
 function tempdir(prefix = `zx-${(0, import_util.randomId)()}`, mode) {
-  const dirpath = import_node_path.default.join(import_node_os.default.tmpdir(), prefix);
-  import_node_fs.default.mkdirSync(dirpath, { recursive: true, mode });
+  const dirpath = import_core.path.join(import_core.os.tmpdir(), prefix);
+  import_vendor.fs.mkdirSync(dirpath, { recursive: true, mode });
   return dirpath;
 }
 function tempfile(name, data, mode) {
-  const filepath = name ? import_node_path.default.join(tempdir(), name) : import_node_path.default.join(import_node_os.default.tmpdir(), `zx-${(0, import_util.randomId)()}`);
-  if (data === void 0) import_node_fs.default.closeSync(import_node_fs.default.openSync(filepath, "w", mode));
-  else import_node_fs.default.writeFileSync(filepath, data, { mode });
+  const filepath = name ? import_core.path.join(tempdir(), name) : import_core.path.join(import_core.os.tmpdir(), `zx-${(0, import_util.randomId)()}`);
+  if (data === void 0) import_vendor.fs.closeSync(import_vendor.fs.openSync(filepath, "w", mode));
+  else import_vendor.fs.writeFileSync(filepath, data, { mode });
   return filepath;
 }
 var parseArgv = (args = import_node_process.default.argv.slice(2), opts = {}, defs = {}) => Object.entries((0, import_vendor.minimist)(args, opts)).reduce(
@@ -100,8 +97,7 @@ var responseToReadable = (response, rs) => {
   }
   rs._read = () => __async(null, null, function* () {
     const result = yield reader.read();
-    if (!result.done) rs.push(import_node_buffer.Buffer.from(result.value));
-    else rs.push(null);
+    rs.push(result.done ? null : import_node_buffer.Buffer.from(result.value));
   });
   return rs;
 };
@@ -142,13 +138,10 @@ function question(_0) {
     input = import_node_process.default.stdin,
     output = import_node_process.default.stdout
   } = {}) {
-    let completer = void 0;
-    if (Array.isArray(choices)) {
-      completer = function completer2(line) {
-        const hits = choices.filter((c) => c.startsWith(line));
-        return [hits.length ? hits : choices, line];
-      };
-    }
+    const completer = Array.isArray(choices) ? (line) => {
+      const hits = choices.filter((c) => c.startsWith(line));
+      return [hits.length ? hits : choices, line];
+    } : void 0;
     const rl = (0, import_node_readline.createInterface)({
       input,
       output,
src/cli.ts
@@ -125,6 +125,7 @@ export async function main(): Promise<void> {
   await runScript(script, scriptPath, tempPath)
 }
 
+const rmrf = (p: string) => p && fs.rmSync(p, { force: true, recursive: true })
 async function runScript(
   script: string,
   scriptPath: string,
@@ -132,8 +133,8 @@ async function runScript(
 ): Promise<void> {
   let nmLink = ''
   const rmTemp = () => {
-    fs.rmSync(tempPath, { force: true, recursive: true })
-    nmLink && fs.rmSync(nmLink, { force: true, recursive: true })
+    rmrf(tempPath)
+    rmrf(nmLink)
   }
   try {
     if (tempPath) {
src/core.ts
@@ -264,13 +264,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       const [cmd, from, snapshot] = boundCtxs.pop()!
       this._command = cmd
       this._from = from
+      this._snapshot = { ac: new AbortController(), ...snapshot }
       this._resolve = resolve!
       this._reject = (v: ProcessOutput) => {
         reject!(v)
         if (snapshot[SYNC]) throw v
       }
-      this._snapshot = { ac: new AbortController(), ...snapshot }
-      if (this._snapshot.halt) this._stage = 'halted'
+      if (snapshot.halt) this._stage = 'halted'
     } else ProcessPromise.disarm(this)
   }
 
@@ -783,14 +783,6 @@ export class ProcessOutput extends Error {
     return !this._dto.error && this.exitCode === 0
   }
 
-  [Symbol.toPrimitive](): string {
-    return this.valueOf()
-  }
-
-  override toString(): string {
-    return this.stdall
-  }
-
   json<T = any>(): T {
     return JSON.parse(this.stdall)
   }
@@ -818,10 +810,18 @@ export class ProcessOutput extends Error {
     return [...this]
   }
 
+  override toString(): string {
+    return this.stdall
+  }
+
   override valueOf(): string {
     return this.stdall.trim()
   }
 
+  [Symbol.toPrimitive](): string {
+    return this.valueOf()
+  }
+
   *[Symbol.iterator](): Iterator<string> {
     const memo: (string | undefined)[] = []
     const dlmtr =
src/globals.ts
@@ -37,6 +37,7 @@ declare global {
   var minimist: typeof _.minimist
   var nothrow: typeof _.nothrow
   var os: typeof _.os
+  var parseArgv: typeof _.parseArgv
   var path: typeof _.path
   var ps: typeof _.ps
   var question: typeof _.question
src/goods.ts
@@ -16,10 +16,15 @@ import { Buffer } from 'node:buffer'
 import process from 'node:process'
 import { createInterface } from 'node:readline'
 import { Readable } from 'node:stream'
-import fs, { type Mode } from 'node:fs'
-import path from 'node:path'
-import os from 'node:os'
-import { $, within, ProcessOutput, type ProcessPromise } from './core.ts'
+import { type Mode } from 'node:fs'
+import {
+  $,
+  within,
+  ProcessOutput,
+  type ProcessPromise,
+  path,
+  os,
+} from './core.ts'
 import {
   type Duration,
   getLast,
@@ -35,6 +40,7 @@ import {
   type RequestInit,
   nodeFetch,
   minimist,
+  fs,
 } from './vendor.ts'
 
 export function tempdir(
@@ -103,8 +109,7 @@ const responseToReadable = (response: Response, rs: Readable) => {
   }
   rs._read = async () => {
     const result = await reader.read()
-    if (!result.done) rs.push(Buffer.from(result.value))
-    else rs.push(null)
+    rs.push(result.done ? null : Buffer.from(result.value))
   }
   return rs
 }
@@ -164,14 +169,13 @@ export async function question(
     output?: NodeJS.WriteStream
   } = {}
 ): Promise<string> {
-  let completer = undefined
-  if (Array.isArray(choices)) {
-    /* c8 ignore next 5 */
-    completer = function completer(line: string) {
-      const hits = choices.filter((c) => c.startsWith(line))
-      return [hits.length ? hits : choices, line]
-    }
-  }
+  /* c8 ignore next 5 */
+  const completer = Array.isArray(choices)
+    ? (line: string) => {
+        const hits = choices.filter((c) => c.startsWith(line))
+        return [hits.length ? hits : choices, line]
+      }
+    : undefined
   const rl = createInterface({
     input,
     output,
src/repl.ts
@@ -12,13 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import os from 'node:os'
-import path from 'node:path'
 import process from 'node:process'
 import repl from 'node:repl'
 import { inspect } from 'node:util'
-import { ProcessOutput, defaults } from './core.ts'
-import { chalk } from './vendor-core.ts'
+import { ProcessOutput, defaults, chalk, path, os } from './core.ts'
 
 const HISTORY =
   process.env.ZX_REPL_HISTORY ?? path.join(os.homedir(), '.zx_repl_history')
.size-limit.json
@@ -15,7 +15,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "123.15 kB",
+    "limit": "122.10 kB",
     "brotli": false,
     "gzip": false
   },
@@ -29,7 +29,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "813.45 kB",
+    "limit": "812.95 kB",
     "brotli": false,
     "gzip": false
   },
@@ -62,7 +62,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "869.40 kB",
+    "limit": "868.95 kB",
     "brotli": false,
     "gzip": false
   }