import type { Workspace, Task } from "../model/model.ts"; import { migrateCards, readCards, cardOrigin } from "./cards.ts"; import type { BoardDocument } from "./board.ts"; import { backupFile, readCatalog, writeCatalog, isManagedBoard, type OriginalFile, } from "./catalog.ts"; import { labelsPath, legacyLabelsPath, parseLabels } from "./labels.ts"; import { readOptional, saveDocument, ensurePrivateStorage, } from "./persistence.ts"; import { format, message } from "./message.ts"; /** Retain originals until the user checks the boards and removes the listed files. */ export async function migrateStorage( workspace: Workspace, boards: BoardDocument[], cards: Task[] = [], ) { if (!workspace.readOptional || !workspace.create) throw new Error(message("Update RuneStone before migrating Project Management storage.")); const snapshot = await readCatalog(workspace); const legacy = boards.filter((board) => !isManagedBoard(board.path)); const legacyLabels = await readOptional(workspace, legacyLabelsPath); const savedLabels = await readOptional(workspace, labelsPath); if (legacyLabels !== null) parseLabels(legacyLabels); if (savedLabels !== null) parseLabels(savedLabels); // Validate every source before creating backups or publishing any changes. for (const board of legacy) { if (snapshot.value.deletedBoardIds?.includes(board.id)) throw new Error(message("This board was deleted. Refresh Project Management before migration.")); if ((await workspace.read(board.path)) !== board.source) throw new Error( format(message("The board changed: {path}. Refresh before migration."), { path: board.path }), ); const existing = snapshot.value.boards.find( (item) => item.value.id === board.id, ); if ( existing && (existing.original?.path !== board.path || existing.original.source !== board.source) ) throw new Error( format(message("A different board already uses the ID from {path}."), { path: board.path }), ); } if (legacyLabels !== null && savedLabels !== null) { const original = JSON.parse(savedLabels).original; if ( original?.path !== legacyLabelsPath || original?.source !== legacyLabels ) throw new Error(message("Hidden labels and legacy labels both exist. Resolve their differences before migration.")); } await ensurePrivateStorage(workspace); let changed = false; for (const board of legacy) { if (snapshot.value.boards.some((item) => item.value.id === board.id)) continue; const original = await backupFile(workspace, board.path, board.source); snapshot.value.boards.push({ name: board.name, value: board.value, original, }); changed = true; } const labelBackup = legacyLabels !== null && savedLabels === null ? await backupFile(workspace, legacyLabelsPath, legacyLabels) : undefined; for (const board of legacy) if ((await workspace.read(board.path)) !== board.source) throw new Error( format(message("The board changed: {path}. Refresh before migration."), { path: board.path }), ); if (legacyLabels !== (await readOptional(workspace, legacyLabelsPath))) throw new Error(message("Legacy labels changed. Refresh before migration.")); if (changed) await writeCatalog(workspace, snapshot.value, snapshot.source); if (labelBackup) { const source = JSON.stringify( { ...JSON.parse(legacyLabels!), original: labelBackup }, null, 2, ) + "\n"; await saveDocument(workspace, labelsPath, source, null); if ((await workspace.read(labelsPath)) !== source) throw new Error(message("The saved label check failed. Retry migration.")); } const saved = await readCatalog(workspace); for (const board of legacy) { const entry = saved.value.boards.find((item) => item.value.id === board.id); const expected = snapshot.value.boards.find( (item) => item.value.id === board.id, ); if ( entry?.original?.source !== board.source || JSON.stringify(entry) !== JSON.stringify(expected) ) throw new Error(message("The saved board check failed. Retry migration.")); } await migrateCards(workspace, cards); const files = await migrationFiles(workspace); for (const file of files) { if ((await workspace.read(file.backup)) !== file.source) throw new Error( format(message("The backup check failed for {path}. Keep the original."), { path: file.path }), ); const current = await readOptional(workspace, file.path); if (current !== null && current !== file.source) throw new Error( format(message("The original changed: {path}. Keep both versions."), { path: file.path }), ); } return files; } export async function migrationFiles( workspace: Workspace, ): Promise { const snapshot = await readCatalog(workspace); const files = snapshot.value.boards.flatMap((board) => board.original ? [board.original] : [], ); const labels = await readOptional(workspace, labelsPath); if (labels !== null) { parseLabels(labels); const original = JSON.parse(labels).original; if ( original && typeof original.path === "string" && typeof original.source === "string" && typeof original.backup === "string" ) files.push(original); } const cards = await readCards(workspace); if (cards.issues.length) throw new Error(cards.issues.join(" ")); for (const task of cards.tasks) { const original = cardOrigin(task.source); if (original) files.push(original); } return files; }