Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

servicePreflight.ts

64 lines2.4 KB

1import packageJson from "../../package.json" with { type: "json" };
2import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
3​
4export type ServicePreflightResult =
5 | {
6 readonly status: "ready";
7 readonly version: string;
8 readonly launcherProtocol: typeof SERVICE_LAUNCHER_PROTOCOL;
9 }
10 | {
11 readonly status: "blocked";
12 readonly version: string;
13 readonly reason: string;
14 };
15​
16export function runServicePreflight(input: {
17 readonly databasePath: string;
18 readonly launcherProtocol: number;
19 readonly version?: string;
20}): ServicePreflightResult {
21 const version = input.version ?? packageJson.version;
22 if (input.launcherProtocol !== SERVICE_LAUNCHER_PROTOCOL) {
23 return {
24 status: "blocked",
25 version,
26 // A stale `t3` earlier on PATH (an old npm/nvm global) makes `t3 service install`
27 // reinstall itself and report success, so name the exact install and the path check.
28 reason: [
29 `The installed service launcher speaks protocol ${input.launcherProtocol}, but t3@${version} requires protocol ${SERVICE_LAUNCHER_PROTOCOL}.`,
30 `On the server machine, install this exact CLI: \`curl -fsSL https://t3.codes/install.sh | T3CODE_VERSION=${version} sh\`.`,
31 `Check that \`t3 --version\` prints ${version} and that \`which -a t3\` lists no older t3 (such as an npm or nvm install) first, then run \`~/.local/bin/t3 service install\` (the installer's default path).`,
32 "Updating the desktop app does not update this CLI, and `t3 service update` only repairs the service with whichever CLI runs it.",
33 ].join(" "),
34 };
35 }
36​
37 return { status: "ready", version, launcherProtocol: SERVICE_LAUNCHER_PROTOCOL };
39​
40export function decodeServicePreflightResult(value: unknown): ServicePreflightResult | undefined {
41 if (typeof value !== "object" || value === null) {
42 return undefined;
43 }
44 const record = value as Record<string, unknown>;
45 if (
46 record.status === "ready" &&
47 record.launcherProtocol === SERVICE_LAUNCHER_PROTOCOL &&
48 typeof record.version === "string"
49 ) {
50 return {
51 status: "ready",
52 version: record.version,
53 launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
54 };
55 }
56 if (
57 record.status === "blocked" &&
58 typeof record.version === "string" &&
59 typeof record.reason === "string"
60 ) {
61 return { status: "blocked", version: record.version, reason: record.reason };
62 }
63 return undefined;