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

CheckpointStore.ts

158 lines5.4 KB

1import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import type { CheckpointStoreError } from "./Errors.ts";
7import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
8import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
9​
10export interface CaptureCheckpointInput {
11 readonly cwd: string;
12 readonly checkpointRef: CheckpointRef;
14​
15export interface RestoreCheckpointInput {
16 readonly cwd: string;
17 readonly checkpointRef: CheckpointRef;
18 readonly fallbackToHead?: boolean;
20​
21export interface DiffCheckpointsInput {
22 readonly cwd: string;
23 readonly fromCheckpointRef: CheckpointRef;
24 readonly toCheckpointRef: CheckpointRef;
25 readonly fallbackFromToHead?: boolean;
26 readonly ignoreWhitespace: boolean;
27 readonly format?: "patch" | "numstat";
28 /** Limits the diff to these exact paths. An empty list yields an empty diff. */
29 readonly filePaths?: ReadonlyArray<string>;
31​
32export interface ListAuthoredPathsInput {
33 readonly cwd: string;
34 readonly fromCheckpointRef: CheckpointRef;
35 readonly toCheckpointRef: CheckpointRef;
37​
38export interface DeleteCheckpointRefsInput {
39 readonly cwd: string;
40 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
42​
43export class CheckpointStore extends Context.Service<
44 CheckpointStore,
45 {
46 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
47​
48 readonly captureCheckpoint: (
49 input: CaptureCheckpointInput,
50 ) => Effect.Effect<void, CheckpointStoreError>;
51​
52 readonly hasCheckpointRef: (
53 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
54 ) => Effect.Effect<boolean, CheckpointStoreError>;
55​
56 readonly restoreCheckpoint: (
57 input: RestoreCheckpointInput,
58 ) => Effect.Effect<boolean, CheckpointStoreError>;
59​
60 readonly diffCheckpoints: (
61 input: DiffCheckpointsInput,
62 ) => Effect.Effect<string, CheckpointStoreError>;
63​
64 /**
65 * Returns null when HEAD did not move or a checkpoint does not record HEAD.
66 * Then every changed path belongs to the turn.
67 */
68 readonly listAuthoredPaths: (
69 input: ListAuthoredPathsInput,
70 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
71​
72 readonly deleteCheckpointRefs: (
73 input: DeleteCheckpointRefsInput,
74 ) => Effect.Effect<void, CheckpointStoreError>;
75 }
76>()("t3/checkpointing/CheckpointStore") {}
77​
78/** @public Service construction is part of the canonical Effect module API. */
79export const make = Effect.gen(function* () {
80 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
81​
82 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
83 operation: string,
84 cwd: string,
85 ) {
86 const handle = yield* vcsRegistry.resolve({ cwd });
87 if (!handle.driver.checkpoints) {
88 return yield* new VcsUnsupportedOperationError({
89 operation,
90 kind: handle.kind,
91 detail: `${handle.kind} driver does not implement checkpoint operations.`,
92 });
93 }
94 return handle.driver.checkpoints satisfies VcsCheckpointOps;
95 });
96​
97 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
98 vcsRegistry
99 .detect({ cwd, requestedKind: "git" })
100 .pipe(Effect.map((repository) => repository !== null));
101​
102 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
103 "captureCheckpoint",
104 )(function* (input) {
105 const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
106 return yield* checkpoints.captureCheckpoint(input);
107 });
108​
109 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
110 "hasCheckpointRef",
111 )(function* (input) {
112 const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
113 return yield* checkpoints.hasCheckpointRef(input);
114 });
115​
116 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
117 "restoreCheckpoint",
118 )(function* (input) {
119 const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
120 return yield* checkpoints.restoreCheckpoint(input);
121 });
122​
123 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
124 "diffCheckpoints",
125 )(function* (input) {
126 const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
127 return yield* checkpoints.diffCheckpoints(input);
128 });
129​
130 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
131 "listAuthoredPaths",
132 )(function* (input) {
133 const checkpoints = yield* resolveCheckpoints("CheckpointStore.listAuthoredPaths", input.cwd);
134 return yield* checkpoints.listAuthoredPaths(input);
135 });
136​
137 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
138 "deleteCheckpointRefs",
139 )(function* (input) {
140 const checkpoints = yield* resolveCheckpoints(
141 "CheckpointStore.deleteCheckpointRefs",
142 input.cwd,
143 );
144 return yield* checkpoints.deleteCheckpointRefs(input);
145 });
146​
147 return CheckpointStore.of({
148 isGitRepository,
149 captureCheckpoint,
150 hasCheckpointRef,
151 restoreCheckpoint,
152 diffCheckpoints,
153 listAuthoredPaths,
154 deleteCheckpointRefs,
155 });
156});
157​
158export const layer = Layer.effect(CheckpointStore, make);