Open History to see the last commit
1import packageJson from "../../package.json" with { type: "json" };
2import { BUILD_MIGRATION_HEAD, recordedMigrations } from "../persistence/databaseMigrationHead.ts";
3import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
4
5export type ServicePreflightResult =
6 | {
7 readonly status: "ready";
8 readonly version: string;
9 readonly launcherProtocol: typeof SERVICE_LAUNCHER_PROTOCOL;
10 }
11 | {
12 readonly status: "blocked";
13 readonly version: string;
14 readonly reason: string;
15 };
16
17export function runServicePreflight(input: {
18 readonly databasePath: string;
19 readonly launcherProtocol: number;
20 readonly version?: string;
21}): ServicePreflightResult {
22 const version = input.version ?? packageJson.version;
23 if (input.launcherProtocol !== SERVICE_LAUNCHER_PROTOCOL) {
24 return {
25 status: "blocked",
26 version,
27 // A stale `t3` earlier on PATH (an old npm/nvm global) makes `t3 service install`
28 // reinstall itself and report success, so name the exact install and the path check.
29 reason: [
30 `The installed service launcher speaks protocol ${input.launcherProtocol}, but t3@${version} requires protocol ${SERVICE_LAUNCHER_PROTOCOL}.`,
31 `On the server machine, install this exact CLI: \`curl -fsSL https://t3.codes/install.sh | T3CODE_VERSION=${version} sh\`.`,
32 `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).`,
33 "Updating the desktop app does not update this CLI, and `t3 service update` only repairs the service with whichever CLI runs it.",
34 ].join(" "),
35 };
36 }
37
38 const newer = newerRecordedMigrations(input.databasePath);
39 if (newer === null) {
40 return {
41 status: "blocked",
42 version,
43 reason: `Could not read this database to confirm t3@${version} supports it.`,
44 };
45 }
46 if (newer.length > 0) {
47 return {
48 status: "blocked",
49 version,
50 reason: `This database has migrations that t3@${version} does not know: ${newer.join(", ")}. A newer build wrote them, and running t3@${version} on this database is not safe. Stay on the newer release, or wait for a release that includes these migrations.`,
51 };
52 }
53
54 return { status: "ready", version, launcherProtocol: SERVICE_LAUNCHER_PROTOCOL };
55}
56
57/** Migrations a newer build recorded, or null when the database exists but cannot be read.
58 * Startup already handles renamed ids this build knows. */
59function newerRecordedMigrations(databasePath: string): ReadonlyArray<string> | null {
60 const recorded = recordedMigrations(databasePath);
61 return recorded === null
62 ? null
63 : recorded.filter(({ id }) => id > BUILD_MIGRATION_HEAD).map(({ id, name }) => `${id}_${name}`);
64}
65
66export function decodeServicePreflightResult(value: unknown): ServicePreflightResult | undefined {
67 if (typeof value !== "object" || value === null) {
68 return undefined;
69 }
70 const record = value as Record<string, unknown>;
71 if (
72 record.status === "ready" &&
73 record.launcherProtocol === SERVICE_LAUNCHER_PROTOCOL &&
74 typeof record.version === "string"
75 ) {
76 return {
77 status: "ready",
78 version: record.version,
79 launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
80 };
81 }
82 if (
83 record.status === "blocked" &&
84 typeof record.version === "string" &&
85 typeof record.reason === "string"
86 ) {
87 return { status: "blocked", version: record.version, reason: record.reason };
88 }
89 return undefined;
90}