The best way to control Claude Code, Codex and more from any computer or phone

main
ts 91 lines 3.29 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as Path from "effect/Path";
5 import * as PlatformError from "effect/PlatformError";
6 import * as Schema from "effect/Schema";
7 import * as ChildProcess from "effect/process/ChildProcess";
8 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9 import * as HostProcess from "@t3tools/shared/HostProcess";
10 import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv";
11
12 class ReplayFixtureGitCommandError extends Schema.TaggedError<ReplayFixtureGitCommandError>()(
13 "ReplayFixtureGitCommandError",
14 {
15 command: Schema.String,
16 exitCode: Schema.Number,
17 },
18 ) {
19 override get message(): string {
20 return `${this.command} failed with exit ${this.exitCode}.`;
21 }
22 }
23
24 function runGit(
25 cwd: string,
26 args: ReadonlyArray<string>,
27 ): Effect.Effect<
28 void,
29 ReplayFixtureGitCommandError | PlatformError.PlatformError,
30 ChildProcessSpawner.ChildProcessSpawner
31 > {
32 return Effect.gen(function* () {
33 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
34 const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform);
35 const exitCode = yield* spawner.exitCode(
36 ChildProcess.make("git", args, { cwd, env, extendEnv: true }),
37 );
38 if (Number(exitCode) !== 0) {
39 return yield* new ReplayFixtureGitCommandError({
40 command: `git ${args.join(" ")}`,
41 exitCode: Number(exitCode),
42 });
43 }
44 });
45 }
46
47 export type ReplayWorkspaceFiles = Readonly<Record<string, string>>;
48
49 const makeCheckpointWorkspaceEffect = Effect.fn("makeCheckpointWorkspace")(function* (
50 fixtureName: string,
51 files: ReplayWorkspaceFiles = {},
52 ) {
53 const fs = yield* FileSystem.FileSystem;
54 const path = yield* Path.Path;
55 const cwd = yield* fs.makeTempDirectory({
56 prefix: `t3-orchestrator-v2-${fixtureName}-`,
57 });
58 yield* runGit(cwd, ["init"]);
59 yield* runGit(cwd, ["config", "user.name", "T3 Code Test"]);
60 yield* runGit(cwd, ["config", "user.email", "t3code-test@example.com"]);
61 yield* fs.writeFileString(path.join(cwd, "README.md"), `# ${fixtureName}\n`);
62 for (const [relativePath, contents] of Object.entries(files)) {
63 const filePath = path.join(cwd, relativePath);
64 yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
65 yield* fs.writeFileString(filePath, contents);
66 }
67 yield* runGit(cwd, ["add", "README.md", ...Object.keys(files)]);
68 yield* runGit(cwd, ["commit", "-m", "initial"]);
69 return cwd;
70 });
71
72 const removeCheckpointWorkspaceEffect = Effect.fn("removeCheckpointWorkspace")(function* (
73 cwd: string,
74 ) {
75 const fs = yield* FileSystem.FileSystem;
76 yield* fs.remove(cwd, { recursive: true });
77 });
78
79 export const checkpointWorkspace = (fixtureName: string, files?: ReplayWorkspaceFiles) =>
80 Effect.acquireRelease(makeCheckpointWorkspaceEffect(fixtureName, files), (cwd) =>
81 removeCheckpointWorkspaceEffect(cwd).pipe(Effect.orDie),
82 ).pipe(Effect.provide(NodeServices.layer));
83
84 export async function makeCheckpointWorkspace(
85 fixtureName: string,
86 files?: ReplayWorkspaceFiles,
87 ): Promise<string> {
88 return await Effect.runPromise(
89 makeCheckpointWorkspaceEffect(fixtureName, files).pipe(Effect.provide(NodeServices.layer)),
90 );
91 }