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

CheckpointStore.ts

148 lines5.0 KB

1import { 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;
13 /** The checkpoint this one follows, so a turn that switched branches diffs from where it landed. */
14 readonly previousCheckpointRef?: CheckpointRef;
16​
17export interface RestoreCheckpointInput {
18 readonly cwd: string;
19 readonly checkpointRef: CheckpointRef;
20 readonly fallbackToHead?: boolean;
22​
23export interface DiffCheckpointsInput {
24 readonly cwd: string;
25 readonly fromCheckpointRef: CheckpointRef;
26 readonly toCheckpointRef: CheckpointRef;
27 readonly fallbackFromToHead?: boolean;
28 readonly ignoreWhitespace: boolean;
29 readonly format?: "patch" | "numstat";
30 /** Maximum bytes collected from the diff command. */
31 readonly maxOutputBytes?: number;
32 /** Limits the diff to these exact paths. An empty list yields an empty diff. */
33 readonly filePaths?: ReadonlyArray<string>;
35​
36export interface ListAuthoredPathsInput {
37 readonly cwd: string;
38 readonly fromCheckpointRef: CheckpointRef;
39 readonly toCheckpointRef: CheckpointRef;
41​
42export interface DeleteCheckpointRefsInput {
43 readonly cwd: string;
44 readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
46​
47export class CheckpointStore extends Context.Service<
48 CheckpointStore,
49 {
50 readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;
51​
52 readonly captureCheckpoint: (
53 input: CaptureCheckpointInput,
54 ) => Effect.Effect<void, CheckpointStoreError>;
55​
56 readonly hasCheckpointRef: (
57 input: Omit<RestoreCheckpointInput, "fallbackToHead">,
58 ) => Effect.Effect<boolean, CheckpointStoreError>;
59​
60 readonly restoreCheckpoint: (
61 input: RestoreCheckpointInput,
62 ) => Effect.Effect<boolean, CheckpointStoreError>;
63​
64 readonly diffCheckpoints: (
65 input: DiffCheckpointsInput,
66 ) => Effect.Effect<string, CheckpointStoreError>;
67​
68 /** Null means HEAD is unchanged or unrecorded; all changed paths then belong to the turn. */
69 readonly listAuthoredPaths: (
70 input: ListAuthoredPathsInput,
71 ) => Effect.Effect<ReadonlySet<string> | null, CheckpointStoreError>;
72​
73 readonly deleteCheckpointRefs: (
74 input: DeleteCheckpointRefsInput,
75 ) => Effect.Effect<void, CheckpointStoreError>;
76 }
77>()("t3/checkpointing/CheckpointStore") {}
78​
79/** @public */
80export const make = Effect.gen(function* () {
81 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
82​
83 const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
84 cwd: string,
85 ) {
86 const handle = yield* vcsRegistry.resolve({ cwd });
87 return handle.driver.checkpoints satisfies VcsCheckpointOps;
88 });
89​
90 const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = (cwd) =>
91 vcsRegistry
92 .detect({ cwd, requestedKind: "git" })
93 .pipe(Effect.map((repository) => repository !== null));
94​
95 const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn(
96 "captureCheckpoint",
97 )(function* (input) {
98 const checkpoints = yield* resolveCheckpoints(input.cwd);
99 return yield* checkpoints.captureCheckpoint(input);
100 });
101​
102 const hasCheckpointRef: CheckpointStore["Service"]["hasCheckpointRef"] = Effect.fn(
103 "hasCheckpointRef",
104 )(function* (input) {
105 const checkpoints = yield* resolveCheckpoints(input.cwd);
106 return yield* checkpoints.hasCheckpointRef(input);
107 });
108​
109 const restoreCheckpoint: CheckpointStore["Service"]["restoreCheckpoint"] = Effect.fn(
110 "restoreCheckpoint",
111 )(function* (input) {
112 const checkpoints = yield* resolveCheckpoints(input.cwd);
113 return yield* checkpoints.restoreCheckpoint(input);
114 });
115​
116 const diffCheckpoints: CheckpointStore["Service"]["diffCheckpoints"] = Effect.fn(
117 "diffCheckpoints",
118 )(function* (input) {
119 const checkpoints = yield* resolveCheckpoints(input.cwd);
120 return yield* checkpoints.diffCheckpoints(input);
121 });
122​
123 const listAuthoredPaths: CheckpointStore["Service"]["listAuthoredPaths"] = Effect.fn(
124 "listAuthoredPaths",
125 )(function* (input) {
126 const checkpoints = yield* resolveCheckpoints(input.cwd);
127 return yield* checkpoints.listAuthoredPaths(input);
128 });
129​
130 const deleteCheckpointRefs: CheckpointStore["Service"]["deleteCheckpointRefs"] = Effect.fn(
131 "deleteCheckpointRefs",
132 )(function* (input) {
133 const checkpoints = yield* resolveCheckpoints(input.cwd);
134 return yield* checkpoints.deleteCheckpointRefs(input);
135 });
136​
137 return CheckpointStore.of({
138 isGitRepository,
139 captureCheckpoint,
140 hasCheckpointRef,
141 restoreCheckpoint,
142 diffCheckpoints,
143 listAuthoredPaths,
144 deleteCheckpointRefs,
145 });
146});
147​
148export const layer = Layer.effect(CheckpointStore, make);