Open History to see the last commit
1import type {
2 CommandId,
3 EnvironmentId,
4 OrchestrationCommand,
5 ProjectId,
6 SourceControlDiscoveryResult,
7 SourceControlProviderKind,
8 SourceControlRepositoryInfo,
9} from "@t3tools/contracts";
10import * as Arr from "effect/Array";
11import * as Option from "effect/Option";
12import * as Order from "effect/Order";
13
14import {
15 ensureBrowseDirectoryPath,
16 findProjectByPath,
17 inferProjectTitleFromPath,
18 isExplicitRelativeProjectPath,
19 isUnsupportedWindowsProjectPath,
20 resolveProjectPathForDispatch,
21} from "../state/projects.ts";
22import type { EnvironmentProject } from "../state/models.ts";
23
24export type AddProjectRemoteProviderKind = Extract<
25 SourceControlProviderKind,
26 "github" | "gitlab" | "bitbucket" | "azure-devops"
27>;
28export type AddProjectRemoteSource = AddProjectRemoteProviderKind | "url";
29
30export type AddProjectRemoteSourceReadiness = Record<
31 AddProjectRemoteSource,
32 { readonly ready: boolean; readonly hint: string | null }
33>;
34
35export type AddProjectCloneFlow =
36 | {
37 readonly step: "repository";
38 readonly environmentId: EnvironmentId;
39 readonly source: AddProjectRemoteSource;
40 }
41 | {
42 readonly step: "confirm";
43 readonly environmentId: EnvironmentId;
44 readonly source: AddProjectRemoteSource;
45 readonly repositoryInput: string;
46 readonly repository: SourceControlRepositoryInfo | null;
47 readonly remoteUrl: string;
48 };
49
50const ADD_PROJECT_REMOTE_SOURCES: ReadonlyArray<AddProjectRemoteSource> = [
51 "url",
52 "github",
53 "gitlab",
54 "bitbucket",
55 "azure-devops",
56];
57
58const ADD_PROJECT_REMOTE_PROVIDER_SOURCES: ReadonlyArray<AddProjectRemoteProviderKind> = [
59 "github",
60 "gitlab",
61 "bitbucket",
62 "azure-devops",
63];
64
65export function addProjectRemoteSourceLabel(source: AddProjectRemoteSource): string {
66 switch (source) {
67 case "github":
68 return "GitHub";
69 case "gitlab":
70 return "GitLab";
71 case "bitbucket":
72 return "Bitbucket";
73 case "azure-devops":
74 return "Azure DevOps";
75 case "url":
76 return "Git URL";
77 }
78}
79
80export function addProjectRemoteSourcePathHint(source: AddProjectRemoteSource): string {
81 switch (source) {
82 case "github":
83 return "owner/repo";
84 case "gitlab":
85 return "group/project";
86 case "bitbucket":
87 return "workspace/repository";
88 case "azure-devops":
89 return "project/repository";
90 case "url":
91 return "URL";
92 }
93}
94
95export function addProjectRemoteSourceProvider(
96 source: AddProjectRemoteSource,
97): AddProjectRemoteProviderKind | null {
98 return source === "url" ? null : source;
99}
100
101export function sortAddProjectProviderSources(
102 readinessBySource: AddProjectRemoteSourceReadiness,
103): ReadonlyArray<AddProjectRemoteProviderKind> {
104 return Arr.sort(
105 ADD_PROJECT_REMOTE_PROVIDER_SOURCES,
106 Order.mapInput(
107 Order.Struct({
108 ready: Order.flip(Order.Boolean),
109 label: Order.String,
110 }),
111 (source: AddProjectRemoteProviderKind) => ({
112 ready: readinessBySource[source].ready,
113 label: addProjectRemoteSourceLabel(source),
114 }),
115 ),
116 );
117}
118
119export function buildAddProjectRemoteSourceReadiness(
120 discovery: SourceControlDiscoveryResult | null,
121): AddProjectRemoteSourceReadiness {
122 const unavailable = {
123 ready: false,
124 hint: "Provider status unavailable. Open Source Control settings and rescan.",
125 } as const;
126 const readiness: AddProjectRemoteSourceReadiness = {
127 url: { ready: true, hint: null },
128 github: unavailable,
129 gitlab: unavailable,
130 bitbucket: unavailable,
131 "azure-devops": unavailable,
132 };
133
134 if (!discovery) {
135 return readiness;
136 }
137
138 const providerByKind = new Map(
139 discovery.sourceControlProviders.map((provider) => [provider.kind, provider]),
140 );
141 for (const source of ADD_PROJECT_REMOTE_SOURCES) {
142 const kind = addProjectRemoteSourceProvider(source);
143 if (!kind) continue;
144 const provider = providerByKind.get(kind);
145 if (!provider) {
146 readiness[source] = unavailable;
147 continue;
148 }
149 if (provider.status !== "available") {
150 readiness[source] = { ready: false, hint: provider.installHint };
151 continue;
152 }
153 if (provider.auth.status === "unauthenticated") {
154 readiness[source] = {
155 ready: false,
156 hint:
157 Option.getOrNull(provider.auth.detail) ??
158 `${provider.label} is not authenticated. Open Source Control settings for setup guidance.`,
159 };
160 continue;
161 }
162 readiness[source] = { ready: true, hint: null };
163 }
164 return readiness;
165}
166
167export function getAddProjectInitialQuery(baseDirectory: string | null | undefined): string {
168 const trimmed = baseDirectory?.trim() ?? "";
169 return trimmed.length === 0 ? "~/" : ensureBrowseDirectoryPath(trimmed);
170}
171
172export function resolveAddProjectPath(input: {
173 readonly rawPath: string;
174 readonly currentProjectCwd?: string | null;
175 readonly platform: string;
176}): { readonly ok: true; readonly path: string } | { readonly ok: false; readonly error: string } {
177 const rawPath = input.rawPath.trim();
178 if (rawPath.length === 0) {
179 return { ok: false, error: "Enter a project path." };
180 }
181 if (isUnsupportedWindowsProjectPath(rawPath, input.platform)) {
182 return { ok: false, error: "Windows-style paths are only supported on Windows environments." };
183 }
184 if (isExplicitRelativeProjectPath(rawPath) && !input.currentProjectCwd) {
185 return { ok: false, error: "Relative paths require an active project in this environment." };
186 }
187 const path = resolveProjectPathForDispatch(rawPath, input.currentProjectCwd);
188 return path.length === 0 ? { ok: false, error: "Enter a project path." } : { ok: true, path };
189}
190
191export function findExistingAddProject(input: {
192 readonly projects: ReadonlyArray<EnvironmentProject>;
193 readonly environmentId: EnvironmentId;
194 readonly path: string;
195}): EnvironmentProject | null {
196 return (
197 findProjectByPath(
198 input.projects.filter((project) => project.environmentId === input.environmentId),
199 input.path,
200 ) ?? null
201 );
202}
203
204export function buildProjectCreateCommand(input: {
205 readonly commandId: CommandId;
206 readonly projectId: ProjectId;
207 readonly workspaceRoot: string;
208 readonly createdAt: string;
209}): Extract<OrchestrationCommand, { type: "project.create" }> {
210 return {
211 type: "project.create",
212 commandId: input.commandId,
213 projectId: input.projectId,
214 title: inferProjectTitleFromPath(input.workspaceRoot),
215 workspaceRoot: input.workspaceRoot,
216 createWorkspaceRootIfMissing: true,
217 defaultModelSelection: null,
218 createdAt: input.createdAt,
219 };
220}