-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
62 lines (51 loc) · 1.24 KB
/
index.ts
File metadata and controls
62 lines (51 loc) · 1.24 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
import { parseArgs } from "node:util";
type CLICommand = "help" | "build";
type Flags = { watch: boolean };
interface Args {
values: Flags;
positionals: string[];
}
async function printHelp() {
console.log(`
usts build - build a userscript
usts build --watch - build userscript in watch mode
`);
}
function isSupportedCommand<T extends CLICommand>(
supportedCommands: T[],
cmd: string,
): cmd is T {
return new Set<string>(supportedCommands).has(cmd);
}
function resolveCommand(parsedArgs: Args): CLICommand {
const cmd = parsedArgs.positionals[2];
if (!cmd) {
return "help";
}
if (isSupportedCommand(["build"], cmd)) {
return cmd;
}
return "help";
}
async function runCommand(cmd: CLICommand, flags: Flags) {
switch (cmd) {
case "help": {
await printHelp();
return;
}
case "build": {
const { build } = await import("./build/index.js");
await build({ watch: flags.watch });
return;
}
}
}
export async function cli(argv: string[]): Promise<void> {
const parsedArgs = parseArgs({
args: argv,
allowPositionals: true,
options: { watch: { type: "boolean", default: false } },
});
const cmd = resolveCommand(parsedArgs);
await runCommand(cmd, parsedArgs.values);
}