-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathProjectSetupScriptRunner.ts
More file actions
108 lines (98 loc) · 3.42 KB
/
ProjectSetupScriptRunner.ts
File metadata and controls
108 lines (98 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { ProjectId } from "@t3tools/contracts";
import { projectScriptRuntimeEnv, setupProjectScript } from "@t3tools/shared/projectScripts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { TerminalManager } from "../../terminal/Services/Manager.ts";
import {
type ProjectSetupScriptRunnerShape,
ProjectSetupScriptRunner,
ProjectSetupScriptRunnerError,
} from "../Services/ProjectSetupScriptRunner.ts";
const makeProjectSetupScriptRunner = Effect.gen(function* () {
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
const serverSettings = yield* ServerSettingsService;
const terminalManager = yield* TerminalManager;
const runForThread: ProjectSetupScriptRunnerShape["runForThread"] = (input) =>
Effect.gen(function* () {
const project =
(input.projectId
? yield* projectionSnapshotQuery
.getProjectShellById(ProjectId.make(input.projectId))
.pipe(Effect.map(Option.getOrUndefined))
: null) ??
(input.projectCwd
? yield* projectionSnapshotQuery
.getActiveProjectByWorkspaceRoot(input.projectCwd)
.pipe(Effect.map(Option.getOrUndefined))
: null) ??
null;
if (!project) {
return yield* new ProjectSetupScriptRunnerError({
message: "Project was not found for setup script execution.",
});
}
const script = setupProjectScript(project.scripts);
if (!script) {
return {
status: "no-script",
} as const;
}
const terminalId = input.preferredTerminalId ?? `setup-${script.id}`;
const cwd = input.worktreePath;
const settings = yield* serverSettings.getSettings;
const actionEnvironment = settings.projectSettings[project.id]?.actionEnvironment ?? {};
const env = projectScriptRuntimeEnv({
project: { cwd: project.workspaceRoot },
worktreePath: input.worktreePath,
extraEnv: actionEnvironment,
});
yield* terminalManager.open({
threadId: input.threadId,
terminalId,
cwd,
worktreePath: input.worktreePath,
env,
});
yield* terminalManager.write({
threadId: input.threadId,
terminalId,
data: `${script.command}\r`,
});
return {
status: "started",
scriptId: script.id,
scriptName: script.name,
terminalId,
cwd,
} as const;
}).pipe(
Effect.mapError((cause) => {
if (
typeof cause === "object" &&
cause !== null &&
"_tag" in cause &&
cause._tag === "ProjectSetupScriptRunnerError"
) {
return cause as ProjectSetupScriptRunnerError;
}
const message =
typeof cause === "object" &&
cause !== null &&
"message" in cause &&
typeof cause.message === "string"
? cause.message
: String(cause);
return new ProjectSetupScriptRunnerError({ message });
}),
);
return {
runForThread,
} satisfies ProjectSetupScriptRunnerShape;
});
export const ProjectSetupScriptRunnerLive = Layer.effect(
ProjectSetupScriptRunner,
makeProjectSetupScriptRunner,
);