-
Notifications
You must be signed in to change notification settings - Fork 24
/
cli.ts
64 lines (63 loc) · 1.89 KB
/
cli.ts
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
import { bold } from "./deps.ts";
import { loadConfig } from "./src/load_config.ts";
import { normalizeScript } from "./src/normalize_script.ts";
import { runCommands } from "./src/run_commands.ts";
import { resolveShell } from "./src/resolve_shell.ts";
import { printScriptsInfo } from "./src/scripts_info.ts";
import { log } from "./src/logger.ts";
import { didYouMean } from "./src/did_you_mean.ts";
import { handleOption } from "./src/options.ts";
if (import.meta.main) {
const args = Deno.args;
if (args.length > 0 && args[0].startsWith("-")) {
try {
await handleOption(args[0]);
} catch (e) {
log.error(e.message);
Deno.exit(1);
}
Deno.exit();
}
let configData;
try {
configData = loadConfig();
} catch (e) {
log.error(e.message);
Deno.exit(2);
}
const { config, cwd } = configData;
if (!config.scripts || Object.entries(config.scripts).length < 1) {
log.warning(
"No scripts available.\nSee https://github.com/umbopepato/velociraptor for guidance on how to create scripts.",
);
Deno.exit();
}
if (args.length < 1) {
printScriptsInfo(config);
Deno.exit();
}
const scriptName = args[0];
if (!(scriptName in config.scripts)) {
log.error(`Script ${bold(scriptName)} not found`);
const suggestion = didYouMean(scriptName, config.scripts);
if (suggestion) console.log(`Did you mean ${bold(suggestion)}?`);
else {
console.log(
`Run ${
bold("vr")
} without arguments to see a list of available scripts.`,
);
}
Deno.exit();
}
const scriptDef = config.scripts[scriptName];
const { scripts, ...rootConfig } = config;
const commands = normalizeScript(scriptDef, rootConfig);
const shell = resolveShell();
try {
await runCommands(commands, shell, args.slice(1), cwd);
} catch (e) {
log.error(`Failed at the ${bold(scriptName)} script`);
Deno.exit(3);
}
}