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

projectGrouping.ts

183 lines5.7 KB

1import { scopedProjectKey, scopeProjectRef } from "../environment/scoped.ts";
2import type { ScopedProjectRef, SidebarProjectGroupingMode } from "@t3tools/contracts";
3import type { ClientSettings } from "@t3tools/contracts/settings";
4​
5import type { EnvironmentProject } from "./models.ts";
6import { normalizeProjectPathForComparison } from "./projects.ts";
7​
8export interface ProjectGroupingSettings {
9 readonly sidebarProjectGroupingMode: SidebarProjectGroupingMode;
10 readonly sidebarProjectGroupingOverrides: Record<string, SidebarProjectGroupingMode>;
12​
13export type ProjectGroupingMode = SidebarProjectGroupingMode;
14​
15export function selectProjectGroupingSettings(settings: ClientSettings): ProjectGroupingSettings {
16 return {
17 sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
18 sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
19 };
21​
22function uniqueNonEmptyValues(values: ReadonlyArray<string | null | undefined>): string[] {
23 const seen = new Set<string>();
24 const unique: string[] = [];
25 for (const value of values) {
26 const trimmed = value?.trim();
27 if (!trimmed || seen.has(trimmed)) {
28 continue;
29 }
30 seen.add(trimmed);
31 unique.push(trimmed);
32 }
33 return unique;
35​
36function deriveRepositoryRelativeProjectPath(
37 project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">,
38): string | null {
39 const rootPath = project.repositoryIdentity?.rootPath?.trim();
40 if (!rootPath) {
41 return null;
42 }
43​
44 const normalizedProjectPath = normalizeProjectPathForComparison(project.workspaceRoot);
45 const normalizedRootPath = normalizeProjectPathForComparison(rootPath);
46 if (normalizedProjectPath.length === 0 || normalizedRootPath.length === 0) {
47 return null;
48 }
49​
50 if (normalizedProjectPath === normalizedRootPath) {
51 return "";
52 }
53​
54 const separator = normalizedRootPath.includes("\\") ? "\\" : "/";
55 const rootPrefix = `${normalizedRootPath}${separator}`;
56 if (!normalizedProjectPath.startsWith(rootPrefix)) {
57 return null;
58 }
59​
60 return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/");
62​
63export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string {
64 return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`;
66​
67export function derivePhysicalProjectKey(
68 project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
69): string {
70 return derivePhysicalProjectKeyFromPath(project.environmentId, project.workspaceRoot);
72​
73export function deriveProjectGroupingOverrideKey(
74 project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
75): string {
76 return derivePhysicalProjectKey(project);
78​
79export function getProjectOrderKey(
80 project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
81): string {
82 return derivePhysicalProjectKey(project);
84​
85export function resolveProjectGroupingMode(
86 project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
87 settings: ProjectGroupingSettings,
88): SidebarProjectGroupingMode {
89 return (
90 settings.sidebarProjectGroupingOverrides?.[deriveProjectGroupingOverrideKey(project)] ??
91 settings.sidebarProjectGroupingMode
92 );
94​
95function deriveRepositoryScopedKey(
96 project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">,
97 groupingMode: SidebarProjectGroupingMode,
98): string | null {
99 const canonicalKey = project.repositoryIdentity?.canonicalKey;
100 if (!canonicalKey) {
101 return null;
102 }
103​
104 if (groupingMode === "repository") {
105 return canonicalKey;
106 }
107​
108 const relativeProjectPath = deriveRepositoryRelativeProjectPath(project);
109 if (relativeProjectPath === null) {
110 return canonicalKey;
111 }
112​
113 return relativeProjectPath.length === 0
114 ? canonicalKey
115 : `${canonicalKey}::${relativeProjectPath}`;
117​
118export function deriveLogicalProjectKey(
119 project: Pick<
120 EnvironmentProject,
121 "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity"
122 >,
123 options?: {
124 readonly groupingMode?: SidebarProjectGroupingMode;
125 },
126): string {
127 const groupingMode = options?.groupingMode ?? "repository";
128 if (groupingMode === "separate") {
129 return derivePhysicalProjectKey(project);
130 }
131​
132 return (
133 deriveRepositoryScopedKey(project, groupingMode) ??
134 derivePhysicalProjectKey(project) ??
135 scopedProjectKey(scopeProjectRef(project.environmentId, project.id))
136 );
138​
139export function deriveLogicalProjectKeyFromSettings(
140 project: Pick<
141 EnvironmentProject,
142 "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity"
143 >,
144 settings: ProjectGroupingSettings,
145): string {
146 return deriveLogicalProjectKey(project, {
147 groupingMode: resolveProjectGroupingMode(project, settings),
148 });
150​
151export function deriveLogicalProjectKeyFromRef(
152 projectRef: ScopedProjectRef,
153 project:
154 | Pick<EnvironmentProject, "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity">
155 | null
156 | undefined,
157 options?: {
158 readonly groupingMode?: SidebarProjectGroupingMode;
159 },
160): string {
161 return project ? deriveLogicalProjectKey(project, options) : scopedProjectKey(projectRef);
163​
164export function deriveProjectGroupLabel(input: {
165 readonly representative: Pick<EnvironmentProject, "title" | "repositoryIdentity">;
166 readonly members: ReadonlyArray<Pick<EnvironmentProject, "title" | "repositoryIdentity">>;
167}): string {
168 const sharedDisplayNames = uniqueNonEmptyValues(
169 input.members.map((member) => member.repositoryIdentity?.displayName),
170 );
171 if (sharedDisplayNames.length === 1) {
172 return sharedDisplayNames[0]!;
173 }
174​
175 const sharedRepositoryNames = uniqueNonEmptyValues(
176 input.members.map((member) => member.repositoryIdentity?.name),
177 );
178 if (sharedRepositoryNames.length === 1) {
179 return sharedRepositoryNames[0]!;
180 }
181​
182 return input.representative.title;