Files
RuneStone/src/lib/features/pm/storage/migration.ts
T
Knotty Wyvern 956ead818b Translate Project Management and add its tests and guide
- Every interface string of Project Management goes through the catalog: 539 new rows with fr, ja, and zh-CN.
- Storage and model messages use a local message() marker and stay free of host imports. The interface translates them.
- A board renders again when the language changes. Dates use the interface language.
- Fix: the ribbon button waits for the feature switches at startup, and the settings code loads no feature module.
- Add six smoke tests: board state, cards, menus, drag and drop, views, and language.
- Add docs/guides/project-management.md and UI-060. Update the user, plugin, SDK, host, development, and interface docs and CLAUDE.md.
- Name the fake board view of tree/renameUndo "Board fixture", because the native Project Management button is now in the ribbon.
2026-10-29 21:53:09 -07:00

152 lines
5.6 KiB
TypeScript

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<OriginalFile[]> {
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;
}