openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
44 lines (43 loc) • 1.87 kB
JavaScript
import { f as safeStat } from "./path-existence-Co7-GyVr.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/plugins/doctor-state-migration-fs.ts
/** True when the legacy-state path exists and is a regular file. */
async function legacyStateFileExists(e) {
return (await safeStat(e))?.isFile() ?? false;
}
/**
* Renames a migrated legacy source to `<path>.migrated`, recording the outcome in the
* doctor changes/warnings lists. An optional archive directory confines all collision
* suffixes to a declared migration backup resource, refusing symbolic-link sources
* rather than changing their relative target. Never throws: a failed archive leaves
* the source in place so a later doctor run can retry without losing migrated data.
*/
async function archiveLegacyStateSource(e) {
const t = e.archiveDirectory ? path.join(e.archiveDirectory, path.basename(e.filePath)) : e.filePath;
let n = `${t}.migrated`;
try {
if (e.archiveDirectory) {
if ((await fs.lstat(e.filePath)).isSymbolicLink()) throw new Error("Cannot move a symbolic-link legacy source into an archive directory");
await fs.mkdir(e.archiveDirectory, { recursive: true });
}
if (await legacyStateFileExists(n)) {
const [r, i] = await Promise.all([fs.readFile(e.filePath), fs.readFile(n)]);
if (r.equals(i)) {
await fs.rm(e.filePath, { force: true });
e.changes.push(`Removed already-archived ${e.label} legacy source ${e.filePath}`);
return;
}
for (let e = 2;; e++) {
n = `${t}.migrated.${e}`;
if (!await legacyStateFileExists(n)) break;
}
}
await fs.rename(e.filePath, n);
e.changes.push(`Archived ${e.label} legacy source -> ${n}`);
} catch (t) {
e.warnings.push(`Failed archiving ${e.label} legacy source: ${String(t)}`);
}
}
//#endregion
export { legacyStateFileExists as n, archiveLegacyStateSource as t };