Open History to see the last commit
1import { Context, Effect, FileSystem, Layer, Path, Schema } from "effect";
2
3import { VcsDriverKind, type VcsDriverKind as VcsDriverKindType } from "@t3tools/contracts";
4
5const ProjectVcsConfig = Schema.Struct({
6 vcs: Schema.optional(
7 Schema.Struct({
8 kind: Schema.optional(VcsDriverKind),
9 }),
10 ),
11 vcsKind: Schema.optional(VcsDriverKind),
12});
13
14interface ProjectVcsConfigFile {
15 readonly vcs?:
16 | {
17 readonly kind?: VcsDriverKindType | undefined;
18 }
19 | undefined;
20 readonly vcsKind?: VcsDriverKindType | undefined;
21}
22
23export interface VcsProjectConfigResolveInput {
24 readonly cwd: string;
25 readonly requestedKind?: VcsDriverKindType | "auto";
26}
27
28export interface VcsProjectConfigShape {
29 readonly resolveKind: (
30 input: VcsProjectConfigResolveInput,
31 ) => Effect.Effect<VcsDriverKindType | "auto">;
32}
33
34export class VcsProjectConfig extends Context.Service<VcsProjectConfig, VcsProjectConfigShape>()(
35 "t3/vcs/VcsProjectConfig",
36) {}
37
38function configuredKind(config: ProjectVcsConfigFile): VcsDriverKindType | "auto" {
39 return config.vcs?.kind ?? config.vcsKind ?? "auto";
40}
41
42function parseConfig(raw: string): ProjectVcsConfigFile | null {
43 try {
44 const parsed = JSON.parse(raw) as unknown;
45 return Schema.is(ProjectVcsConfig)(parsed) ? parsed : null;
46 } catch {
47 return null;
48 }
49}
50
51export const make = Effect.fn("makeVcsProjectConfig")(function* () {
52 const fileSystem = yield* FileSystem.FileSystem;
53 const path = yield* Path.Path;
54
55 const findConfigPath = Effect.fn("VcsProjectConfig.findConfigPath")(function* (cwd: string) {
56 let current = cwd;
57 while (true) {
58 const candidate = path.join(current, ".t3code", "vcs.json");
59 if (yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))) {
60 return candidate;
61 }
62
63 const parent = path.dirname(current);
64 if (parent === current) {
65 return null;
66 }
67 current = parent;
68 }
69 });
70
71 const readConfiguredKind = Effect.fn("VcsProjectConfig.readConfiguredKind")(function* (
72 configPath: string,
73 ) {
74 const raw = yield* fileSystem.readFileString(configPath).pipe(
75 Effect.catch((error) =>
76 Effect.logWarning("failed to read VCS project config", {
77 configPath,
78 error,
79 }).pipe(Effect.as(null)),
80 ),
81 );
82 if (raw === null) {
83 return "auto" as const;
84 }
85
86 const parsed = parseConfig(raw);
87 if (parsed === null) {
88 yield* Effect.logWarning("invalid VCS project config", {
89 configPath,
90 });
91 return "auto" as const;
92 }
93
94 return configuredKind(parsed);
95 });
96
97 const resolveKind: VcsProjectConfigShape["resolveKind"] = Effect.fn(
98 "VcsProjectConfig.resolveKind",
99 )(function* (input) {
100 if (input.requestedKind !== undefined && input.requestedKind !== "auto") {
101 return input.requestedKind;
102 }
103
104 const configPath = yield* findConfigPath(input.cwd);
105 if (configPath === null) {
106 return "auto";
107 }
108
109 return yield* readConfiguredKind(configPath);
110 });
111
112 return VcsProjectConfig.of({
113 resolveKind,
114 });
115});
116
117export const layer = Layer.effect(VcsProjectConfig, make());