( db: Database, designId: string, workspacePath: string | null, migrateFiles: boolean, workspaceMode?: WorkspaceMode, )
| 141 | } |
| 142 | |
| 143 | export async function bindWorkspace( |
| 144 | db: Database, |
| 145 | designId: string, |
| 146 | workspacePath: string | null, |
| 147 | migrateFiles: boolean, |
| 148 | workspaceMode?: WorkspaceMode, |
| 149 | ): Promise<Design> { |
| 150 | const current = requireDesign(db, designId); |
| 151 | |
| 152 | if (workspacePath === null) { |
| 153 | logger.info('workspace.clear.start', { designId }); |
| 154 | const cleared = clearDesignWorkspace(db, designId); |
| 155 | if (cleared === null) { |
| 156 | throw new Error(`Design not found: ${designId}`); |
| 157 | } |
| 158 | logger.info('workspace.clear.done', { designId }); |
| 159 | return cleared; |
| 160 | } |
| 161 | |
| 162 | const normalizedPath = normalizeWorkspacePath(workspacePath); |
| 163 | const comparisonPath = workspacePathComparisonKey(normalizedPath); |
| 164 | if ( |
| 165 | current.workspacePath !== null && |
| 166 | workspacePathComparisonKey(current.workspacePath) === comparisonPath |
| 167 | ) { |
| 168 | logger.info('workspace.bind.noop', { designId, workspacePath: normalizedPath }); |
| 169 | return current; |
| 170 | } |
| 171 | const conflict = findWorkspaceConflict(db, designId, normalizedPath); |
| 172 | if (conflict !== null) { |
| 173 | throw new Error(workspaceConflictMessage(conflict)); |
| 174 | } |
| 175 | await assertExistingWorkspaceDirectory(normalizedPath); |
| 176 | |
| 177 | logger.info('workspace.bind.start', { |
| 178 | designId, |
| 179 | workspacePath: normalizedPath, |
| 180 | migrateFiles, |
| 181 | }); |
| 182 | |
| 183 | if (migrateFiles) { |
| 184 | await migrateWorkspaceFiles(db, designId, normalizedPath); |
| 185 | } |
| 186 | |
| 187 | const updated = updateDesignWorkspace(db, designId, normalizedPath, workspaceMode); |
| 188 | if (updated === null) { |
| 189 | throw new Error(`Design not found: ${designId}`); |
| 190 | } |
| 191 | |
| 192 | logger.info('workspace.bind.done', { |
| 193 | designId, |
| 194 | workspacePath: normalizedPath, |
| 195 | migrateFiles, |
| 196 | }); |
| 197 | return updated; |
| 198 | } |
no test coverage detected