(value: unknown, canonicalLocalServer?: ServerConnection.Key)
| 32 | } |
| 33 | |
| 34 | export function migrateCanonicalLocalServerState(value: unknown, canonicalLocalServer?: ServerConnection.Key) { |
| 35 | if (!canonicalLocalServer || canonicalLocalServer === "local") return value |
| 36 | if (!isRecord(value)) return value |
| 37 | const projects = isRecord(value.projects) ? value.projects : undefined |
| 38 | const lastProject = isRecord(value.lastProject) ? value.lastProject : undefined |
| 39 | const previousProjects = projects?.[canonicalLocalServer] |
| 40 | const previousLastProject = lastProject?.[canonicalLocalServer] |
| 41 | if (!Array.isArray(previousProjects) && typeof previousLastProject !== "string") return value |
| 42 | |
| 43 | const next = { ...value } |
| 44 | if (projects && Array.isArray(previousProjects)) { |
| 45 | const local = Array.isArray(projects.local) ? projects.local : [] |
| 46 | const worktrees = new Set( |
| 47 | local.flatMap((project) => (isRecord(project) && typeof project.worktree === "string" ? [project.worktree] : [])), |
| 48 | ) |
| 49 | const migrated = previousProjects.filter((project) => { |
| 50 | if (!isRecord(project) || typeof project.worktree !== "string") return true |
| 51 | if (worktrees.has(project.worktree)) return false |
| 52 | worktrees.add(project.worktree) |
| 53 | return true |
| 54 | }) |
| 55 | const nextProjects: Record<string, unknown> = { ...projects, local: [...local, ...migrated] } |
| 56 | delete nextProjects[canonicalLocalServer] |
| 57 | next.projects = nextProjects |
| 58 | } |
| 59 | if (lastProject && typeof previousLastProject === "string") { |
| 60 | const nextLastProject = { ...lastProject } |
| 61 | if (typeof nextLastProject.local !== "string") nextLastProject.local = previousLastProject |
| 62 | delete nextLastProject[canonicalLocalServer] |
| 63 | next.lastProject = nextLastProject |
| 64 | } |
| 65 | return next |
| 66 | } |
| 67 | |
| 68 | export function createServerProjects<T extends ServerProjectState>(input: { |
| 69 | scope: Accessor<ServerScope> |
no test coverage detected