Open History to see the last commit
1import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
2
3import type { VcsDriverKind, VcsError, VcsRepositoryIdentity } from "@t3tools/contracts";
4import { VcsUnsupportedOperationError } from "@t3tools/contracts";
5import * as GitVcsDriver from "./GitVcsDriver.ts";
6import * as VcsProjectConfig from "./VcsProjectConfig.ts";
7import * as VcsDriver from "./VcsDriver.ts";
8
9const DETECTION_CACHE_CAPACITY = 2_048;
10const DETECTION_CACHE_TTL = Duration.seconds(2);
11
12export interface VcsDriverResolveInput {
13 readonly cwd: string;
14 readonly requestedKind?: VcsDriverKind | "auto";
15}
16
17export interface VcsDriverHandle {
18 readonly kind: VcsDriverKind;
19 readonly repository: VcsRepositoryIdentity;
20 readonly driver: VcsDriver.VcsDriverShape;
21}
22
23export interface VcsDriverRegistryShape {
24 readonly get: (kind: VcsDriverKind) => Effect.Effect<VcsDriver.VcsDriverShape, VcsError>;
25 readonly detect: (
26 input: VcsDriverResolveInput,
27 ) => Effect.Effect<VcsDriverHandle | null, VcsError>;
28 readonly resolve: (input: VcsDriverResolveInput) => Effect.Effect<VcsDriverHandle, VcsError>;
29}
30
31export class VcsDriverRegistry extends Context.Service<VcsDriverRegistry, VcsDriverRegistryShape>()(
32 "t3/vcs/VcsDriverRegistry",
33) {}
34
35const unsupported = (operation: string, kind: VcsDriverKind, detail: string) =>
36 new VcsUnsupportedOperationError({
37 operation,
38 kind,
39 detail,
40 });
41
42function detectionCacheKey(input: {
43 readonly cwd: string;
44 readonly requestedKind: VcsDriverKind | "auto";
45}): string {
46 return `${input.requestedKind}\0${input.cwd}`;
47}
48
49function parseDetectionCacheKey(key: string): {
50 readonly cwd: string;
51 readonly requestedKind: VcsDriverKind | "auto";
52} {
53 const separatorIndex = key.indexOf("\0");
54 if (separatorIndex === -1) {
55 return {
56 cwd: key,
57 requestedKind: "auto",
58 };
59 }
60 return {
61 requestedKind: key.slice(0, separatorIndex) as VcsDriverKind | "auto",
62 cwd: key.slice(separatorIndex + 1),
63 };
64}
65
66export const make = Effect.fn("makeVcsDriverRegistry")(function* () {
67 const projectConfig = yield* VcsProjectConfig.VcsProjectConfig;
68 const git = yield* GitVcsDriver.makeVcsDriverShape();
69 const drivers: Partial<Record<VcsDriverKind, VcsDriver.VcsDriverShape>> = {
70 git,
71 };
72
73 const get: VcsDriverRegistryShape["get"] = (kind) => {
74 const driver = drivers[kind];
75 if (!driver) {
76 return Effect.fail(
77 unsupported("VcsDriverRegistry.get", kind, `No ${kind} VCS driver is registered.`),
78 );
79 }
80 return Effect.succeed(driver);
81 };
82
83 const detectWithDriver = Effect.fn("VcsDriverRegistry.detectWithDriver")(function* (
84 kind: VcsDriverKind,
85 driver: VcsDriver.VcsDriverShape,
86 cwd: string,
87 ) {
88 const repository = yield* driver.detectRepository(cwd);
89 if (!repository) {
90 return null;
91 }
92 return {
93 kind,
94 repository,
95 driver,
96 } satisfies VcsDriverHandle;
97 });
98
99 const detectResolvedKind = Effect.fn("VcsDriverRegistry.detectResolvedKind")(function* (input: {
100 readonly cwd: string;
101 readonly requestedKind: VcsDriverKind | "auto";
102 }) {
103 const requestedKind = input.requestedKind;
104
105 if (requestedKind !== "auto" && requestedKind !== "unknown") {
106 const driver = yield* get(requestedKind);
107 return yield* detectWithDriver(requestedKind, driver, input.cwd);
108 }
109
110 return yield* detectWithDriver("git", git, input.cwd);
111 });
112
113 const detectionCache = yield* Cache.makeWith<string, VcsDriverHandle | null, VcsError>(
114 (key) => detectResolvedKind(parseDetectionCacheKey(key)),
115 {
116 capacity: DETECTION_CACHE_CAPACITY,
117 timeToLive: (exit) => (Exit.isSuccess(exit) ? DETECTION_CACHE_TTL : Duration.zero),
118 },
119 );
120
121 const detect: VcsDriverRegistryShape["detect"] = Effect.fn("VcsDriverRegistry.detect")(
122 function* (input) {
123 const requestedKind = yield* projectConfig.resolveKind(input);
124 return yield* Cache.get(detectionCache, detectionCacheKey({ cwd: input.cwd, requestedKind }));
125 },
126 );
127
128 const resolve: VcsDriverRegistryShape["resolve"] = Effect.fn("VcsDriverRegistry.resolve")(
129 function* (input) {
130 const detected = yield* detect(input);
131 if (detected) {
132 return detected;
133 }
134
135 const requestedKind = input.requestedKind ?? "auto";
136 return yield* unsupported(
137 "VcsDriverRegistry.resolve",
138 requestedKind === "auto" ? "unknown" : requestedKind,
139 requestedKind === "auto"
140 ? `No supported VCS repository was detected at ${input.cwd}.`
141 : `No ${requestedKind} repository was detected at ${input.cwd}.`,
142 );
143 },
144 );
145
146 return VcsDriverRegistry.of({
147 get,
148 detect,
149 resolve,
150 });
151});
152
153export const layer = Layer.effect(VcsDriverRegistry, make()).pipe(
154 Layer.provide(VcsProjectConfig.layer),
155);