Open History to see the last commit
1import { ProjectId, type ProjectScript } from "@t3tools/contracts";
2import { projectScriptRuntimeEnv, setupProjectScript } from "@t3tools/shared/projectScripts";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9import * as TerminalManager from "../terminal/Manager.ts";
10import * as ProjectService from "./ProjectService.ts";
11
12export interface ProjectSetupScriptRunnerResultNoScript {
13 readonly status: "no-script";
14}
15
16export interface ProjectSetupScriptRunnerResultStarted {
17 readonly status: "started";
18 readonly scriptId: string;
19 readonly scriptName: string;
20 readonly terminalId: string;
21 readonly cwd: string;
22}
23
24export type ProjectSetupScriptRunnerResult =
25 | ProjectSetupScriptRunnerResultNoScript
26 | ProjectSetupScriptRunnerResultStarted;
27
28export interface ProjectSetupScriptRunnerInput {
29 readonly threadId: string;
30 readonly projectId?: string;
31 readonly projectCwd?: string;
32 readonly worktreePath: string;
33 readonly preferredTerminalId?: string;
34 readonly project?: {
35 readonly workspaceRoot: string;
36 readonly scripts: ReadonlyArray<ProjectScript>;
37 };
38}
39
40export class ProjectSetupScriptOperationError extends Schema.TaggedErrorClass<ProjectSetupScriptOperationError>()(
41 "ProjectSetupScriptOperationError",
42 {
43 threadId: Schema.String,
44 projectId: Schema.optional(Schema.String),
45 projectCwd: Schema.optional(Schema.String),
46 worktreePath: Schema.String,
47 operation: Schema.Literals(["resolveProject", "openTerminal", "writeCommand"]),
48 cause: Schema.Defect(),
49 },
50) {
51 override get message(): string {
52 return `Project setup script operation '${this.operation}' failed for thread '${this.threadId}' in '${this.worktreePath}'.`;
53 }
54}
55
56export class ProjectSetupScriptProjectNotFoundError extends Schema.TaggedErrorClass<ProjectSetupScriptProjectNotFoundError>()(
57 "ProjectSetupScriptProjectNotFoundError",
58 {
59 threadId: Schema.String,
60 projectId: Schema.optional(Schema.String),
61 projectCwd: Schema.optional(Schema.String),
62 worktreePath: Schema.String,
63 },
64) {
65 override get message(): string {
66 return `Project was not found for setup script execution for thread '${this.threadId}' in '${this.worktreePath}'.`;
67 }
68}
69
70export const ProjectSetupScriptRunnerError = Schema.Union([
71 ProjectSetupScriptOperationError,
72 ProjectSetupScriptProjectNotFoundError,
73]);
74export type ProjectSetupScriptRunnerError = typeof ProjectSetupScriptRunnerError.Type;
75
76export class ProjectSetupScriptRunner extends Context.Service<
77 ProjectSetupScriptRunner,
78 {
79 readonly runForThread: (
80 input: ProjectSetupScriptRunnerInput,
81 ) => Effect.Effect<ProjectSetupScriptRunnerResult, ProjectSetupScriptRunnerError>;
82 }
83>()("t3/project/ProjectSetupScriptRunner") {}
84
85export const make = Effect.gen(function* () {
86 const projects = yield* ProjectService.ProjectService;
87 const terminalManager = yield* TerminalManager.TerminalManager;
88
89 const runForThread: ProjectSetupScriptRunner["Service"]["runForThread"] = Effect.fn(
90 "ProjectSetupScriptRunner.runForThread",
91 )(function* (input) {
92 const errorContext = {
93 threadId: input.threadId,
94 worktreePath: input.worktreePath,
95 ...(input.projectId === undefined ? {} : { projectId: input.projectId }),
96 ...(input.projectCwd === undefined ? {} : { projectCwd: input.projectCwd }),
97 };
98 const suppliedProject = input.project;
99 const projectById =
100 suppliedProject ??
101 (input.projectId
102 ? yield* projects.getById(ProjectId.make(input.projectId)).pipe(
103 Effect.map(Option.getOrUndefined),
104 Effect.mapError(
105 (cause) =>
106 new ProjectSetupScriptOperationError({
107 ...errorContext,
108 operation: "resolveProject",
109 cause,
110 }),
111 ),
112 )
113 : null);
114 const project =
115 suppliedProject ??
116 projectById ??
117 (input.projectCwd
118 ? yield* projects.getByWorkspaceRoot(input.projectCwd).pipe(
119 Effect.map(Option.getOrUndefined),
120 Effect.mapError(
121 (cause) =>
122 new ProjectSetupScriptOperationError({
123 ...errorContext,
124 operation: "resolveProject",
125 cause,
126 }),
127 ),
128 )
129 : null);
130
131 if (!project) {
132 return yield* new ProjectSetupScriptProjectNotFoundError(errorContext);
133 }
134
135 const script = setupProjectScript(project.scripts);
136 if (!script) {
137 return {
138 status: "no-script",
139 } as const;
140 }
141
142 const terminalId = input.preferredTerminalId ?? `setup-${script.id}`;
143 const cwd = input.worktreePath;
144 const env = projectScriptRuntimeEnv({
145 project: { cwd: project.workspaceRoot },
146 worktreePath: input.worktreePath,
147 });
148
149 yield* terminalManager
150 .open({
151 threadId: input.threadId,
152 terminalId,
153 cwd,
154 worktreePath: input.worktreePath,
155 env,
156 })
157 .pipe(
158 Effect.mapError(
159 (cause) =>
160 new ProjectSetupScriptOperationError({
161 ...errorContext,
162 operation: "openTerminal",
163 cause,
164 }),
165 ),
166 );
167 yield* terminalManager
168 .write({
169 threadId: input.threadId,
170 terminalId,
171 data: `${script.command}\r`,
172 })
173 .pipe(
174 Effect.mapError(
175 (cause) =>
176 new ProjectSetupScriptOperationError({
177 ...errorContext,
178 operation: "writeCommand",
179 cause,
180 }),
181 ),
182 );
183
184 return {
185 status: "started",
186 scriptId: script.id,
187 scriptName: script.name,
188 terminalId,
189 cwd,
190 } as const;
191 });
192
193 return ProjectSetupScriptRunner.of({ runForThread });
194});
195
196export const layer = Layer.effect(ProjectSetupScriptRunner, make);