Skip to content

Commit 9d28d8f

Browse files
committed
refactor(cli): optimize main() inners
1 parent dc448d4 commit 9d28d8f

File tree

9 files changed

+75
-75
lines changed

9 files changed

+75
-75
lines changed

.size-limit.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
{
2525
"name": "js parts",
2626
"path": "build/*.{js,cjs}",
27-
"limit": "816.30 kB",
27+
"limit": "816.20 kB",
2828
"brotli": false,
2929
"gzip": false
3030
},
@@ -38,14 +38,14 @@
3838
{
3939
"name": "vendor",
4040
"path": "build/vendor-*",
41-
"limit": "769.15 kB",
41+
"limit": "769.10 kB",
4242
"brotli": false,
4343
"gzip": false
4444
},
4545
{
4646
"name": "all",
4747
"path": ["build/*", "man/*", "README.md", "LICENSE"],
48-
"limit": "872.70 kB",
48+
"limit": "872.50 kB",
4949
"brotli": false,
5050
"gzip": false
5151
}

build/cli.cjs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ var import_node_process2 = __toESM(require("process"), 1);
140140
var import_meta = {};
141141
var EXT = ".mjs";
142142
var EXT_RE = /^\.[mc]?[jt]sx?$/;
143+
var argv = (0, import_index.parseArgv)(import_node_process2.default.argv.slice(2), {
144+
default: (0, import_index.resolveDefaults)({ ["prefer-local"]: false }, "ZX_", import_node_process2.default.env, /* @__PURE__ */ new Set(["env", "install", "registry"])),
145+
// exclude 'prefer-local' to let minimist infer the type
146+
string: ["shell", "prefix", "postfix", "eval", "cwd", "ext", "registry", "env"],
147+
boolean: ["version", "help", "quiet", "verbose", "install", "repl", "experimental"],
148+
alias: { e: "eval", i: "install", v: "version", h: "help", l: "prefer-local", "env-file": "env" },
149+
stopEarly: true,
150+
parseBoolean: true,
151+
camelCase: true
152+
});
143153
isMain() && main().catch((err) => {
144154
if (err instanceof import_index.ProcessOutput) {
145155
console.error("Error:", err.message);
@@ -177,21 +187,17 @@ function printUsage() {
177187
${import_index.chalk.italic("Full documentation:")} ${import_index.chalk.underline("https://google.github.io/zx/")}
178188
`);
179189
}
180-
var argv = (0, import_index.parseArgv)(import_node_process2.default.argv.slice(2), {
181-
default: { ["prefer-local"]: false },
182-
// exclude 'prefer-local' to let minimist infer the type
183-
string: ["shell", "prefix", "postfix", "eval", "cwd", "ext", "registry", "env"],
184-
boolean: ["version", "help", "quiet", "verbose", "install", "repl", "experimental"],
185-
alias: { e: "eval", i: "install", v: "version", h: "help", l: "prefer-local", "env-file": "env" },
186-
stopEarly: true,
187-
parseBoolean: true,
188-
camelCase: true
189-
}, (0, import_index.resolveDefaults)({}, "ZX_", import_node_process2.default.env, /* @__PURE__ */ new Set(["env", "install", "registry"])));
190190
function main() {
191191
return __async(this, null, function* () {
192192
var _a2;
193-
yield require("./globals.cjs");
194-
argv.ext = normalizeExt(argv.ext);
193+
if (argv.version) {
194+
console.log(import_index.VERSION);
195+
return;
196+
}
197+
if (argv.help) {
198+
printUsage();
199+
return;
200+
}
195201
if (argv.cwd) import_index.$.cwd = argv.cwd;
196202
if (argv.env) {
197203
const envfile = import_index.path.resolve((_a2 = import_index.$.cwd) != null ? _a2 : import_node_process2.default.cwd(), argv.env);
@@ -204,18 +210,12 @@ function main() {
204210
if (argv.prefix) import_index.$.prefix = argv.prefix;
205211
if (argv.postfix) import_index.$.postfix = argv.postfix;
206212
if (argv.preferLocal) import_index.$.preferLocal = argv.preferLocal;
207-
if (argv.version) {
208-
console.log(import_index.VERSION);
209-
return;
210-
}
211-
if (argv.help) {
212-
printUsage();
213-
return;
214-
}
213+
yield require("./globals.cjs");
215214
if (argv.repl) {
216215
yield startRepl();
217216
return;
218217
}
218+
argv.ext = normalizeExt(argv.ext);
219219
const { script, scriptPath, tempPath } = yield readScript();
220220
yield runScript(script, scriptPath, tempPath);
221221
});

build/cli.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22
import { transformMarkdown } from './md.js';
33
import { type minimist } from './vendor.js';
4-
export declare function printUsage(): void;
54
export declare const argv: minimist.ParsedArgs;
5+
export declare function printUsage(): void;
66
export declare function main(): Promise<void>;
77
export { transformMarkdown };
88
export declare function injectGlobalRequire(origin: string): void;

build/cli.js

100644100755
File mode changed.

build/index.cjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22
const {
33
__pow,
4-
__spreadValues,
54
__export,
65
__reExport,
76
__toESM,
@@ -69,11 +68,11 @@ var parseArgv = (args = import_node_process.default.argv.slice(2), opts = {}, de
6968
m[_k] = _v;
7069
return m;
7170
},
72-
__spreadValues({}, defs)
71+
defs
7372
);
7473
function updateArgv(args, opts) {
7574
for (const k in argv) delete argv[k];
76-
Object.assign(argv, parseArgv(args, opts));
75+
parseArgv(args, opts, argv);
7776
}
7877
var argv = parseArgv();
7978
function sleep(duration) {
@@ -88,7 +87,7 @@ var responseToReadable = (response, rs) => {
8887
rs.push(null);
8988
return rs;
9089
}
91-
rs._read = () => __async(void 0, null, function* () {
90+
rs._read = () => __async(null, null, function* () {
9291
const result = yield reader.read();
9392
if (!result.done) rs.push(import_node_buffer.Buffer.from(result.value));
9493
else rs.push(null);
@@ -238,7 +237,7 @@ function spinner(title, callback) {
238237
let i = 0;
239238
const stream = import_core.$.log.output || import_node_process.default.stderr;
240239
const spin = () => stream.write(` ${"\u280B\u2819\u2839\u2838\u283C\u2834\u2826\u2827\u2807\u280F"[i++ % 10]} ${title}\r`);
241-
return (0, import_core.within)(() => __async(this, null, function* () {
240+
return (0, import_core.within)(() => __async(null, null, function* () {
242241
import_core.$.verbose = false;
243242
const id = setInterval(spin, 100);
244243
try {

build/vendor-core.cjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var require_posix = __commonJS({
1919
exports2.sync = exports2.isexe = void 0;
2020
var fs_1 = require("fs");
2121
var promises_1 = require("fs").promises;
22-
var isexe = (_0, ..._1) => __async(exports2, [_0, ..._1], function* (path, options = {}) {
22+
var isexe = (_0, ..._1) => __async(null, [_0, ..._1], function* (path, options = {}) {
2323
const { ignoreErrors = false } = options;
2424
try {
2525
return checkStat(yield (0, promises_1.stat)(path), options);
@@ -73,7 +73,7 @@ var require_win32 = __commonJS({
7373
exports2.sync = exports2.isexe = void 0;
7474
var fs_1 = require("fs");
7575
var promises_1 = require("fs").promises;
76-
var isexe = (_0, ..._1) => __async(exports2, [_0, ..._1], function* (path, options = {}) {
76+
var isexe = (_0, ..._1) => __async(null, [_0, ..._1], function* (path, options = {}) {
7777
const { ignoreErrors = false } = options;
7878
try {
7979
return checkStat(yield (0, promises_1.stat)(path), path, options);
@@ -208,7 +208,7 @@ var require_lib = __commonJS({
208208
const prefix = !pathPart && rRel.test(cmd) ? cmd.slice(0, 2) : "";
209209
return prefix + join(pathPart, cmd);
210210
};
211-
var which2 = (_0, ..._1) => __async(exports2, [_0, ..._1], function* (cmd, opt = {}) {
211+
var which2 = (_0, ..._1) => __async(null, [_0, ..._1], function* (cmd, opt = {}) {
212212
const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
213213
const found = [];
214214
for (const envPart of pathEnv) {
@@ -1273,7 +1273,7 @@ var _tree = ({
12731273
return Promise.reject(err);
12741274
}
12751275
};
1276-
var tree = (opts, cb) => __async(void 0, null, function* () {
1276+
var tree = (opts, cb) => __async(null, null, function* () {
12771277
return _tree({ opts, cb });
12781278
});
12791279
var treeSync = (opts, cb) => _tree({ opts, cb, sync: true });

build/vendor-extra.cjs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6923,7 +6923,7 @@ var require_make_dir = __commonJS({
69236923
if (typeof options === "number") return options;
69246924
return __spreadValues(__spreadValues({}, defaults), options).mode;
69256925
};
6926-
module2.exports.makeDir = (dir, options) => __async(exports2, null, function* () {
6926+
module2.exports.makeDir = (dir, options) => __async(null, null, function* () {
69276927
checkPath(dir);
69286928
return fs6.mkdir(dir, {
69296929
mode: getMode(options),
@@ -11577,7 +11577,7 @@ var init_node = __esm({
1157711577
const { body: o3 } = i[H];
1157811578
return o3 === null ? 0 : yr(o3) ? o3.size : import_node_buffer2.Buffer.isBuffer(o3) ? o3.length : o3 && typeof o3.getLengthSync == "function" && o3.hasKnownLength && o3.hasKnownLength() ? o3.getLengthSync() : null;
1157911579
}, "getTotalBytes");
11580-
Xs = n2((_0, _1) => __async(void 0, [_0, _1], function* (i, { body: o3 }) {
11580+
Xs = n2((_0, _1) => __async(null, [_0, _1], function* (i, { body: o3 }) {
1158111581
o3 === null ? i.end() : yield Zs(o3, i);
1158211582
}), "writeToStream");
1158311583
gr = typeof import_node_http2.default.validateHeaderName == "function" ? import_node_http2.default.validateHeaderName : (i) => {
@@ -12335,7 +12335,7 @@ _streams = new WeakMap();
1233512335
_ended = new WeakMap();
1233612336
_aborted = new WeakMap();
1233712337
_onFinished = new WeakMap();
12338-
var onMergedStreamFinished = (passThroughStream, streams) => __async(void 0, null, function* () {
12338+
var onMergedStreamFinished = (passThroughStream, streams) => __async(null, null, function* () {
1233912339
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_COUNT);
1234012340
const controller = new AbortController();
1234112341
try {
@@ -12348,10 +12348,10 @@ var onMergedStreamFinished = (passThroughStream, streams) => __async(void 0, nul
1234812348
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_COUNT);
1234912349
}
1235012350
});
12351-
var onMergedStreamEnd = (_0, _1) => __async(void 0, [_0, _1], function* (passThroughStream, { signal }) {
12351+
var onMergedStreamEnd = (_0, _1) => __async(null, [_0, _1], function* (passThroughStream, { signal }) {
1235212352
yield (0, import_promises.finished)(passThroughStream, { signal, cleanup: true });
1235312353
});
12354-
var onInputStreamsUnpipe = (_0, _1, _2) => __async(void 0, [_0, _1, _2], function* (passThroughStream, streams, { signal }) {
12354+
var onInputStreamsUnpipe = (_0, _1, _2) => __async(null, [_0, _1, _2], function* (passThroughStream, streams, { signal }) {
1235512355
try {
1235612356
for (var iter = __forAwait((0, import_node_events.on)(passThroughStream, "unpipe", { signal })), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
1235712357
const [unpipedStream] = temp.value;
@@ -12375,7 +12375,7 @@ var validateStream = (stream) => {
1237512375
throw new TypeError(`Expected a readable stream, got: \`${typeof stream}\`.`);
1237612376
}
1237712377
};
12378-
var endWhenStreamsDone = (_0) => __async(void 0, [_0], function* ({ passThroughStream, stream, streams, ended, aborted, onFinished }) {
12378+
var endWhenStreamsDone = (_0) => __async(null, [_0], function* ({ passThroughStream, stream, streams, ended, aborted, onFinished }) {
1237912379
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_PER_STREAM);
1238012380
const controller = new AbortController();
1238112381
try {
@@ -12397,7 +12397,7 @@ var endWhenStreamsDone = (_0) => __async(void 0, [_0], function* ({ passThroughS
1239712397
}
1239812398
});
1239912399
var isAbortError = (error) => (error == null ? void 0 : error.code) === "ERR_STREAM_PREMATURE_CLOSE";
12400-
var afterMergedStreamFinished = (onFinished, stream) => __async(void 0, null, function* () {
12400+
var afterMergedStreamFinished = (onFinished, stream) => __async(null, null, function* () {
1240112401
try {
1240212402
yield onFinished;
1240312403
abortStream(stream);
@@ -12409,7 +12409,7 @@ var afterMergedStreamFinished = (onFinished, stream) => __async(void 0, null, fu
1240912409
}
1241012410
}
1241112411
});
12412-
var onInputStreamEnd = (_0) => __async(void 0, [_0], function* ({ passThroughStream, stream, streams, ended, aborted, controller: { signal } }) {
12412+
var onInputStreamEnd = (_0) => __async(null, [_0], function* ({ passThroughStream, stream, streams, ended, aborted, controller: { signal } }) {
1241312413
try {
1241412414
yield (0, import_promises.finished)(stream, { signal, cleanup: true, readable: true, writable: false });
1241512415
if (streams.has(stream)) {
@@ -12426,7 +12426,7 @@ var onInputStreamEnd = (_0) => __async(void 0, [_0], function* ({ passThroughStr
1242612426
}
1242712427
}
1242812428
});
12429-
var onInputStreamUnpipe = (_0) => __async(void 0, [_0], function* ({ stream, streams, ended, aborted, controller: { signal } }) {
12429+
var onInputStreamUnpipe = (_0) => __async(null, [_0], function* ({ stream, streams, ended, aborted, controller: { signal } }) {
1243012430
yield (0, import_node_events.once)(stream, unpipeEvent, { signal });
1243112431
streams.delete(stream);
1243212432
ended.delete(stream);
@@ -12578,7 +12578,7 @@ var normalizeOptions = (options = {}) => {
1257812578
ignore: [...(_b2 = options.ignore) != null ? _b2 : [], ...defaultIgnoredDirectories]
1257912579
};
1258012580
};
12581-
var isIgnoredByIgnoreFiles = (patterns, options) => __async(void 0, null, function* () {
12581+
var isIgnoredByIgnoreFiles = (patterns, options) => __async(null, null, function* () {
1258212582
const { cwd, suppressErrors, deep, ignore } = normalizeOptions(options);
1258312583
const paths = yield (0, import_fast_glob.default)(patterns, __spreadValues({
1258412584
cwd,
@@ -12587,7 +12587,7 @@ var isIgnoredByIgnoreFiles = (patterns, options) => __async(void 0, null, functi
1258712587
ignore
1258812588
}, ignoreFilesGlobOptions));
1258912589
const files = yield Promise.all(
12590-
paths.map((filePath) => __async(void 0, null, function* () {
12590+
paths.map((filePath) => __async(null, null, function* () {
1259112591
return {
1259212592
filePath,
1259312593
content: yield import_promises3.default.readFile(filePath, "utf8")
@@ -12627,13 +12627,13 @@ var getDirectoryGlob = ({ directoryPath, files, extensions }) => {
1262712627
const extensionGlob = (extensions == null ? void 0 : extensions.length) > 0 ? `.${extensions.length > 1 ? `{${extensions.join(",")}}` : extensions[0]}` : "";
1262812628
return files ? files.map((file) => import_node_path3.default.posix.join(directoryPath, `**/${import_node_path3.default.extname(file) ? file : `${file}${extensionGlob}`}`)) : [import_node_path3.default.posix.join(directoryPath, `**${extensionGlob ? `/*${extensionGlob}` : ""}`)];
1262912629
};
12630-
var directoryToGlob = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (directoryPaths, {
12630+
var directoryToGlob = (_0, ..._1) => __async(null, [_0, ..._1], function* (directoryPaths, {
1263112631
cwd = import_node_process2.default.cwd(),
1263212632
files,
1263312633
extensions
1263412634
} = {}) {
1263512635
const globs = yield Promise.all(
12636-
directoryPaths.map((directoryPath) => __async(void 0, null, function* () {
12636+
directoryPaths.map((directoryPath) => __async(null, null, function* () {
1263712637
return (yield isDirectory(normalizePathForDirectoryGlob(directoryPath, cwd))) ? getDirectoryGlob({ directoryPath, files, extensions }) : directoryPath;
1263812638
}))
1263912639
);
@@ -12673,7 +12673,7 @@ var normalizeOptions2 = (options = {}) => {
1267312673
checkCwdOption(options.cwd);
1267412674
return options;
1267512675
};
12676-
var normalizeArguments = (function_) => (patterns, options) => __async(void 0, null, function* () {
12676+
var normalizeArguments = (function_) => (patterns, options) => __async(null, null, function* () {
1267712677
return function_(toPatternsArray(patterns), normalizeOptions2(options));
1267812678
});
1267912679
var normalizeArgumentsSync = (function_) => (patterns, options) => function_(toPatternsArray(patterns), normalizeOptions2(options));
@@ -12685,7 +12685,7 @@ var getIgnoreFilesPatterns = (options) => {
1268512685
}
1268612686
return patterns;
1268712687
};
12688-
var getFilter = (options) => __async(void 0, null, function* () {
12688+
var getFilter = (options) => __async(null, null, function* () {
1268912689
const ignoreFilesPatterns = getIgnoreFilesPatterns(options);
1269012690
return createFilterFunction(
1269112691
ignoreFilesPatterns.length > 0 && (yield isIgnoredByIgnoreFiles(ignoreFilesPatterns, options))
@@ -12738,15 +12738,15 @@ var convertNegativePatterns = (patterns, options) => {
1273812738
return tasks;
1273912739
};
1274012740
var normalizeExpandDirectoriesOption = (options, cwd) => __spreadValues(__spreadValues({}, cwd ? { cwd } : {}), Array.isArray(options) ? { files: options } : options);
12741-
var generateTasks = (patterns, options) => __async(void 0, null, function* () {
12741+
var generateTasks = (patterns, options) => __async(null, null, function* () {
1274212742
const globTasks = convertNegativePatterns(patterns, options);
1274312743
const { cwd, expandDirectories } = options;
1274412744
if (!expandDirectories) {
1274512745
return globTasks;
1274612746
}
1274712747
const directoryToGlobOptions = normalizeExpandDirectoriesOption(expandDirectories, cwd);
1274812748
return Promise.all(
12749-
globTasks.map((task) => __async(void 0, null, function* () {
12749+
globTasks.map((task) => __async(null, null, function* () {
1275012750
let { patterns: patterns2, options: options2 } = task;
1275112751
[
1275212752
patterns2,
@@ -12773,7 +12773,7 @@ var generateTasksSync = (patterns, options) => {
1277312773
return { patterns: patterns2, options: options2 };
1277412774
});
1277512775
};
12776-
var globby = normalizeArguments((patterns, options) => __async(void 0, null, function* () {
12776+
var globby = normalizeArguments((patterns, options) => __async(null, null, function* () {
1277712777
const [
1277812778
tasks,
1277912779
filter

0 commit comments

Comments
 (0)