-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathcli.ts
More file actions
157 lines (140 loc) · 4.6 KB
/
Copy pathcli.ts
File metadata and controls
157 lines (140 loc) · 4.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { basename } from "node:path";
import arg from "arg";
import pc from "picocolors";
import { runCli as runAuthCli } from "./commands/auth.ts";
import { runCli as runConfigCli } from "./commands/config.ts";
import { runCli as runConsoleCli } from "./commands/console.ts";
import { runCli as runGhaCli } from "./commands/gha.ts";
import { runCli as runInitCli } from "./commands/init.ts";
import { runCli as runMcpCli } from "./commands/mcp.ts";
import { runCli as runSecretCli } from "./commands/secret.ts";
import { runCli as runWatchCli } from "./commands/watch.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(" config inspect and change repository or organization settings");
stream(" console open the repository or organization console");
stream(" secret list secret names, save or delete scoped credentials");
stream(" init install pullfrog on the current repository and open its dashboard");
stream(" auth manage provider credentials for the current repository");
stream(" watch stream a PR's activity as one JSON line per event");
stream(" mcp run a stdio MCP server exposing PR activity as an agent tool");
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 === "config" || command === "console" || command === "secret") {
const runCommand =
command === "config" ? runConfigCli : command === "secret" ? runSecretCli : runConsoleCli;
await runCommand({ 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 (command === "mcp") {
await runMcpCli({
args: commandArgs,
prog: PROG,
showHelp: globalParsed["--help"] === true,
});
return;
}
if (command === "watch") {
await runWatchCli({
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();
// exit explicitly rather than waiting for the event loop to drain. a leaked
// handle — a self-daemonized `pullfrog_shell` descendant is the one we've
// actually seen — otherwise strands a finished job for hours. safe here and
// nowhere earlier: `run()` has fully resolved, so `main()`'s `finally` (the
// end-of-run PATCH, artifact persistence, status checks) has already
// completed, and @actions/core writes its outputs synchronously. see #1087.
process.exit(process.exitCode ?? 0);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(pc.red(message));
process.exit(1);
}