singularity-forge/scripts/check-protected-deletions.mjs
Mikael Hugo 565cd1069a
Some checks are pending
sf self-deploy / build, test, and publish server image (push) Waiting to run
sf self-deploy / upgrade vega source server (push) Blocked by required conditions
sf self-deploy / deploy test and probe (push) Blocked by required conditions
sf self-deploy / promote prod (push) Blocked by required conditions
fix(build): skip protected deletion check outside git worktree
2026-05-17 23:18:41 +02:00

48 lines
1.3 KiB
JavaScript

#!/usr/bin/env node
import { execFileSync } from "node:child_process";
import { existsSync } from "node:fs";
const PROTECTED_PATHS = [":(glob)src/resources/extensions/**/*.d.ts"];
if (!existsSync(".git")) {
process.stdout.write(
"check-protected-deletions: skipped outside git worktree\n",
);
process.exit(0);
}
function git(args) {
return execFileSync("git", args, {
stdio: ["ignore", "pipe", "pipe"],
encoding: "utf-8",
}).trim();
}
function listDeleted(cached) {
const args = ["diff", "--name-only", "--diff-filter=D"];
if (cached) args.push("--cached");
args.push("--", ...PROTECTED_PATHS);
const out = git(args);
return out ? out.split("\n").filter(Boolean) : [];
}
const stagedOnly = process.argv.includes("--cached");
const deleted = stagedOnly
? listDeleted(true)
: [...new Set([...listDeleted(false), ...listDeleted(true)])];
if (deleted.length > 0) {
const mode = stagedOnly ? "staged " : "";
process.stderr.write(
`check-protected-deletions: refusing ${mode}protected declaration deletions:\n`,
);
for (const path of deleted) {
process.stderr.write(` ${path}\n`);
}
process.stderr.write(
"\nRestore these files or make an explicit reviewed deletion outside automation.\n",
);
process.exit(1);
}
process.stdout.write("check-protected-deletions: ok\n");