Skip to content

Commit e3f043a

Browse files
authored
refactor: simplify ProcessPromise._snapshot inners (#1281)
* chore: bump version to v8.7.2 * refactor: separate snapshot builder * refactor: simplify `ProcessPromise.timeout()` inners * chore: internal types imprs * refactor: make public `ProcessPromise.sync` getter
1 parent 3ca9cd8 commit e3f043a

11 files changed

Lines changed: 265 additions & 270 deletions

File tree

.size-limit.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"README.md",
1616
"LICENSE"
1717
],
18-
"limit": "121.801 kB",
18+
"limit": "121.20 kB",
1919
"brotli": false,
2020
"gzip": false
2121
},
@@ -29,14 +29,14 @@
2929
"build/globals.js",
3030
"build/deno.js"
3131
],
32-
"limit": "812.60 kB",
32+
"limit": "812.20 kB",
3333
"brotli": false,
3434
"gzip": false
3535
},
3636
{
3737
"name": "libdefs",
3838
"path": "build/*.d.ts",
39-
"limit": "39.20 kB",
39+
"limit": "39.00 kB",
4040
"brotli": false,
4141
"gzip": false
4242
},
@@ -62,7 +62,7 @@
6262
"README.md",
6363
"LICENSE"
6464
],
65-
"limit": "868.65 kB",
65+
"limit": "868.002 kB",
6666
"brotli": false,
6767
"gzip": false
6868
}

