feat: relink moved project folders

This commit is contained in:
muena
2026-11-01 18:48:40 +01:00
parent 5200fcde70
commit a5e20a7e03
4 changed files with 199 additions and 1 deletions

View File

@@ -0,0 +1,83 @@
import { promises as fs } from "node:fs";
import { basename, dirname, join } from "node:path";
import { environment } from "@raycast/api";
import { canonicalProjectPath, listRuns, projectDataRoot, saveRun } from "./velum-project";
import type { ProjectRun } from "./velum-project";
export type RelocationResult = { runsMoved: number; copiesRelinked: number; copiesMissing: number };
async function isDirectory(path: string): Promise<boolean> {
try { return (await fs.stat(path)).isDirectory(); }
catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
throw error;
}
}
async function exists(path: string): Promise<boolean> {
try { await fs.lstat(path); return true; }
catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
throw error;
}
}
async function relocatedCopyPath(run: ProjectRun, newProjectPath: string): Promise<string | undefined> {
if (!run.copyPath || await isDirectory(run.copyPath)) return run.copyPath;
const parent = dirname(newProjectPath);
const candidates = [
join(parent, basename(run.copyPath)),
join(parent, `${basename(newProjectPath)}-Velum-${run.id}`),
];
for (const candidate of candidates) if (await isDirectory(candidate)) return candidate;
return run.copyPath;
}
/** Change the path identity of a project, preserving every saved run and its private rules. */
export async function relocateProject(oldPath: string, chosenPath: string,
support = environment.supportPath): Promise<RelocationResult> {
const previous = canonicalProjectPath(oldPath);
const next = canonicalProjectPath(chosenPath);
if (previous === next) throw new Error("Der neue Ordner ist bereits der Projektordner.");
if (!await isDirectory(next)) throw new Error("Der neue Projektordner ist nicht vorhanden.");
const sourceRoot = projectDataRoot(previous, support);
const targetRoot = projectDataRoot(next, support);
if (await exists(targetRoot)) {
throw new Error("Für diesen Ordner gibt es bereits ein Velum-Projekt. Die Projekte wurden nicht zusammengeführt.");
}
const runs = await listRuns(previous, support);
if (!runs.length) throw new Error("Für den bisherigen Ordner gibt es keinen gespeicherten Projektstand.");
const originals = await Promise.all(runs.map(async ({ directory }) =>
await fs.readFile(join(directory, "run.json"), "utf8")));
const updated = await Promise.all(runs.map(async ({ run }) => ({
...run,
projectPath: next,
projectName: basename(next),
copyPath: await relocatedCopyPath(run, next),
})));
const copyExists = await Promise.all(updated.map(async (run) =>
!run.copyPath || await isDirectory(run.copyPath)));
await fs.rename(sourceRoot, targetRoot);
try {
for (let index = 0; index < runs.length; index++) {
await saveRun(join(targetRoot, "runs", basename(runs[index].directory)), updated[index]);
}
} catch (error) {
try {
for (let index = 0; index < runs.length; index++) {
await fs.writeFile(join(targetRoot, "runs", basename(runs[index].directory), "run.json"),
originals[index], { encoding: "utf8", mode: 0o600 });
}
await fs.rename(targetRoot, sourceRoot);
} catch (rollbackError) {
throw new Error(`Projektpfad konnte nicht vollständig aktualisiert werden: ${(error as Error).message}. `
+ `Bitte die Projektstände prüfen; Rücksetzung fehlgeschlagen: ${(rollbackError as Error).message}`);
}
throw error;
}
return {
runsMoved: runs.length,
copiesRelinked: updated.filter((run, index) => run.copyPath !== runs[index].run.copyPath).length,
copiesMissing: copyExists.filter((available) => !available).length,
};
}