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

DesktopCliShim.ts

166 lines6.8 KB

1import * as Cause from "effect/Cause";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Option from "effect/Option";
5import * as Random from "effect/Random";
6​
7import * as DesktopEnvironment from "./DesktopEnvironment.ts";
8import { makeComponentLogger } from "./DesktopObservability.ts";
9​
10// AppImage mounts change each run; the launcher must mount the image itself.
11const { logInfo, logWarning } = makeComponentLogger("desktop-cli-shim");
12​
13export const MARKER = "Written by T3 Code: runs the desktop app's bundled t3 CLI.";
14​
15const SERVER_ENTRY = "apps/server/dist/bin.mjs";
16​
17const shellWord = (value: string) => `'${value.replaceAll("'", `'"'"'`)}'`;
18/** cmd.exe expands `%` even inside quotes; Windows paths cannot contain `"`. */
19const cmdText = (value: string) => value.replaceAll("%", "%%");
20const cmdWord = (value: string) => `"${cmdText(value)}"`;
21​
22const MOVED = "T3 Code has moved or been removed. Open the app once to update this command.";
23​
24export type CliShimTarget =
25 | { readonly kind: "appimage"; readonly appImage: string; readonly executableName: string }
26 | { readonly kind: "direct"; readonly executable: string; readonly entry: string }
27 | { readonly kind: "windows"; readonly executable: string; readonly entry: string };
28​
29/** Embed this install's home and executable path so `sudo`, which clears the environment, uses the same install. */
30export const renderCliShim = (input: {
31 readonly target: CliShimTarget;
32 readonly shimPath: string;
33 readonly t3Home: string;
34}) => {
35 const { target } = input;
36 if (target.kind === "windows") {
37 const utf8 = [target.executable, target.entry, input.shimPath, input.t3Home].some((value) =>
38 [...value].some((character) => character.codePointAt(0)! > 0x7f),
39 );
40 const restore = utf8 ? ["chcp %t3_codepage% >nul"] : [];
41 return [
42 "@echo off",
43 `rem ${MARKER}`,
44 // Delayed expansion would rewrite `!name!` in paths and arguments.
45 "setlocal EnableExtensions DisableDelayedExpansion",
46 // Ctrl-C skips codepage restoration, so switch to UTF-8 only for non-ASCII install paths.
47 ...(utf8
48 ? [
49 `for /f "tokens=2 delims=:." %%c in ('chcp') do set "t3_codepage=%%c"`,
50 "chcp 65001 >nul",
51 ]
52 : []),
53 `set "T3CODE_CLI_PATH=${cmdText(input.shimPath)}"`,
54 `if not defined T3CODE_HOME set "T3CODE_HOME=${cmdText(input.t3Home)}"`,
55 'set "ELECTRON_RUN_AS_NODE=1"',
56 // Electron's Windows executable starts without stdin; this tells the CLI to reopen the console.
57 'set "T3CODE_DESKTOP_CLI_LAUNCHER=1"',
58 // A goto, not a parenthesized block: "Program Files (x86)" would close the block early.
59 `if exist ${cmdWord(target.executable)} goto run`,
60 ...restore,
61 `echo ${MOVED} 1>&2`,
62 "exit /b 127",
63 ":run",
64 `${cmdWord(target.executable)} ${cmdWord(target.entry)} %*`,
65 'set "t3_exit=%ERRORLEVEL%"',
66 ...restore,
67 "exit /b %t3_exit%",
68 "",
69 ].join("\r\n");
70 }
71 const header = [
72 "#!/bin/sh",
73 `# ${MARKER}`,
74 `export T3CODE_CLI_PATH=${shellWord(input.shimPath)}`,
75 `home=${shellWord(input.t3Home)}`,
76 'export T3CODE_HOME="${T3CODE_HOME:-$home}"',
77 "export ELECTRON_RUN_AS_NODE=1",
78 `app=${shellWord(target.kind === "appimage" ? target.appImage : target.executable)}`,
79 'if [ ! -x "$app" ]; then',
80 ` echo "${MOVED}" >&2`,
81 " exit 127",
82 "fi",
83 ];
84 if (target.kind === "direct") {
85 return [...header, `exec "$app" ${shellWord(target.entry)} "$@"`, ""].join("\n");
86 }
87 const run = (root: string) =>
88 `"${root}/${target.executableName}" "${root}/resources/app.asar/${SERVER_ENTRY}" "$@"`;
89 return [
90 ...header,
91 // Extract-and-run would make the runtime start the app instead of mounting it.
92 "unset APPIMAGE_EXTRACT_AND_RUN",
93 // The runtime stays mounted until killed; traps unmount it because background jobs ignore Ctrl-C.
94 "work=$(mktemp -d) || exit 1",
95 '"$app" --appimage-mount >"$work/mount" 2>/dev/null &',
96 "mounter=$!",
97 `trap 'kill "$mounter" 2>/dev/null; rm -rf "$work"' EXIT`,
98 "trap 'exit 130' INT TERM",
99 "mount=",
100 'while kill -0 "$mounter" 2>/dev/null; do',
101 ' mount=$(head -n 1 "$work/mount")',
102 ' [ -d "$mount" ] && break',
103 " mount=",
104 " sleep 0.05",
105 "done",
106 // Without FUSE the runtime cannot mount (it prints why and exits); extracting the image still works.
107 'if [ -z "$mount" ]; then',
108 ' (cd "$work" && "$app" --appimage-extract >/dev/null) || { echo "Could not open $app." >&2; exit 1; }',
109 ' mount="$work/squashfs-root"',
110 "fi",
111 run("$mount"),
112 "",
113 ].join("\n");
114};
115​
116export const launcherPath = (environment: DesktopEnvironment.DesktopEnvironment["Service"]) =>
117 environment.path.join(
118 environment.baseDir,
119 "bin",
120 environment.platform === "win32" ? "t3.cmd" : "t3",
121 );
122​
123export const install = Effect.gen(function* () {
124 const environment = yield* DesktopEnvironment.DesktopEnvironment;
125 if (!environment.isPackaged) return Option.none<string>();
126 const fs = yield* FileSystem.FileSystem;
127 const path = environment.path;
128 const windows = environment.platform === "win32";
129 const shimPath = launcherPath(environment);
130 const entry = path.join(environment.serverRoot, SERVER_ENTRY);
131 const target: CliShimTarget = windows
132 ? { kind: "windows", executable: process.execPath, entry }
133 : Option.match(environment.appImagePath, {
134 onSome: (appImage) => ({
135 kind: "appimage" as const,
136 appImage,
137 executableName: path.basename(process.execPath),
138 }),
139 onNone: () => ({ kind: "direct" as const, executable: process.execPath, entry }),
140 });
141 const content = renderCliShim({ target, shimPath, t3Home: environment.baseDir });
142​
143 return yield* Effect.gen(function* () {
144 const existing = yield* fs.readFileString(shimPath).pipe(Effect.option);
145 if (Option.isSome(existing) && !existing.value.includes(MARKER)) {
146 yield* logWarning("leaving a t3 launcher the app did not write", { shimPath });
147 return Option.none<string>();
148 }
149 if (Option.getOrUndefined(existing) !== content) {
150 yield* fs.makeDirectory(path.dirname(shimPath), { recursive: true });
151 // Unique per call: backend startup and the first-launch command install can race here.
152 const staging = `${shimPath}.${process.pid}.${yield* Random.nextIntBetween(0, 0xffffff)}.tmp`;
153 yield* fs.writeFileString(staging, content, { mode: 0o755 });
154 yield* fs.rename(staging, shimPath);
155 yield* logInfo("installed t3 launcher", { shimPath });
156 }
157 return Option.some(shimPath);
158 }).pipe(
159 Effect.catchCause((cause) =>
160 logWarning("could not install t3 launcher", { shimPath, cause: Cause.pretty(cause) }).pipe(
161 Effect.as(Option.none<string>()),
162 ),
163 ),
164 Effect.withSpan("desktop.cliShim.install"),
165 );
166});