build/cli.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function readScript() {
294294
}
295295
function readScriptFromStdin() {
296296
return __async(this, null, function* () {
297-
return !import_node_process2.default.stdin.isTTY ? (0, import_index.stdin)() : "";
297+
return import_node_process2.default.stdin.isTTY ? "" : (0, import_index.stdin)();
298298
});
299299
}
300300
function readScriptFromHttp(remote) {

build/cli.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env node
2-
import { transformMarkdown } from './md.js';
32
import { type minimist } from './vendor.js';
3+
export { transformMarkdown } from './md.js';
44
export declare const argv: minimist.ParsedArgs;
55
export declare function printUsage(): void;
66
export declare function main(): Promise<void>;
7-
export { transformMarkdown };
87
export declare function injectGlobalRequire(origin: string): void;
98
export declare function isMain(metaurl?: string, scriptpath?: string): boolean;
109
export declare function normalizeExt(ext?: string): string | undefined;

build/core.cjs

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
const {
33
__spreadValues,
4+
__spreadProps,
45
__export,
56
__toESM,
67
__toCommonJS,
@@ -432,8 +433,14 @@ var defaults = resolveDefaults({
432433
killSignal: SIGTERM,
433434
timeoutSignal: SIGTERM
434435
});
435-
var boundCtxs = [];
436+
var snapshots = [];
436437
var delimiters = [];
438+
var getSnapshot = (snapshot, from, cmd) => __spreadProps(__spreadValues({}, snapshot), {
439+
ac: snapshot.ac || new AbortController(),
440+
ee: new import_node_events.EventEmitter(),
441+
from,
442+
cmd
443+
});
437444
var $ = new Proxy(
438445
function(pieces, ...args) {
439446
const snapshot = getStore();
@@ -445,7 +452,7 @@ var $ = new Proxy(
445452
};
446453
}
447454
const from = getCallerLocation();
448-
if (pieces.some((p) => p == void 0))
455+
if (pieces.some((p) => p == null))
449456
throw new Error(`Malformed command at ${from}`);
450457
checkShell();
451458
checkQuote();
@@ -454,10 +461,10 @@ var $ = new Proxy(
454461
pieces,
455462
args
456463
);
457-
boundCtxs.push([cmd, from, snapshot]);
458-
const process3 = new ProcessPromise(import_util.noop);
459-
if (!process3.isHalted()) process3.run();
460-
return process3.output || process3;
464+
snapshots.push(getSnapshot(snapshot, from, cmd));
465+
const pp = new ProcessPromise(import_util.noop);
466+
if (!pp.isHalted()) pp.run();
467+
return pp.sync ? pp.output : pp;
461468
},
462469
{
463470
set(_, key, value) {
@@ -483,68 +490,58 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
483490
});
484491
this._stage = "initial";
485492
this._id = (0, import_util.randomId)();
486-
this._cmd = "";
487-
this._from = "";
488-
this._snapshot = getStore();
489493
this._piped = false;
490-
this._ee = new import_node_events.EventEmitter();
491-
this._ac = new AbortController();
492494
this._stdin = new import_vendor_core2.VoidStream();
493495
this._zurk = null;
494496
this._output = null;
495497
this._reject = import_util.noop;
496498
this._resolve = import_util.noop;
497499
// Stream-like API
498500
this.writable = true;
499-
if (boundCtxs.length) {
500-
const [cmd, from, snapshot] = boundCtxs.pop();
501-
this._cmd = cmd;
502-
this._from = from;
503-
this._snapshot = __spreadValues({}, snapshot);
501+
if (snapshots.length) {
502+
this._snapshot = snapshots.pop();
504503
this._resolve = resolve;
505504
this._reject = (v) => {
506505
reject(v);
507-
if (this.isSync()) throw v;
506+
if (this.sync) throw v;
508507
};
509-
if (snapshot.halt) this._stage = "halted";
508+
if (this._snapshot.halt) this._stage = "halted";
510509
} else _ProcessPromise.disarm(this);
511510
}
512511
run() {
513-
var _a, _b, _c, _d, _e, _f, _g;
512+
var _a, _b, _c, _d;
514513
if (this.isRunning() || this.isSettled()) return this;
515514
this._stage = "running";
516515
(_a = this._pipedFrom) == null ? void 0 : _a.run();
517516
const self = this;
518517
const $2 = self._snapshot;
519518
const id = self.id;
520-
const timeout = (_b = self._timeout) != null ? _b : $2.timeout;
521-
const timeoutSignal = (_c = self._timeoutSignal) != null ? _c : $2.timeoutSignal;
522519
if ($2.preferLocal) {
523520
const dirs = $2.preferLocal === true ? [$2.cwd, $2[CWD]] : [$2.preferLocal].flat();
524521
$2.env = (0, import_util.preferLocalBin)($2.env, ...dirs);
525522
}
526523
this._zurk = (0, import_vendor_core2.exec)({
527-
id,
528-
sync: self.isSync(),
529524
cmd: self.fullCmd,
530-
cwd: (_d = $2.cwd) != null ? _d : $2[CWD],
531-
input: (_f = (_e = $2.input) == null ? void 0 : _e.stdout) != null ? _f : $2.input,
525+
cwd: (_b = $2.cwd) != null ? _b : $2[CWD],
526+
input: (_d = (_c = $2.input) == null ? void 0 : _c.stdout) != null ? _d : $2.input,
527+
stdin: self._stdin,
528+
sync: self.sync,
532529
signal: self.signal,
533530
shell: (0, import_util.isString)($2.shell) ? $2.shell : true,
531+
id,
534532
env: $2.env,
535533
spawn: $2.spawn,
536534
spawnSync: $2.spawnSync,
537535
store: $2.store,
538-
stdin: self._stdin,
539-
stdio: (_g = self._stdio) != null ? _g : $2.stdio,
536+
stdio: $2.stdio,
540537
detached: $2.detached,
541-
ee: self._ee,
538+
ee: $2.ee,
542539
run(cb, ctx) {
543540
var _a2, _b2;
544541
((_b2 = (_a2 = self.cmd).then) == null ? void 0 : _b2.call(
545542
_a2,
546-
(_cmd) => {
547-
self._cmd = _cmd;
543+
(cmd) => {
544+
$2.cmd = cmd;
548545
ctx.cmd = self.fullCmd;
549546
cb();
550547
},
@@ -554,7 +551,7 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
554551
on: {
555552
start: () => {
556553
$2.log({ kind: "cmd", cmd: self.cmd, verbose: self.isVerbose(), id });
557-
self.timeout(timeout, timeoutSignal);
554+
self.timeout($2.timeout, $2.timeoutSignal);
558555
},
559556
stdout: (data) => {
560557
if (self._piped) return;
@@ -572,7 +569,7 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
572569
error,
573570
duration,
574571
store,
575-
from: self._from
572+
from: $2.from
576573
});
577574
$2.log({ kind: "end", signal, exitCode: status, duration, error, verbose: self.isVerbose(), id });
578575
if (stdout.length && (0, import_util.getLast)((0, import_util.getLast)(stdout)) !== BR_CC) c.on.stdout(EOL, c);
@@ -598,7 +595,7 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
598595
})(dest, ...args)
599596
);
600597
this._piped = true;
601-
const ee = this._ee;
598+
const { ee } = this._snapshot;
602599
const from = new import_vendor_core2.VoidStream();
603600
const fill = () => {
604601
for (const chunk of this._zurk.store[source]) from.write(chunk);
@@ -661,10 +658,11 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
661658
return (_a = this.child) == null ? void 0 : _a.pid;
662659
}
663660
get cmd() {
664-
return this._cmd;
661+
return this._snapshot.cmd;
665662
}
666663
get fullCmd() {
667-
return (this._snapshot.prefix || "") + this.cmd + (this._snapshot.postfix || "");
664+
const { prefix = "", postfix = "", cmd } = this._snapshot;
665+
return prefix + cmd + postfix;
668666
}
669667
get child() {
670668
var _a;
@@ -692,14 +690,17 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
692690
return this._snapshot.signal || this.ac.signal;
693691
}
694692
get ac() {
695-
return this._snapshot.ac || this._ac;
693+
return this._snapshot.ac;
696694
}
697695
get output() {
698696
return this._output;
699697
}
700698
get stage() {
701699
return this._stage;
702700
}
701+
get sync() {
702+
return this._snapshot[SYNC];
703+
}
703704
get [Symbol.toStringTag]() {
704705
return "ProcessPromise";
705706
}
@@ -708,31 +709,29 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
708709
}
709710
// Configurators
710711
stdio(stdin, stdout = "pipe", stderr = "pipe") {
711-
this._stdio = [stdin, stdout, stderr];
712+
this._snapshot.stdio = [stdin, stdout, stderr];
712713
return this;
713714
}
714715
nothrow(v = true) {
715-
this._nothrow = v;
716+
this._snapshot.nothrow = v;
716717
return this;
717718
}
718719
quiet(v = true) {
719-
this._quiet = v;
720+
this._snapshot.quiet = v;
720721
return this;
721722
}
722723
verbose(v = true) {
723-
this._verbose = v;
724+
this._snapshot.verbose = v;
724725
return this;
725726
}
726-
timeout(d = 0, signal = this._timeoutSignal || $.timeoutSignal) {
727+
timeout(d = 0, signal = $.timeoutSignal) {
727728
if (this.isSettled()) return this;
728-
this._timeout = (0, import_util.parseDuration)(d);
729-
this._timeoutSignal = signal;
729+
const $2 = this._snapshot;
730+
$2.timeout = (0, import_util.parseDuration)(d);
731+
$2.timeoutSignal = signal;
730732
if (this._timeoutId) clearTimeout(this._timeoutId);
731-
if (this._timeout && this.isRunning()) {
732-
this._timeoutId = setTimeout(
733-
() => this.kill(this._timeoutSignal),
734-
this._timeout
735-
);
733+
if ($2.timeout && this.isRunning()) {
734+
this._timeoutId = setTimeout(() => this.kill($2.timeoutSignal), $2.timeout);
736735
this.finally(() => clearTimeout(this._timeoutId)).catch(import_util.noop);
737736
}
738737
return this;
@@ -755,22 +754,16 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
755754
}
756755
// Status checkers
757756
isQuiet() {
758-
var _a;
759-
return (_a = this._quiet) != null ? _a : this._snapshot.quiet;
757+
return this._snapshot.quiet;
760758
}
761759
isVerbose() {
762-
var _a;
763-
return ((_a = this._verbose) != null ? _a : this._snapshot.verbose) && !this.isQuiet();
760+
return this._snapshot.verbose && !this.isQuiet();
764761
}
765762
isNothrow() {
766-
var _a;
767-
return (_a = this._nothrow) != null ? _a : this._snapshot.nothrow;
763+
return this._snapshot.nothrow;
768764
}
769765
isHalted() {
770-
return this.stage === "halted" && !this.isSync();
771-
}
772-
isSync() {
773-
return this._snapshot[SYNC];
766+
return this.stage === "halted" && !this.sync;
774767
}
775768
isSettled() {
776769
return !!this.output;

build/core.d.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,10 @@ type PipeMethod = {
7171
export declare class ProcessPromise extends Promise<ProcessOutput> {
7272
private _stage;
7373
private _id;
74-
private _cmd;
75-
private _from;
7674
private _snapshot;
77-
private _stdio?;
78-
private _nothrow?;
79-
private _quiet?;
80-
private _verbose?;
81-
private _timeout?;
82-
private _timeoutSignal?;
8375
private _timeoutId?;
8476
private _piped;
8577
private _pipedFrom?;
86-
private _ee;
87-
private _ac;
8878
private _stdin;
8979
private _zurk;
9080
private _output;
@@ -115,6 +105,7 @@ export declare class ProcessPromise extends Promise<ProcessOutput> {
115105
get ac(): AbortController;
116106
get output(): ProcessOutput | null;
117107
get stage(): ProcessStage;
108+
get sync(): boolean;
118109
get [Symbol.toStringTag](): string;
119110
[Symbol.toPrimitive](): string;
120111
stdio(stdin: IOType, stdout?: IOType, stderr?: IOType): ProcessPromise;
@@ -124,14 +115,13 @@ export declare class ProcessPromise extends Promise<ProcessOutput> {
124115
timeout(d?: Duration, signal?: NodeJS.Signals | undefined): ProcessPromise;
125116
json<T = any>(): Promise<T>;
126117
text(encoding?: Encoding): Promise<string>;
127-
lines(delimiter?: string | RegExp): Promise<string[]>;
118+
lines(delimiter?: Options['delimiter']): Promise<string[]>;
128119
buffer(): Promise<Buffer>;
129120
blob(type?: string): Promise<Blob>;
130121
isQuiet(): boolean;
131122
isVerbose(): boolean;
132123
isNothrow(): boolean;
133124
isHalted(): boolean;
134-
private isSync;
135125
private isSettled;
136126
private isRunning;
137127
then<R = ProcessOutput, E = ProcessOutput>(onfulfilled?: ((value: ProcessOutput) => PromiseLike<R> | R) | undefined | null, onrejected?: ((reason: ProcessOutput) => PromiseLike<E> | E) | undefined | null): Promise<R | E>;

0 commit comments

Comments
 (0)