-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
156 lines (136 loc) · 4.67 KB
/
index.ts
File metadata and controls
156 lines (136 loc) · 4.67 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
#!/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 { registerLogin } from "./lib/login.ts";
import { registerMe } from "./lib/me.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")) {
await Promise.all([checkVersion(), getAppBanner()]);
}
program
.name("sf")
.description("The San Francisco Compute command line tool.")
.version(pkg.version);
// commands
registerLogin(program);
registerContracts(program);
registerBalance(program);
registerTokens(program);
registerUpgrade(program);
await registerScale(program);
registerMe(program);
await registerVM(program);
await registerNodes(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);
});
const config = await loadConfig();
let exchangeAccountId = config.account_id;
if (!exchangeAccountId) {
const client = await apiClient(config.auth_token);
const { data } = await client.GET("/v0/me");
if (data?.id) {
exchangeAccountId = data.id;
saveConfig({ ...config, account_id: data.id });
}
}
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();