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

VcsDriverRegistry.ts

160 lines4.9 KB

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";
21​
22export interface VcsDriverHandle {
23 readonly kind: VcsDriverKind;
24 readonly repository: VcsRepositoryIdentity;
25 readonly driver: VcsDriver.VcsDriver["Service"];
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}`;
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 };
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.match({
119 onSuccess: (detected) => (detected === null ? Duration.zero : DETECTION_CACHE_TTL),
120 onFailure: () => Duration.zero,
121 }),
122 },
123 );
124​
125 const detect: VcsDriverRegistry["Service"]["detect"] = Effect.fn("VcsDriverRegistry.detect")(
126 function* (input) {
127 const requestedKind = yield* projectConfig.resolveKind(input);
128 return yield* Cache.get(detectionCache, detectionCacheKey({ cwd: input.cwd, requestedKind }));
129 },
130 );
131​
132 const resolve: VcsDriverRegistry["Service"]["resolve"] = Effect.fn("VcsDriverRegistry.resolve")(
133 function* (input) {
134 const detected = yield* detect(input);
135 if (detected) {
136 return detected;
137 }
138​
139 const requestedKind = input.requestedKind ?? "auto";
140 return yield* new VcsUnsupportedOperationError({
141 operation: "VcsDriverRegistry.resolve",
142 kind: requestedKind === "auto" ? "unknown" : requestedKind,
143 detail:
144 requestedKind === "auto"
145 ? `No supported VCS repository was detected at ${input.cwd}.`
146 : `No ${requestedKind} repository was detected at ${input.cwd}.`,
147 });
148 },
149 );
150​
151 return VcsDriverRegistry.of({
152 get,
153 detect,
154 resolve,
155 });
156});
157​
158export const layer = Layer.effect(VcsDriverRegistry, make).pipe(
159 Layer.provide(VcsProjectConfig.layer),
160);