everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
231 lines (229 loc) • 5.28 kB
JavaScript
let node_fs = require("node:fs");
let node_path = require("node:path");
//#region src/services/clean.ts
/** The absolute never-touch list — truth (PGlite data), identity (session
* credentials), machine prefs (port pins), and the publish accelerator whose
* cleaning would silently cost full re-uploads. `bos clean` reports these as
* kept, always. */
const CLEAN_NEVER_TOUCH = [
{
name: "db",
reason: "PGlite data — the app's actual state"
},
{
name: "session.json",
reason: "login credentials — bos logout, not bos clean"
},
{
name: "infra-state.json",
reason: "machine port prefs"
},
{
name: "deploy-state.json",
reason: "skip-unchanged accelerator — cleaning costs full re-uploads"
}
];
/** Files touched within this window are never cleaned — a live dev session
* is appending to its log and `bos logs` reads it. */
const LIVE_LOG_WINDOW_MS = 36e5;
function bosPath(configDir, name) {
return (0, node_path.join)(configDir, ".bos", name);
}
function planClean(configDir) {
const removable = [
{
id: "logs",
path: bosPath(configDir, "logs"),
kind: "files"
},
{
id: "bundles",
path: bosPath(configDir, "bundles"),
kind: "dir"
},
{
id: "regression",
path: bosPath(configDir, "regression"),
kind: "dir"
},
{
id: "generated",
path: bosPath(configDir, "generated"),
kind: "dir"
},
{
id: "bundle-cache",
path: bosPath(configDir, "bundle-cache"),
kind: "dir"
},
{
id: "snapshot",
path: bosPath(configDir, "snapshot"),
kind: "dir"
},
{
id: "sync-backup",
path: bosPath(configDir, "sync-backup"),
kind: "dir"
},
{
id: "import-backup",
path: bosPath(configDir, "import-backup"),
kind: "dir"
},
{
id: "compose-retired",
path: bosPath(configDir, "compose-retired"),
kind: "dir"
},
{
id: "manifest-cache",
path: bosPath(configDir, "manifest.json"),
kind: "file"
},
{
id: "resolved-config",
path: bosPath(configDir, "bos.resolved-config.json"),
kind: "file"
},
{
id: "build-cache",
path: bosPath(configDir, "build-cache.json"),
kind: "file"
}
];
const guarded = CLEAN_NEVER_TOUCH.map(({ name, reason }) => ({
id: name,
path: bosPath(configDir, name),
kind: (0, node_fs.existsSync)(bosPath(configDir, name)) ? "dir" : "file",
guarded: reason
}));
return [...removable, ...guarded];
}
function dirSize(path) {
let total = 0;
try {
for (const name of (0, node_fs.readdirSync)(path)) {
const full = (0, node_path.join)(path, name);
const stat = (0, node_fs.statSync)(full);
if (stat.isDirectory()) total += dirSize(full);
else total += stat.size;
}
} catch {
return total;
}
return total;
}
function removeTree(target) {
const bytes = dirSize(target.path);
(0, node_fs.rmSync)(target.path, {
recursive: true,
force: true
});
return bytes;
}
function removeFile(target) {
const bytes = (0, node_fs.statSync)(target.path).size;
(0, node_fs.rmSync)(target.path, { force: true });
return bytes;
}
function cleanLogs(target, dryRun) {
const now = Date.now();
let bytes = 0;
let fresh = 0;
let names = [];
try {
names = (0, node_fs.readdirSync)(target.path);
} catch {
return {
id: target.id,
path: target.path,
bytes: 0,
action: "absent"
};
}
for (const name of names) {
const full = (0, node_path.join)(target.path, name);
let size = 0;
let mtimeMs = 0;
try {
const stat = (0, node_fs.statSync)(full);
if (!stat.isFile()) continue;
size = stat.size;
mtimeMs = stat.mtimeMs;
} catch {
continue;
}
if (now - mtimeMs < LIVE_LOG_WINDOW_MS) {
fresh++;
continue;
}
bytes += size;
if (!dryRun) (0, node_fs.rmSync)(full, { force: true });
}
return {
id: target.id,
path: target.path,
bytes,
action: bytes > 0 || fresh > 0 ? dryRun ? "would-remove" : "removed" : "absent",
...fresh > 0 ? { reason: `${fresh} fresh file(s) kept (live sessions)` } : {}
};
}
function executeClean(configDir, opts = {}) {
const dryRun = opts.dryRun ?? false;
const skip = new Set(opts.skip ?? []);
const entries = [];
let freedBytes = 0;
for (const target of planClean(configDir)) {
if (skip.has(target.id)) continue;
if (target.guarded) {
entries.push({
id: target.id,
path: target.path,
bytes: 0,
action: "kept",
reason: target.guarded
});
continue;
}
if (!(0, node_fs.existsSync)(target.path)) {
entries.push({
id: target.id,
path: target.path,
bytes: 0,
action: "absent"
});
continue;
}
if (target.kind === "files") {
const entry = cleanLogs(target, dryRun);
entries.push(entry);
freedBytes += entry.bytes;
continue;
}
const bytes = target.kind === "dir" ? dirSize(target.path) : (0, node_fs.statSync)(target.path).size;
if (!dryRun) {
if (target.kind === "dir") removeTree(target);
else removeFile(target);
}
entries.push({
id: target.id,
path: target.path,
bytes,
action: dryRun ? "would-remove" : "removed"
});
freedBytes += bytes;
}
return {
status: "ok",
dryRun,
entries,
freedBytes
};
}
//#endregion
exports.CLEAN_NEVER_TOUCH = CLEAN_NEVER_TOUCH;
exports.dirSize = dirSize;
exports.executeClean = executeClean;
exports.planClean = planClean;
//# sourceMappingURL=clean.cjs.map