|
| 1 | +/* jscpd:ignore-start */ |
| 2 | +import type * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 3 | +import type { PlatformError } from "@effect/platform/Error" |
| 4 | +import type * as FileSystem from "@effect/platform/FileSystem" |
| 5 | +import type * as Path from "@effect/platform/Path" |
| 6 | +import { Effect } from "effect" |
| 7 | + |
| 8 | +import type { TemplateConfig } from "../../core/domain.js" |
| 9 | +import { resolveComposeProjectName, resolveProjectBootstrapVolumeName } from "../../core/domain.js" |
| 10 | +import { type DockerCommandError, DockerIdentityConflictError } from "../../shell/errors.js" |
| 11 | +import type { ProjectStatus } from "../projects-core.js" |
| 12 | +import { loadProjectIndex, loadProjectStatus } from "../projects-core.js" |
| 13 | +import { deleteDockerGitProject } from "../projects-delete.js" |
| 14 | + |
| 15 | +type CreateProjectRuntime = FileSystem.FileSystem | Path.Path | CommandExecutor.CommandExecutor |
| 16 | + |
| 17 | +type DockerIdentityOwner = Pick< |
| 18 | + TemplateConfig, |
| 19 | + "containerName" | "serviceName" | "volumeName" | "enableMcpPlaywright" |
| 20 | +> |
| 21 | + |
| 22 | +type DockerIdentityNamespace = "container" | "composeProject" | "volume" |
| 23 | + |
| 24 | +type DockerIdentityClaim = Omit<DockerIdentityConflictError["conflicts"][number], "conflictingProjectDir"> & { |
| 25 | + readonly namespace: DockerIdentityNamespace |
| 26 | +} |
| 27 | + |
| 28 | +type ConflictState = { |
| 29 | + readonly conflicts: Array<DockerIdentityConflictError["conflicts"][number]> |
| 30 | + readonly conflictingProjects: Map< |
| 31 | + string, |
| 32 | + { |
| 33 | + readonly projectDir: string |
| 34 | + readonly repoUrl: string |
| 35 | + readonly containerName: string |
| 36 | + readonly serviceName: string |
| 37 | + } |
| 38 | + > |
| 39 | +} |
| 40 | + |
| 41 | +const resolveBrowserContainerClaims = ( |
| 42 | + config: DockerIdentityOwner |
| 43 | +): ReadonlyArray<DockerIdentityClaim> => |
| 44 | + config.enableMcpPlaywright |
| 45 | + ? [{ namespace: "container", kind: "browserContainerName", name: `${config.containerName}-browser` }] |
| 46 | + : [] |
| 47 | + |
| 48 | +const resolveBrowserVolumeClaims = ( |
| 49 | + config: DockerIdentityOwner |
| 50 | +): ReadonlyArray<DockerIdentityClaim> => |
| 51 | + config.enableMcpPlaywright |
| 52 | + ? [{ namespace: "volume", kind: "browserVolumeName", name: `${config.volumeName}-browser` }] |
| 53 | + : [] |
| 54 | + |
| 55 | +const resolveDockerIdentityClaims = ( |
| 56 | + config: DockerIdentityOwner |
| 57 | +): ReadonlyArray<DockerIdentityClaim> => [ |
| 58 | + { namespace: "container", kind: "containerName", name: config.containerName }, |
| 59 | + ...resolveBrowserContainerClaims(config), |
| 60 | + { namespace: "composeProject", kind: "serviceName", name: resolveComposeProjectName(config) }, |
| 61 | + { namespace: "volume", kind: "volumeName", name: config.volumeName }, |
| 62 | + ...resolveBrowserVolumeClaims(config), |
| 63 | + { namespace: "volume", kind: "bootstrapVolumeName", name: resolveProjectBootstrapVolumeName(config) } |
| 64 | +] |
| 65 | + |
| 66 | +const loadProjectStatusOrNull = (configPath: string) => |
| 67 | + loadProjectStatus(configPath).pipe( |
| 68 | + Effect.match({ |
| 69 | + onFailure: () => null, |
| 70 | + onSuccess: (value) => value |
| 71 | + }) |
| 72 | + ) |
| 73 | + |
| 74 | +const collectSharedClaims = ( |
| 75 | + candidateClaims: ReadonlyArray<DockerIdentityClaim>, |
| 76 | + existingClaims: ReadonlyArray<DockerIdentityClaim>, |
| 77 | + projectDir: string |
| 78 | +): ReadonlyArray<DockerIdentityConflictError["conflicts"][number]> => |
| 79 | + candidateClaims.flatMap((candidate) => |
| 80 | + existingClaims.some( |
| 81 | + (existing) => existing.namespace === candidate.namespace && existing.name === candidate.name |
| 82 | + ) |
| 83 | + ? [{ conflictingProjectDir: projectDir, kind: candidate.kind, name: candidate.name }] |
| 84 | + : [] |
| 85 | + ) |
| 86 | + |
| 87 | +const appendClaims = ( |
| 88 | + conflicts: Array<DockerIdentityConflictError["conflicts"][number]>, |
| 89 | + sharedClaims: ReadonlyArray<DockerIdentityConflictError["conflicts"][number]> |
| 90 | +): void => { |
| 91 | + for (const claim of sharedClaims) { |
| 92 | + conflicts.push(claim) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +const rememberConflictingProject = ( |
| 97 | + conflictingProjects: ConflictState["conflictingProjects"], |
| 98 | + status: ProjectStatus |
| 99 | +): void => { |
| 100 | + conflictingProjects.set(status.projectDir, { |
| 101 | + projectDir: status.projectDir, |
| 102 | + repoUrl: status.config.template.repoUrl, |
| 103 | + containerName: status.config.template.containerName, |
| 104 | + serviceName: status.config.template.serviceName |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +const scanConflicts = ( |
| 109 | + resolvedOutDir: string, |
| 110 | + config: DockerIdentityOwner |
| 111 | +): Effect.Effect<ConflictState | null, PlatformError, CreateProjectRuntime> => |
| 112 | + Effect.gen(function*(_) { |
| 113 | + const index = yield* _(loadProjectIndex()) |
| 114 | + if (index === null) { |
| 115 | + return null |
| 116 | + } |
| 117 | + |
| 118 | + const candidateClaims = resolveDockerIdentityClaims(config) |
| 119 | + const state: ConflictState = { |
| 120 | + conflicts: [], |
| 121 | + conflictingProjects: new Map() |
| 122 | + } |
| 123 | + |
| 124 | + for (const configPath of index.configPaths) { |
| 125 | + const status = yield* _(loadProjectStatusOrNull(configPath)) |
| 126 | + if (status === null || status.projectDir === resolvedOutDir) { |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + const sharedClaims = collectSharedClaims( |
| 131 | + candidateClaims, |
| 132 | + resolveDockerIdentityClaims(status.config.template), |
| 133 | + status.projectDir |
| 134 | + ) |
| 135 | + if (sharedClaims.length === 0) { |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + appendClaims(state.conflicts, sharedClaims) |
| 140 | + rememberConflictingProject(state.conflictingProjects, status) |
| 141 | + } |
| 142 | + |
| 143 | + return state |
| 144 | + }) |
| 145 | + |
| 146 | +const deleteConflictingProjects = ( |
| 147 | + conflictingProjects: ConflictState["conflictingProjects"] |
| 148 | +): Effect.Effect<void, DockerCommandError | PlatformError, CreateProjectRuntime> => |
| 149 | + Effect.gen(function*(_) { |
| 150 | + for (const conflictingProject of conflictingProjects.values()) { |
| 151 | + yield* _( |
| 152 | + Effect.logWarning( |
| 153 | + `Force enabled: replacing conflicting docker-git project ${conflictingProject.projectDir}` |
| 154 | + ) |
| 155 | + ) |
| 156 | + yield* _(deleteDockerGitProject(conflictingProject)) |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | +export const deleteConflictingProjectsIfNeeded = ( |
| 161 | + resolvedOutDir: string, |
| 162 | + config: DockerIdentityOwner, |
| 163 | + force: boolean |
| 164 | +): Effect.Effect<void, DockerIdentityConflictError | PlatformError | DockerCommandError, CreateProjectRuntime> => |
| 165 | + Effect.gen(function*(_) { |
| 166 | + const state = yield* _(scanConflicts(resolvedOutDir, config)) |
| 167 | + if (state === null || state.conflicts.length === 0) { |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + if (!force) { |
| 172 | + return yield* _( |
| 173 | + Effect.fail(new DockerIdentityConflictError({ projectDir: resolvedOutDir, conflicts: state.conflicts })) |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + yield* _(deleteConflictingProjects(state.conflictingProjects)) |
| 178 | + }) |
| 179 | +/* jscpd:ignore-end */ |
0 commit comments