-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
181 lines (162 loc) · 5.84 KB
/
index.ts
File metadata and controls
181 lines (162 loc) · 5.84 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
#!/usr/bin/env node
// Polyfill for Intl.Segmenter to avoid segfaults in pkg builds
// pkg uses small-icu which causes crashes when Intl.Segmenter.segment() is called
// See: https://github.com/yao-pkg/pkg-fetch/issues/134
// Use polyfill-force to always replace the native implementation
import "@formatjs/intl-segmenter/polyfill-force.js";
import * as console from "node:console";
import os from "node:os";
import process from "node:process";
import { Command } from "@commander-js/extra-typings";
import pkg from "../package.json" with { type: "json" };
import { apiClient } from "./apiClient.ts";
import { checkVersion } from "./checkVersion.ts";
import { loadConfig, saveConfig } from "./helpers/config.ts";
import { getAppBanner } from "./lib/app-banner.ts";
import { registerBalance } from "./lib/balance.ts";
import { registerContracts } from "./lib/contracts/index.tsx";
import { registerDev } from "./lib/dev.ts";
import { registerImages } from "./lib/images/index.ts";
import { registerLogin } from "./lib/login.ts";
import { registerMe } from "./lib/me.ts";
import { registerMigrate, showMigrateBanner } from "./lib/migrate.ts";
import { registerNodes } from "./lib/nodes/index.ts";
import { analytics, IS_TRACKING_DISABLED } from "./lib/posthog.ts";
import { registerScale } from "./lib/scale/index.tsx";
import { registerTokens } from "./lib/tokens.ts";
import { registerUpgrade } from "./lib/upgrade.ts";
import { registerVM } from "./lib/vm/index.ts";
import { registerZones } from "./lib/zones.tsx";
async function main() {
const program = new Command();
if (!process.argv.includes("--json")) {
const [shownUpgradeBanner] = await Promise.all([
checkVersion(),
getAppBanner(),
]);
// If the user is already on the latest version of the legacy CLI, nudge
// them toward the new Rust CLI instead of showing nothing. We avoid
// double-stacking with the upgrade banner since users on outdated builds
// need to upgrade before migrating, and skip the banner for the
// `upgrade` / `migrate` commands themselves (where it'd just be noise)
// and for users who've opted out via SF_CLI_DISABLE_MIGRATE_BANNER.
const subcommand = process.argv[2];
if (
!shownUpgradeBanner &&
subcommand !== "migrate" &&
subcommand !== "upgrade" &&
!process.env.SF_CLI_DISABLE_MIGRATE_BANNER
) {
showMigrateBanner();
}
}
program
.name("sf")
.description("The San Francisco Compute command line tool.")
.version(pkg.version);
// Hydrate `account_id` before registering commands so feature-flag-gated
// surfaces (e.g. `--enable-infiniband` on `sf nodes create`) resolve
// correctly on the very first CLI invocation after login, rather than only
// appearing after the cache has been seeded by a previous run.
const config = await loadConfig();
let exchangeAccountId = config.account_id;
if (!exchangeAccountId) {
const client = await apiClient(config.auth_token);
const { data } = await client.GET("/v1/account/me", {});
if (data?.id) {
exchangeAccountId = data.id;
await saveConfig({ ...config, account_id: data.id });
}
}
// commands
registerLogin(program);
registerContracts(program);
registerBalance(program);
registerTokens(program);
registerUpgrade(program);
registerMigrate(program);
await registerScale(program);
registerMe(program);
await registerVM(program);
await registerNodes(program);
registerImages(program);
await registerZones(program);
// (development commands)
registerDev(program);
if (IS_TRACKING_DISABLED) {
await program.parseAsync(process.argv);
} else {
// Add global process exit handlers to ensure analytics cleanup
let isShuttingDown = false;
const ensureAnalyticsShutdown = async () => {
if (!isShuttingDown) {
isShuttingDown = true;
try {
await analytics.shutdown();
} catch (_err) {
// Silently ignore analytics shutdown errors
}
}
};
process.on("beforeExit", ensureAnalyticsShutdown);
process.on("SIGINT", async () => {
await ensureAnalyticsShutdown();
process.exit(130);
});
process.on("SIGTERM", async () => {
await ensureAnalyticsShutdown();
process.exit(0);
});
program.exitOverride((error) => {
let isError = true;
switch (error.code) {
case "commander.helpDisplayed":
case "commander.help":
case "commander.version":
isError = false;
break;
}
process.exit(isError ? 1 : 0);
});
if (exchangeAccountId) {
const args = process.argv.slice(2).reduce((acc, arg, i, arr) => {
if (arg.startsWith("--")) {
const key = arg.slice(2);
const nextArg = arr[i + 1];
if (nextArg && !nextArg.startsWith("-")) {
(acc as Record<string, string | number | boolean>)[key] =
Number.isNaN(Number(nextArg)) ? nextArg : Number(nextArg);
} else {
(acc as Record<string, boolean>)[key] = true;
}
}
return acc;
}, {});
analytics.track({
event: `${process.argv[2] || "unknown"}${
process.argv[3] ? "_" + process.argv[3] : ""
}`,
properties: {
...args,
shell: process.env.SHELL,
os: os.platform(),
cliVersion: program.version(),
argsRaw: process.argv.slice(2).join(" "),
},
});
}
try {
await program.parseAsync(process.argv);
// Add cleanup only when the process would naturally exit but PostHog keeps it alive
// This only triggers when the event loop empties (command is done) but process doesn't exit
process.on("beforeExit", async () => {
await ensureAnalyticsShutdown();
process.exit(0);
});
} catch (err) {
console.log(err);
process.exit(1);
}
}
}
main();