-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·138 lines (110 loc) · 3.64 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#! /usr/bin/env node
import { createInterpreter, registerGlobals } from './lib/interpreter.js';
import { parseOptsToString } from './lib/parse-options.js';
import{ $ } from 'zx';
import { logo } from './logo.js';
import stringify from 'code-stringify';
import { PerformanceObserver, performance } from 'node:perf_hooks';
const CONFIG = JSON.parse(process.env.CONFIG);
registerGlobals(CONFIG);
$.verbose = false;
const { stdout: dirname } = await $`pwd`;
const __dirname = dirname.trim();
_z.program.configureOutput({
writeOut: str => {
console.log(chalk.green(str));
},
writeErr: str => {
console.error(chalk.red(`${str}`));
},
});
_z.program.addHelpText('beforeAll', logo);
const populateSubcommands = (parentNode, paths, optsCode) => {
const currentPath = paths[0];
const nextPath = paths.slice(1);
const nodeName = currentPath.endsWith('.js') ? optsCode.name : currentPath;
const currentNode = parentNode.subcommands.find(n => n.name === nodeName) || {
name: nodeName,
subcommands: [],
};
if (nextPath.length) {
return {
...parentNode,
subcommands: [
...parentNode.subcommands.filter(s => s.name !== currentPath),
populateSubcommands(currentNode, paths.slice(1), optsCode),
],
};
}
const optsSubs = optsCode?.subcommands || [];
if (currentPath !== 'index.js') {
parentNode.subcommands.push({
...currentNode,
...optsCode,
subcommands: [...currentNode.subcommands, ...optsSubs],
});
return parentNode;
}
return {
...parentNode,
...optsCode,
subcommands: [...parentNode.subcommands, ...optsSubs],
};
};
const CACHE_FILE = `${__dirname}/.zli.cache.js`;
async function getParsedOpts() {
if (fs.existsSync(CACHE_FILE) && process.argv[2] !== '--write-cache') {
const cache = await import(`${CACHE_FILE}`);
return cache.default;
} else {
const commandStrs = await globby([`${CONFIG.root}/**.js`, `${CONFIG.root}/**/*.js`, `!${CONFIG.root}/**/node_modules`]);
return commandStrs.reduce(
(a, c) => {
const cmdStartIdx = CONFIG.root.endsWith('/') ? CONFIG.root.split('/').length - 1 : CONFIG.root.split('/').length;
const parts = c.split('/').slice(cmdStartIdx);
const script = _z.resolve(__dirname, c);
const opts = parseOptsToString(script);
try {
const asCode = new Function(`const subs = ${opts}; return subs`)();
if (!_z.hasValues(asCode)) {
return a;
}
return populateSubcommands(a, parts, asCode);
} catch (e) {
const error = new Error(`Opts parsing failed on ${script} with error:
${e.message}`);
console.error(error);
console.error(`
Opts that failed parsing:
${opts}
`);
process.exit(1);
}
},
{ subcommands: [] }
);
}
}
const showNames = (node, parentName = '') => {
if (node.name) console.info(parentName + ' > ' + node.name);
if (node.options)
console.info(
'---->',
node.options.map(o => [o.name[0], o.name])
);
if (node.subcommands) {
node.subcommands.map(s => showNames(s, node.name));
}
};
const parsedOpts = await getParsedOpts();
if (process.argv[2] === '--show-arg-name-map') {
showNames(parsedOpts);
process.exit(0);
}
if (process.argv[2] === '--write-cache') {
fs.writeFileSync(CACHE_FILE, `export default ${stringify(parsedOpts)};`, { flag: 'w' });
process.exit(0);
}
const interpreter = createInterpreter(__dirname + '/node_modules/@venicemusic/zli/index.js', process.argv, parsedOpts);
interpreter.preprocess();
await interpreter.execute();