-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·201 lines (171 loc) · 5.15 KB
/
Copy pathindex.ts
File metadata and controls
executable file
·201 lines (171 loc) · 5.15 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
import { runCLI, type InitOptions } from "./commands/init.js";
import { extractFlag, hasHelpFlag } from "./core/args.js";
import { runCheck } from "./commands/check.js";
import { printCommandHelp, printHelp } from "./commands/help.js";
import { COMMANDS } from "./commands/helpTopics.js";
import pkg from "../package.json" with { type: "json" };
import { runVerify } from "./commands/verify.js";
import { runProfile } from "./commands/profile.js";
import { runLogin } from "./commands/login.js";
import { runWhoami } from "./commands/whoami.js";
import { runLogout } from "./commands/logout.js";
import { runSessions } from "./commands/sessions.js";
import { runConfig } from "./commands/config.js";
import { runUsers } from "./commands/users.js";
import { runOrg } from "./commands/org.js";
import { runApps } from "./commands/apps.js";
import { runTemplates } from "./commands/templates.js";
import { isCancelled } from "./core/cancel.js";
import kleur from "kleur";
export const VERSION = pkg.version;
const args = process.argv.slice(2);
const command = args[0];
async function main() {
if (!command) {
printHelp();
return;
}
if (command === "-h" || command === "--help") {
printHelp();
return;
}
if (command === "-v" || command === "--version") {
console.log(VERSION);
return;
}
// `seamless help [command]` is the spelled-out form of `--help`.
if (command === "help") {
const topic = args[1];
if (!topic) {
printHelp();
return;
}
if (printCommandHelp(topic)) return;
unknownCommand(topic);
return;
}
// Every command answers -h / --help itself, ahead of its own arg parsing.
if (COMMANDS.includes(command) && hasHelpFlag(args.slice(1))) {
printCommandHelp(command);
return;
}
if (command === "init") {
await runCLI(...parseInitArgs(args.slice(1)));
return;
}
if (command === "templates") {
await runTemplates(args.slice(1));
return;
}
if (command === "check") {
await runCheck();
return;
}
if (command === "verify") {
await runVerify(args.slice(1));
return;
}
if (command === "profile") {
await runProfile(args.slice(1));
return;
}
if (command === "login") {
await runLogin(args.slice(1));
return;
}
if (command === "whoami") {
await runWhoami(args.slice(1));
return;
}
if (command === "logout") {
await runLogout(args.slice(1));
return;
}
if (command === "sessions") {
await runSessions(args.slice(1));
return;
}
if (command === "config") {
await runConfig(args.slice(1));
return;
}
if (command === "users") {
await runUsers(args.slice(1));
return;
}
if (command === "org") {
await runOrg(args.slice(1));
return;
}
if (command === "apps") {
await runApps(args.slice(1));
return;
}
unknownCommand(command);
}
// Flags init handles itself rather than passing through as a template flag.
// Anything else starting with `--` is a template id or alias, which is what
// keeps adding a template to the registry from needing code here.
const INIT_SWITCHES = new Set(["--local", "--yes", "-y", "--force"]);
// init's arguments, as the runCLI(projectName, aliases, opts) triple. Only
// splits them up: which values are valid is settled in init.ts, against the
// registry, before anything is written.
export function parseInitArgs(
args: string[],
): [string | undefined, string[], InitOptions] {
const valued = ["profile", "app", "web", "api", "email", "auth", "admin"];
const values: Record<string, string | undefined> = {};
let rest = args;
for (const name of valued) {
const extracted = extractFlag(rest, name);
values[name] = extracted.value;
rest = extracted.rest;
}
const aliases = rest
.filter((a) => a.startsWith("--") && !INIT_SWITCHES.has(a))
.map((a) => a.replace(/^--+/, ""));
const projectName = rest.find((a) => !a.startsWith("-"));
return [
projectName,
aliases,
{
profileFlag: values.profile,
appId: values.app,
web: values.web,
api: values.api,
email: values.email,
auth: values.auth,
admin: values.admin,
local: rest.includes("--local"),
yes: rest.includes("--yes") || rest.includes("-y"),
force: rest.includes("--force"),
},
];
}
// An unrecognized command used to be treated as a project name and scaffolded,
// which made every typo create a directory with no indication the command was
// not understood. Scaffolding is `init` and nothing else.
function unknownCommand(name: string) {
console.error(kleur.red(`Unknown command "${name}".`));
if (!name.startsWith("-")) {
console.error(
kleur.dim("To scaffold a project, run: ") +
kleur.cyan(`seamless init ${name}`),
);
}
console.error(kleur.dim(`Commands: ${COMMANDS.join(", ")}`));
console.error(
kleur.dim("Run seamless --help, or seamless <command> --help, for details."),
);
process.exit(1);
}
main().catch((err) => {
if (isCancelled(err)) {
console.log(err.message);
// 130 is the conventional exit status for a command ended by Ctrl-C.
process.exit(130);
}
console.error("Error:", err.message);
process.exit(1);
});