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

open.ts

135 lines3.8 KB

1/**
2 * Open - Browser/editor launch service interface.
3 *
4 * Owns process launch helpers for opening URLs in a browser and workspace
5 * paths in a configured editor.
6 *
7 * @module Open
8 */
9import { spawn } from "node:child_process";
10​
11import { EDITORS, type EditorId } from "@t3tools/contracts";
12import { ServiceMap, Schema, Effect, Layer } from "effect";
13​
14// ==============================
15// Definitions
16// ==============================
17​
18export class OpenError extends Schema.TaggedErrorClass<OpenError>()("OpenError", {
19 message: Schema.String,
20 cause: Schema.optional(Schema.Defect),
21}) {}
22​
23export interface OpenInEditorInput {
24 readonly cwd: string;
25 readonly editor: EditorId;
27​
28interface EditorLaunch {
29 readonly command: string;
30 readonly args: ReadonlyArray<string>;
32​
33const LINE_COLUMN_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
34​
35function shouldUseGotoFlag(editorId: EditorId, target: string): boolean {
36 return editorId === "cursor" && LINE_COLUMN_SUFFIX_PATTERN.test(target);
38​
39/**
40 * OpenShape - Service API for browser and editor launch actions.
41 */
42export interface OpenShape {
43 /**
44 * Open a URL target in the default browser.
45 */
46 readonly openBrowser: (target: string) => Effect.Effect<void, OpenError>;
47​
48 /**
49 * Open a workspace path in a selected editor integration.
50 *
51 * Launches the editor as a detached process so server startup is not blocked.
52 */
53 readonly openInEditor: (input: OpenInEditorInput) => Effect.Effect<void, OpenError>;
55​
56/**
57 * Open - Service tag for browser/editor launch operations.
58 */
59export class Open extends ServiceMap.Service<Open, OpenShape>()("t3/open") {}
60​
61// ==============================
62// Implementations
63// ==============================
64​
65export const resolveEditorLaunch = Effect.fnUntraced(function* (
66 input: OpenInEditorInput,
67 platform: NodeJS.Platform = process.platform,
68): Effect.fn.Return<EditorLaunch, OpenError> {
69 const editorDef = EDITORS.find((editor) => editor.id === input.editor);
70 if (!editorDef) {
71 return yield* new OpenError({ message: `Unknown editor: ${input.editor}` });
72 }
73​
74 if (editorDef.command) {
75 return shouldUseGotoFlag(editorDef.id, input.cwd)
76 ? { command: editorDef.command, args: ["--goto", input.cwd] }
77 : { command: editorDef.command, args: [input.cwd] };
78 }
79​
80 if (editorDef.id !== "file-manager") {
81 return yield* new OpenError({ message: `Unsupported editor: ${input.editor}` });
82 }
83​
84 switch (platform) {
85 case "darwin":
86 return { command: "open", args: [input.cwd] };
87 case "win32":
88 return { command: "explorer", args: [input.cwd] };
89 default:
90 return { command: "xdg-open", args: [input.cwd] };
91 }
92});
93​
94export const launchDetached = (launch: EditorLaunch) =>
95 Effect.callback<void, OpenError>((resume) => {
96 let child;
97 try {
98 child = spawn(launch.command, [...launch.args], {
99 detached: true,
100 stdio: "ignore",
101 });
102 } catch (error) {
103 return resume(
104 Effect.fail(new OpenError({ message: "failed to spawn detached process", cause: error })),
105 );
106 }
107​
108 const handleSpawn = () => {
109 child.unref();
110 resume(Effect.void);
111 };
112​
113 child.once("spawn", handleSpawn);
114 child.once("error", (cause) =>
115 resume(Effect.fail(new OpenError({ message: "failed to spawn detached process", cause }))),
116 );
117 });
118​
119const make = Effect.gen(function* () {
120 const open = yield* Effect.tryPromise({
121 try: () => import("open"),
122 catch: (cause) => new OpenError({ message: "failed to load browser opener", cause }),
123 });
124​
125 return {
126 openBrowser: (target) =>
127 Effect.tryPromise({
128 try: () => open.default(target),
129 catch: (cause) => new OpenError({ message: "Browser auto-open failed", cause }),
130 }),
131 openInEditor: (input) => Effect.flatMap(resolveEditorLaunch(input), launchDetached),
132 } satisfies OpenShape;
133});
134​
135export const OpenLive = Layer.effect(Open, make);