everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
105 lines (103 loc) • 3.86 kB
JavaScript
const require_services_lockfile_schema = require("../lockfile/schema.cjs");
let node_fs = require("node:fs");
let node_path = require("node:path");
//#region src/services/scaffold/snapshot.ts
const SNAPSHOT_DIR = ".bos";
const SNAPSHOT_FILE = "sync-snapshot.json";
function snapshotPath(projectDir) {
return (0, node_path.join)(projectDir, SNAPSHOT_DIR, SNAPSHOT_FILE);
}
function legacySnapshot(projectDir) {
const path = snapshotPath(projectDir);
if (!(0, node_fs.existsSync)(path)) return null;
try {
const content = (0, node_fs.readFileSync)(path, "utf-8");
return JSON.parse(content);
} catch {
return null;
}
}
/**
* The sync baseline (ADR 0047): the committed form is `bos.lock.json`'s
* `template.sync`; the legacy `.bos/sync-snapshot.json` file is the fallback
* while a repo still runs lock-less. Lock-first keeps the two in agreement
* once the migration absorbs the file, and keeps lock-less children working
* unchanged.
*/
async function readSnapshot(projectDir) {
const lock = await require_services_lockfile_schema.readLockfileIfExists(projectDir);
if (lock?.template) return {
timestamp: lock.template.syncedAt,
files: lock.template.files
};
return legacySnapshot(projectDir);
}
/**
* Record the baseline through the lock when one exists — splicing only the
* `template` key, never recomputing the frozen composition (a sync has no
* business touching remotes). The legacy file is deleted on the absorb path
* so it stops regenerating beside the lock. Lock-less repos keep the legacy
* file: it is still the only baseline, now local-only.
*/
async function writeSnapshot(projectDir, data) {
const syncedAt = (/* @__PURE__ */ new Date()).toISOString();
const lock = await require_services_lockfile_schema.readLockfileIfExists(projectDir);
if (lock) {
require_services_lockfile_schema.writeLockfileSync({
...lock,
template: {
syncedAt,
files: data.files
}
}, projectDir);
const legacyPath = snapshotPath(projectDir);
if ((0, node_fs.existsSync)(legacyPath)) (0, node_fs.rmSync)(legacyPath, { force: true });
return;
}
const dir = (0, node_path.join)(projectDir, SNAPSHOT_DIR);
if (!(0, node_fs.existsSync)(dir)) (0, node_fs.mkdirSync)(dir, { recursive: true });
(0, node_fs.writeFileSync)(snapshotPath(projectDir), `${JSON.stringify({
timestamp: syncedAt,
files: data.files
}, null, 2)}\n`);
}
/**
* The upgrade migration leg: absorb a legacy `.bos/sync-snapshot.json` into
* `bos.lock.json`'s `template.sync` and delete the file, so it stops
* rewriting ~70 hash lines across long-lived branches and stops being
* committed. "absorbed" — the lock gained the baseline (the repo's committed
* artifact changes; `bos.lock.json` rides the migration report). "removed" —
* a stale duplicate beside an already-absorbed lock, or an unparseable file.
* undefined — nothing to do: no legacy file, or no lock (the legacy file
* stays: it is still the only baseline).
*/
async function absorbLegacySnapshot(projectDir) {
const legacyPath = snapshotPath(projectDir);
if (!(0, node_fs.existsSync)(legacyPath)) return void 0;
let legacy = null;
try {
legacy = JSON.parse((0, node_fs.readFileSync)(legacyPath, "utf-8"));
} catch {
legacy = null;
}
const lock = await require_services_lockfile_schema.readLockfileIfExists(projectDir);
if (!lock) return void 0;
if (legacy && !lock.template) {
require_services_lockfile_schema.writeLockfileSync({
...lock,
template: {
syncedAt: legacy.timestamp,
files: legacy.files
}
}, projectDir);
(0, node_fs.rmSync)(legacyPath, { force: true });
return "absorbed";
}
(0, node_fs.rmSync)(legacyPath, { force: true });
return "removed";
}
//#endregion
exports.absorbLegacySnapshot = absorbLegacySnapshot;
exports.readSnapshot = readSnapshot;
exports.writeSnapshot = writeSnapshot;
//# sourceMappingURL=snapshot.cjs.map