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