Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
import { projectScriptRuntimeEnv, setupProjectScript } from "@t3tools/shared/projectScripts";
|
|
import { Effect, Layer } from "effect";
|
|
|
|
import { OrchestrationEngineService } from "../../orchestration/Services/OrchestrationEngine.ts";
|
|
import { TerminalManager } from "../../terminal/Services/Manager.ts";
|
|
import {
|
|
type ProjectSetupScriptRunnerShape,
|
|
ProjectSetupScriptRunner,
|
|
} from "../Services/ProjectSetupScriptRunner.ts";
|
|
|
|
const makeProjectSetupScriptRunner = Effect.gen(function* () {
|
|
const orchestrationEngine = yield* OrchestrationEngineService;
|
|
const terminalManager = yield* TerminalManager;
|
|
|
|
const runForThread: ProjectSetupScriptRunnerShape["runForThread"] = (input) =>
|
|
Effect.gen(function* () {
|
|
const readModel = yield* orchestrationEngine.getReadModel();
|
|
const project =
|
|
(input.projectId
|
|
? readModel.projects.find((entry) => entry.id === input.projectId)
|
|
: null) ??
|
|
(input.projectCwd
|
|
? readModel.projects.find((entry) => entry.workspaceRoot === input.projectCwd)
|
|
: null) ??
|
|
null;
|
|
|
|
if (!project) {
|
|
return yield* Effect.fail(new Error("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 env = projectScriptRuntimeEnv({
|
|
project: { cwd: project.workspaceRoot },
|
|
worktreePath: input.worktreePath,
|
|
});
|
|
|
|
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;
|
|
});
|
|
|
|
return {
|
|
runForThread,
|
|
} satisfies ProjectSetupScriptRunnerShape;
|
|
});
|
|
|
|
export const ProjectSetupScriptRunnerLive = Layer.effect(
|
|
ProjectSetupScriptRunner,
|
|
makeProjectSetupScriptRunner,
|
|
);
|