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

logicalProject.ts

157 lines4.9 KB

1import { scopedProjectKey, scopeProjectRef } from "@workbench/client-runtime";
2import type { ScopedProjectRef, SidebarProjectGroupingMode } from "@workbench/contracts";
3import { normalizeProjectPathForComparison } from "./lib/projectPaths";
4import type { Project } from "./types";
5​
6export interface ProjectGroupingSettings {
7 sidebarProjectGroupingMode: SidebarProjectGroupingMode;
8 sidebarProjectGroupingOverrides: Record<string, SidebarProjectGroupingMode>;
9}
10​
11export type ProjectGroupingMode = SidebarProjectGroupingMode;
12​
13function uniqueNonEmptyValues(values: ReadonlyArray<string | null | undefined>): string[] {
14 const seen = new Set<string>();
15 const unique: string[] = [];
16 for (const value of values) {
17 const trimmed = value?.trim();
18 if (!trimmed || seen.has(trimmed)) {
19 continue;
20 }
21 seen.add(trimmed);
22 unique.push(trimmed);
23 }
24 return unique;
26​
27function deriveRepositoryRelativeProjectPath(
28 project: Pick<Project, "cwd" | "repositoryIdentity">,
29): string | null {
30 const rootPath = project.repositoryIdentity?.rootPath?.trim();
31 if (!rootPath) {
32 return null;
33 }
34​
35 const normalizedProjectPath = normalizeProjectPathForComparison(project.cwd);
36 const normalizedRootPath = normalizeProjectPathForComparison(rootPath);
37 if (normalizedProjectPath.length === 0 || normalizedRootPath.length === 0) {
38 return null;
39 }
40​
41 if (normalizedProjectPath === normalizedRootPath) {
42 return "";
43 }
44​
45 const separator = normalizedRootPath.includes("\\") ? "\\" : "/";
46 const rootPrefix = `${normalizedRootPath}${separator}`;
47 if (!normalizedProjectPath.startsWith(rootPrefix)) {
48 return null;
49 }
50​
51 return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/");
53​
54export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string {
55 return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`;
57​
58export function derivePhysicalProjectKey(project: Pick<Project, "environmentId" | "cwd">): string {
59 return derivePhysicalProjectKeyFromPath(project.environmentId, project.cwd);
61​
62export function deriveProjectGroupingOverrideKey(
63 project: Pick<Project, "environmentId" | "cwd">,
64): string {
65 return derivePhysicalProjectKey(project);
67​
68export function resolveProjectGroupingMode(
69 project: Pick<Project, "environmentId" | "cwd">,
70 settings: ProjectGroupingSettings,
71): SidebarProjectGroupingMode {
72 return (
73 settings.sidebarProjectGroupingOverrides?.[deriveProjectGroupingOverrideKey(project)] ??
74 settings.sidebarProjectGroupingMode
75 );
77​
78function deriveRepositoryScopedKey(
79 project: Pick<Project, "cwd" | "repositoryIdentity">,
80 groupingMode: SidebarProjectGroupingMode,
81): string | null {
82 const canonicalKey = project.repositoryIdentity?.canonicalKey;
83 if (!canonicalKey) {
84 return null;
85 }
86​
87 if (groupingMode === "repository") {
88 return canonicalKey;
89 }
90​
91 const relativeProjectPath = deriveRepositoryRelativeProjectPath(project);
92 if (relativeProjectPath === null) {
93 return canonicalKey;
94 }
95​
96 return relativeProjectPath.length === 0
97 ? canonicalKey
98 : `${canonicalKey}::${relativeProjectPath}`;
100​
101export function deriveLogicalProjectKey(
102 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">,
103 options?: {
104 groupingMode?: SidebarProjectGroupingMode;
105 },
106): string {
107 const groupingMode = options?.groupingMode ?? "repository";
108 if (groupingMode === "separate") {
109 return derivePhysicalProjectKey(project);
110 }
111​
112 return (
113 deriveRepositoryScopedKey(project, groupingMode) ??
114 derivePhysicalProjectKey(project) ??
115 scopedProjectKey(scopeProjectRef(project.environmentId, project.id))
116 );
118​
119export function deriveLogicalProjectKeyFromSettings(
120 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">,
121 settings: ProjectGroupingSettings,
122): string {
123 return deriveLogicalProjectKey(project, {
124 groupingMode: resolveProjectGroupingMode(project, settings),
125 });
127​
128export function deriveLogicalProjectKeyFromRef(
129 projectRef: ScopedProjectRef,
130 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity"> | null | undefined,
131 options?: {
132 groupingMode?: SidebarProjectGroupingMode;
133 },
134): string {
135 return project ? deriveLogicalProjectKey(project, options) : scopedProjectKey(projectRef);
137​
138export function deriveProjectGroupLabel(input: {
139 representative: Pick<Project, "name" | "repositoryIdentity">;
140 members: ReadonlyArray<Pick<Project, "name" | "repositoryIdentity">>;
141}): string {
142 const sharedDisplayNames = uniqueNonEmptyValues(
143 input.members.map((member) => member.repositoryIdentity?.displayName),
144 );
145 if (sharedDisplayNames.length === 1) {
146 return sharedDisplayNames[0]!;
147 }
148​
149 const sharedRepositoryNames = uniqueNonEmptyValues(
150 input.members.map((member) => member.repositoryIdentity?.name),
151 );
152 if (sharedRepositoryNames.length === 1) {
153 return sharedRepositoryNames[0]!;
154 }
155​
156 return input.representative.name;