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

projectWorktreeRoot.ts

49 lines2.1 KB

1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import type * as Path from "effect/Path";
4​
5import type { ProjectionSnapshotQueryShape } from "../orchestration/Services/ProjectionSnapshotQuery.ts";
6import { expandHomePathWith } from "../pathExpansion.ts";
7​
8/**
9 * Build a resolver for the absolute directory a project creates its new
10 * worktrees in, or `undefined` when the server's own worktrees directory
11 * applies. Services come in already resolved so callers can use it from a
12 * service closure without widening their own requirements.
13 */
14export const makeProjectWorktreeRootResolver = (services: {
15 readonly projectionSnapshotQuery: ProjectionSnapshotQueryShape;
16 readonly path: Path.Path;
17}) =>
18 Effect.fn("resolveProjectWorktreeRoot")(function* (workspaceRoot: string) {
19 const project = yield* services.projectionSnapshotQuery
20 .getActiveProjectByWorkspaceRoot(workspaceRoot)
21 .pipe(
22 // Losing the preferred location is recoverable; losing the thread is not.
23 Effect.catch((cause) =>
24 Effect.logWarning("failed to read project worktree root; using the default location", {
25 workspaceRoot,
26 cause,
27 }).pipe(Effect.as(Option.none())),
28 ),
29 );
30​
31 const configured = Option.getOrUndefined(project)?.worktreeRoot;
32 if (configured == null) return undefined;
33​
34 // The contract accepts every platform's absolute form, because any client
35 // can edit a project it does not host. Only this server knows which of
36 // those forms its own path module understands: a Windows root reaching a
37 // POSIX server would resolve under the server's cwd rather than fail, so
38 // the default location is the safer answer.
39 const expanded = expandHomePathWith(configured, services.path);
40 if (!services.path.isAbsolute(expanded)) {
41 yield* Effect.logWarning(
42 "project worktree location is not absolute on this server; using the default location",
43 { workspaceRoot, worktreeRoot: configured },
44 );
45 return undefined;
46 }
47​
48 return services.path.resolve(expanded);
49 });