Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·103 lines (95 loc) · 2.95 KB
/
Copy pathcli.ts
File metadata and controls
executable file
·103 lines (95 loc) · 2.95 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
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
#!/usr/bin/env node
import { type Command, Option, program } from "commander";
import { handleBuild } from "./commands/build.ts";
import { handleTest } from "./commands/test.ts";
import {
CONTAINER_RUNTIMES,
type ContainerRuntime,
DEFAULT_CONTAINER_RUNTIME,
resolveContainerRuntime,
} from "./constants.ts";
import { parseFlagToBoolean } from "./utils.ts";
function addContainerRuntimeOption(cmd: Command) {
cmd.addOption(
new Option("--runtime <runtime>", "container runtime to use for build/test")
.choices([...CONTAINER_RUNTIMES])
.default(process.env.HACK_CONTAINER_RUNTIME ?? DEFAULT_CONTAINER_RUNTIME),
);
}
/**
* Adds test options to a command and returns a function to transform the options
* to match what handleTest expects.
*/
function addTestOptions(cmd: Command) {
cmd
.option(
"--frozen-lock [value]",
"if it should use the existing commit lazy lock file",
false,
)
.option(
"-r, --remote",
"use the remote dotfiles from github default branch",
false,
)
.option("-s, --skip-cargo [value]", "weahter to skip cargo install", false)
.option("-n, --nightly", "use nightly for bleeding edge neovim")
.allowUnknownOption();
}
function transformContainerRuntimeOption(opts: {
runtime?: string;
}): ContainerRuntime {
return resolveContainerRuntime(opts.runtime);
}
/**
* Transforms commander options to match handleTest's expected format
*/
function transformTestOptions(opts: {
frozenLock?: boolean | string;
remote?: boolean;
skipCargo?: boolean | string;
nightly?: boolean;
runtime?: string;
}) {
return {
nightly: Boolean(opts.nightly),
frozenLock: parseFlagToBoolean(opts.frozenLock),
remote: Boolean(opts.remote),
skipCargo: parseFlagToBoolean(opts.skipCargo),
runtime: transformContainerRuntimeOption(opts),
};
}
const buildCommand = program
.command("build")
.description("build hacksore/nvim container test image")
.allowUnknownOption();
addContainerRuntimeOption(buildCommand);
buildCommand.action((_opts, command) => {
return handleBuild({
runtime: transformContainerRuntimeOption(command.optsWithGlobals()),
});
});
const testCommand = program
.command("test")
.description("Run the hacksore/nvim container test image");
addContainerRuntimeOption(testCommand);
addTestOptions(testCommand);
testCommand.action((_opts, command) => {
return handleTest(transformTestOptions(command.optsWithGlobals()));
});
program
.name("hack")
.description(
"🤓 CLI to manage various things for building my neovim config\nBy default it will run build and test for convenience",
)
.option("-v, --version", "output the current version")
.allowExcessArguments()
.allowUnknownOption();
addContainerRuntimeOption(program);
addTestOptions(program);
program.action(async (opts) => {
const runtime = transformContainerRuntimeOption(opts);
await handleBuild({ runtime });
await handleTest(transformTestOptions(opts));
});
program.parse();