-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.cli.js
More file actions
64 lines (53 loc) · 1.58 KB
/
util.cli.js
File metadata and controls
64 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const waw = require("./util.waw");
module.exports = function runExe(modules) {
const cmdRaw = process.argv[2];
if (!cmdRaw) return false;
const cmd = String(cmdRaw).toLowerCase();
for (let mi = modules.length - 1; mi >= 0; mi--) {
const m = modules[mi];
if (!m || !Array.isArray(m.files)) continue;
for (let fi = m.files.length - 1; fi >= 0; fi--) {
const f = m.files[fi];
if (typeof f !== "string") continue;
// Only match exact filename: cli.js (not *.cli.js)
const normalized = f.replace(/\\/g, "/");
const base = normalized.split("/").pop();
if (base.toLowerCase() !== "cli.js") continue;
const ex = require(f);
if (!ex) continue;
const ctx = {
...waw,
modules,
module: m,
module_root: m.__root,
module_config: m,
};
// case 1: export is object OR function with command keys
if ((typeof ex === "object" || typeof ex === "function") && !Array.isArray(ex)) {
// direct match first
if (typeof ex[cmdRaw] === "function") {
ex[cmdRaw](ctx);
return true;
}
if (typeof ex[cmd] === "function") {
ex[cmd](ctx);
return true;
}
// case-insensitive key scan (covers "--V", etc.)
for (const k of Object.keys(ex)) {
if (k.toLowerCase() !== cmd) continue;
if (typeof ex[k] !== "function") continue;
ex[k](ctx);
return true;
}
}
// case 2: export is a function that decides itself
// module.exports = (cmd, waw) => boolean/void
if (typeof ex === "function") {
const handled = ex(cmdRaw, ctx);
if (handled !== false) return true;
}
}
}
return false;
};