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