droboticflow/scripts/check-git-hooks.mjs

32 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
import { execFileSync } from "node:child_process";
import { accessSync, constants, existsSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
try {
if (process.env.CI || process.env.HUSKY === "0" || !existsSync(resolve(root, ".git"))) {
console.log("Git hooks: skipped (CI, HUSKY=0, or exported source)");
} else {
let configured = "";
try {
configured = execFileSync("git", ["config", "--get", "core.hooksPath"], { cwd: root, encoding: "utf8" }).trim();
} catch {}
if (process.argv.includes("--install")) {
if (configured && resolve(root, configured) !== resolve(root, ".husky/_"))
throw Error("Custom hooksPath detected; left unchanged");
execFileSync(process.execPath, [resolve(root, "node_modules/husky/bin.js")], { cwd: root, stdio: "inherit" });
configured = execFileSync("git", ["config", "--get", "core.hooksPath"], { cwd: root, encoding: "utf8" }).trim();
}
if (resolve(root, configured) !== resolve(root, ".husky/_"))
throw Error("core.hooksPath must resolve to this checkout's .husky/_");
for (const name of [".husky/_/pre-commit"]) accessSync(resolve(root, name), constants.X_OK);
accessSync(resolve(root, ".husky/_/h"), constants.R_OK);
accessSync(resolve(root, ".husky/pre-commit"), constants.R_OK);
console.log(`Git hooks: active in ${root}`);
}
} catch (error) {
console.error(`Git hooks unavailable: ${error.message}. Run npm run hooks:install in this checkout.`);
process.exitCode = 1;
}