Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

replayWorkspace.ts

99 lines3.6 KB

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 * as ChildProcess from "effect/process/ChildProcess";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv";
11​
12class 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 }
23​
24// `git commit` otherwise leaves a detached `git maintenance run --auto` behind, which can still
25// be writing into `.git` when a test removes the workspace (ENOTEMPTY).
26const NO_BACKGROUND_MAINTENANCE = ["-c", "maintenance.auto=false", "-c", "gc.auto=0"];
27​
28function runGit(
29 cwd: string,
30 args: ReadonlyArray<string>,
31): Effect.Effect<
32 void,
33 ReplayFixtureGitCommandError | PlatformError.PlatformError,
34 ChildProcessSpawner.ChildProcessSpawner
35> {
36 return Effect.gen(function* () {
37 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
38 const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform);
39 const exitCode = yield* spawner.exitCode(
40 ChildProcess.make("git", [...NO_BACKGROUND_MAINTENANCE, ...args], {
41 cwd,
42 env,
43 extendEnv: true,
44 }),
45 );
46 if (Number(exitCode) !== 0) {
47 return yield* new ReplayFixtureGitCommandError({
48 command: `git ${args.join(" ")}`,
49 exitCode: Number(exitCode),
50 });
51 }
52 });
54​
55export type ReplayWorkspaceFiles = Readonly<Record<string, string>>;
56​
57const makeCheckpointWorkspaceEffect = Effect.fn("makeCheckpointWorkspace")(function* (
58 fixtureName: string,
59 files: ReplayWorkspaceFiles = {},
60) {
61 const fs = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 const cwd = yield* fs.makeTempDirectory({
64 prefix: `t3-orchestrator-v2-${fixtureName}-`,
65 });
66 yield* runGit(cwd, ["init"]);
67 yield* runGit(cwd, ["config", "user.name", "T3 Code Test"]);
68 yield* runGit(cwd, ["config", "user.email", "t3code-test@example.com"]);
69 yield* fs.writeFileString(path.join(cwd, "README.md"), `# ${fixtureName}\n`);
70 for (const [relativePath, contents] of Object.entries(files)) {
71 const filePath = path.join(cwd, relativePath);
72 yield* fs.makeDirectory(path.dirname(filePath), { recursive: true });
73 yield* fs.writeFileString(filePath, contents);
74 }
75 yield* runGit(cwd, ["add", "README.md", ...Object.keys(files)]);
76 yield* runGit(cwd, ["commit", "-m", "initial"]);
77 return cwd;
78});
79​
80const removeCheckpointWorkspaceEffect = Effect.fn("removeCheckpointWorkspace")(function* (
81 cwd: string,
82) {
83 const fs = yield* FileSystem.FileSystem;
84 yield* fs.remove(cwd, { recursive: true });
85});
86​
87export const checkpointWorkspace = (fixtureName: string, files?: ReplayWorkspaceFiles) =>
88 Effect.acquireRelease(makeCheckpointWorkspaceEffect(fixtureName, files), (cwd) =>
89 removeCheckpointWorkspaceEffect(cwd).pipe(Effect.orDie),
90 ).pipe(Effect.provide(NodeServices.layer));
91​
92export async function makeCheckpointWorkspace(
93 fixtureName: string,
94 files?: ReplayWorkspaceFiles,
95): Promise<string> {
96 return await Effect.runPromise(
97 makeCheckpointWorkspaceEffect(fixtureName, files).pipe(Effect.provide(NodeServices.layer)),
98 );