-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcli.ts
More file actions
115 lines (101 loc) · 2.75 KB
/
Copy pathcli.ts
File metadata and controls
115 lines (101 loc) · 2.75 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
104
105
106
107
108
109
110
111
112
113
114
115
import { basename } from "node:path";
import arg from "arg";
import pc from "picocolors";
import { runCli as runAuthCli } from "./commands/auth.ts";
import { runCli as runGhaCli } from "./commands/gha.ts";
import { runCli as runInitCli } from "./commands/init.ts";
const VERSION = process.env.CLI_VERSION ?? "0.0.0";
const bin = basename(process.argv[1] || "");
const PROG = bin === "pf" || bin === "pullfrog" ? bin : "pullfrog";
const rawArgs = process.argv.slice(2);
function printMainUsage(stream: typeof console.log): void {
stream(`usage: ${PROG} <command>\n`);
stream("commands:");
stream(" init set up pullfrog on the current repository");
stream(" auth manage provider credentials for the current repository");
stream("");
stream("global options:");
stream(" -h, --help show help");
stream(" -v, --version show version");
}
function parseGlobalArgs(args: string[]) {
return arg(
{
"--help": Boolean,
"--version": Boolean,
"-h": "--help",
"-v": "--version",
},
{
argv: args,
stopAtPositional: true,
}
);
}
function exitWithUsageError(message: string): never {
console.error(`${message}\n`);
printMainUsage(console.error);
process.exit(1);
}
async function run(): Promise<void> {
let globalParsed: ReturnType<typeof parseGlobalArgs>;
try {
globalParsed = parseGlobalArgs(rawArgs);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
exitWithUsageError(message);
}
if (globalParsed["--version"]) {
console.log(VERSION);
process.exit(0);
}
const command = globalParsed._[0];
const commandArgs = globalParsed._.slice(1);
if (!command) {
if (globalParsed["--help"]) {
console.log(`${pc.bold("pullfrog")} v${VERSION}\n`);
printMainUsage(console.log);
process.exit(0);
}
printMainUsage(console.log);
process.exit(0);
}
if (command === "init") {
await runInitCli({
args: commandArgs,
prog: PROG,
showHelp: globalParsed["--help"] === true,
});
return;
}
if (command === "gha") {
await runGhaCli({
args: commandArgs,
prog: PROG,
showHelp: globalParsed["--help"] === true,
});
return;
}
if (command === "auth") {
await runAuthCli({
args: commandArgs,
prog: PROG,
showHelp: globalParsed["--help"] === true,
});
return;
}
if (globalParsed["--help"]) {
printMainUsage(console.log);
process.exit(0);
}
console.error(`unknown command: ${pc.bold(command)}\n`);
printMainUsage(console.error);
process.exit(1);
}
try {
await run();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(pc.red(message));
process.exit(1);
}