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 Option from "effect/Option";
6import * as Path from "effect/Path";
7import * as Schema from "effect/Schema";
8
9import { VcsDriverKind, type VcsDriverKind as VcsDriverKindType } from "@t3tools/contracts";
10import { fromLenientJson } from "@t3tools/shared/schemaJson";
11
12const ProjectVcsConfig = Schema.Struct({
13 vcs: Schema.optional(
14 Schema.Struct({
15 kind: Schema.optional(VcsDriverKind),
16 }),
17 ),
18 vcsKind: Schema.optional(VcsDriverKind),
19});
20const ProjectVcsConfigJson = fromLenientJson(ProjectVcsConfig);
21const decodeProjectVcsConfigJson = Schema.decodeUnknownEffect(ProjectVcsConfigJson);
22
23type ProjectVcsConfigFile = typeof ProjectVcsConfig.Type;
24
25export interface VcsProjectConfigResolveInput {
26 readonly cwd: string;
27 readonly requestedKind?: VcsDriverKindType | "auto";
28}
29
30export class VcsProjectConfigError extends Schema.TaggedErrorClass<VcsProjectConfigError>()(
31 "VcsProjectConfigError",
32 {
33 operation: Schema.Literals(["inspect", "read", "decode"]),
34 cwd: Schema.String,
35 configPath: Schema.String,
36 cause: Schema.Defect(),
37 },
38) {
39 override get message(): string {
40 return `Failed to ${this.operation} VCS project config at ${this.configPath}.`;
41 }
42}
43
44export class VcsProjectConfig extends Context.Service<
45 VcsProjectConfig,
46 {
47 readonly resolveKind: (
48 input: VcsProjectConfigResolveInput,
49 ) => Effect.Effect<VcsDriverKindType | "auto">;
50 }
51>()("t3/vcs/VcsProjectConfig") {}
52
53function configuredKind(config: ProjectVcsConfigFile): VcsDriverKindType | "auto" {
54 return config.vcs?.kind ?? config.vcsKind ?? "auto";
55}
56
57const logVcsProjectConfigError = (error: VcsProjectConfigError) =>
58 Effect.logWarning(error).pipe(
59 Effect.annotateLogs({
60 operation: error.operation,
61 cwd: error.cwd,
62 configPath: error.configPath,
63 errorTag: error._tag,
64 }),
65 );
66
67export const make = Effect.gen(function* () {
68 const fileSystem = yield* FileSystem.FileSystem;
69 const path = yield* Path.Path;
70
71 const findConfigPath = Effect.fn("VcsProjectConfig.findConfigPath")(function* (cwd: string) {
72 let current = cwd;
73 while (true) {
74 const candidate = path.join(current, ".t3code", "vcs.json");
75 const exists = yield* fileSystem.exists(candidate).pipe(
76 Effect.mapError(
77 (cause) =>
78 new VcsProjectConfigError({
79 operation: "inspect",
80 cwd,
81 configPath: candidate,
82 cause,
83 }),
84 ),
85 Effect.catchTags({
86 VcsProjectConfigError: (error) => logVcsProjectConfigError(error).pipe(Effect.as(false)),
87 }),
88 );
89 if (exists) {
90 return Option.some(candidate);
91 }
92
93 const parent = path.dirname(current);
94 if (parent === current) {
95 return Option.none();
96 }
97 current = parent;
98 }
99 });
100
101 const readConfiguredKind = Effect.fn("VcsProjectConfig.readConfiguredKind")(function* (
102 cwd: string,
103 configPath: string,
104 ) {
105 const raw = yield* fileSystem.readFileString(configPath).pipe(
106 Effect.mapError(
107 (cause) =>
108 new VcsProjectConfigError({
109 operation: "read",
110 cwd,
111 configPath,
112 cause,
113 }),
114 ),
115 );
116 const parsed = yield* decodeProjectVcsConfigJson(raw).pipe(
117 Effect.mapError(
118 (cause) =>
119 new VcsProjectConfigError({
120 operation: "decode",
121 cwd,
122 configPath,
123 cause,
124 }),
125 ),
126 );
127 return configuredKind(parsed);
128 });
129
130 const resolveKind: VcsProjectConfig["Service"]["resolveKind"] = Effect.fn(
131 "VcsProjectConfig.resolveKind",
132 )(function* (input) {
133 if (input.requestedKind !== undefined && input.requestedKind !== "auto") {
134 return input.requestedKind;
135 }
136
137 return yield* findConfigPath(input.cwd).pipe(
138 Effect.flatMap(
139 Option.match({
140 onNone: () => Effect.succeed("auto" as const),
141 onSome: (configPath) => readConfiguredKind(input.cwd, configPath),
142 }),
143 ),
144 Effect.catchTags({
145 VcsProjectConfigError: (error) =>
146 logVcsProjectConfigError(error).pipe(Effect.as("auto" as const)),
147 }),
148 );
149 });
150
151 return VcsProjectConfig.of({
152 resolveKind,
153 });
154});
155
156export const layer = Layer.effect(VcsProjectConfig, make);