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

editor.ts

147 lines5.7 KB

1import type { EDITORS } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Option from "effect/Option";
5import * as Path from "effect/Path";
6​
7import * as HostProcess from "./hostProcess.ts";
8import { isCommandAvailable } from "./shell.ts";
9​
10type Editor = (typeof EDITORS)[number];
11​
12const installNames: Partial<Record<Editor["id"], ReadonlyArray<string>>> = {
13 vscode: ["Visual Studio Code"],
14 "vscode-insiders": ["Visual Studio Code - Insiders"],
15 // `Antigravity.app` is the separate Hub, not the IDE.
16 antigravity: ["Antigravity IDE"],
17 idea: ["IntelliJ IDEA", "IntelliJ IDEA CE", "IntelliJ IDEA Ultimate"],
18 pycharm: ["PyCharm", "PyCharm CE"],
19 rider: ["Rider", "JetBrains Rider"],
20};
21​
22/** True for `<name> <version>.app`, where the version is digits and dots (`IntelliJ IDEA 2026.1.4.app`).
23 * A plain prefix match would let one editor steal another's bundle, e.g. stable VS Code resolving to
24 * `Visual Studio Code - Insiders.app`. */
25const isVersionedBundle = (entry: string, name: string) => {
26 if (!entry.startsWith(`${name} `) || !entry.endsWith(".app")) return false;
27 return /^\d[\d.]*$/.test(entry.slice(name.length + 1, -".app".length));
28};
29​
30export const resolveEditorCommand = Effect.fn("editor.resolveEditorCommand")(function* (
31 editor: Editor,
32 env: NodeJS.ProcessEnv,
33) {
34 if (editor.commands === null) return Option.none();
35 const baseArgs = "baseArgs" in editor ? editor.baseArgs : [];
36 for (const command of editor.commands) {
37 if (yield* isCommandAvailable(command, { env })) return Option.some({ command, baseArgs });
38 }
39​
40 const platform = yield* HostProcess.HostProcessPlatform;
41 const path = yield* Path.Path;
42 const fs = yield* FileSystem.FileSystem;
43 const home = env.HOME;
44 const names = installNames[editor.id] ?? [editor.label];
45 const command = editor.commands[0];
46 const jetbrains = editor.launchStyle === "line-column";
47 const candidates: string[] = [];
48​
49 if (platform === "darwin") {
50 const roots = [...(home ? [path.join(home, "Applications")] : []), "/Applications"];
51 for (const root of roots) {
52 // JetBrains Toolbox installs bundles named after the app and its version
53 // (`IntelliJ IDEA 2026.1.4.app`), so list the directory and accept
54 // `<name> <version>.app` as well as the exact `<name>.app`.
55 const entries = yield* fs.readDirectory(root).pipe(Effect.orElseSucceed(() => []));
56 const bundles = new Set(names.map((name) => `${name}.app`));
57 for (const entry of entries) {
58 if (names.some((name) => entry === `${name}.app` || isVersionedBundle(entry, name))) {
59 bundles.add(entry);
60 }
61 }
62 for (const bundle of bundles) {
63 const contents = path.join(root, bundle, "Contents");
64 candidates.push(
65 ...(jetbrains || editor.id === "zed"
66 ? [path.join(contents, "MacOS", editor.id === "zed" ? "cli" : command)]
67 : [
68 path.join(contents, "Resources/app/bin", command),
69 path.join(contents, "Resources/app/bin/code"),
70 ]),
71 );
72 }
73 }
74 if (home && jetbrains) {
75 candidates.push(
76 path.join(home, "Library/Application Support/JetBrains/Toolbox/scripts", command),
77 );
78 }
79 } else if (platform === "win32") {
80 const roots = [
81 ...(env.LOCALAPPDATA ? [path.join(env.LOCALAPPDATA, "Programs")] : []),
82 ...[env.ProgramFiles, env["ProgramFiles(x86)"], env.ProgramW6432].filter(
83 (root): root is string => !!root,
84 ),
85 ];
86 if (jetbrains) {
87 if (env.LOCALAPPDATA) {
88 candidates.push(path.join(env.LOCALAPPDATA, "JetBrains/Toolbox/scripts", `${command}.cmd`));
89 }
90 for (const directory of roots.flatMap((root) => [root, path.join(root, "JetBrains")])) {
91 const entries = yield* fs.readDirectory(directory).pipe(Effect.orElseSucceed(() => []));
92 for (const entry of entries) {
93 if (names.some((name) => entry === name || entry.startsWith(`${name} `))) {
94 candidates.push(path.join(directory, entry, "bin", `${command}64.exe`));
95 candidates.push(path.join(directory, entry, "bin", `${command}.exe`));
96 }
97 }
98 }
99 } else {
100 const name =
101 editor.id === "vscode"
102 ? "Microsoft VS Code"
103 : editor.id === "vscode-insiders"
104 ? "Microsoft VS Code Insiders"
105 : editor.label;
106 for (const root of roots) {
107 candidates.push(
108 path.join(root, name, "resources/app/bin", `${command}.cmd`),
109 path.join(root, name, "resources/app/bin/code.cmd"),
110 path.join(root, name, "bin", `${command}.cmd`),
111 path.join(root, name, "bin/code.cmd"),
112 );
113 if (editor.id === "zed") {
114 candidates.push(
115 path.join(root, name, "bin", "zed.exe"),
116 path.join(root, name, "zed.exe"),
117 );
118 }
119 }
120 }
121 } else if (platform === "linux") {
122 const dirs = [
123 ...(home ? [path.join(home, ".local/bin")] : []),
124 "/usr/local/bin",
125 "/usr/bin",
126 "/snap/bin",
127 ];
128 if (jetbrains) {
129 const dataHome = env.XDG_DATA_HOME || (home ? path.join(home, ".local/share") : undefined);
130 if (dataHome) dirs.push(path.join(dataHome, "JetBrains/Toolbox/scripts"));
131 }
132 for (const dir of dirs) {
133 for (const name of editor.commands) candidates.push(path.join(dir, name));
134 }
135 }
136​
137 for (const candidate of candidates) {
138 if (yield* isCommandAvailable(candidate, { env })) {
139 return Option.some({
140 command: candidate,
141 baseArgs:
142 editor.id === "kiro" && (platform === "darwin" || platform === "win32") ? [] : baseArgs,
143 });
144 }
145 }
146 return Option.none();
147});