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