amatya-aditya_advanced-mult.../scripts/setup-git-hooks.mjs
amatya-aditya e3c13af0ee feat!: update 32 files
BREAKING CHANGE: behavior changed
2026-03-04 02:10:25 -06:00

40 lines
1.1 KiB
JavaScript

import { chmodSync, existsSync } from "node:fs";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
const proc = globalThis.process;
function run(command, args, options = {}) {
return spawnSync(command, args, {
stdio: "pipe",
encoding: "utf8",
...options,
});
}
const insideRepo = run("git", ["rev-parse", "--is-inside-work-tree"]);
if (insideRepo.status !== 0 || insideRepo.stdout.trim() !== "true") {
proc.stdout.write("[hooks] Not a git repository. Skipping git hook setup.\n");
proc.exit(0);
}
const setHooksPath = run("git", ["config", "core.hooksPath", ".githooks"]);
if (setHooksPath.status !== 0) {
proc.stderr.write(
setHooksPath.stderr || "[hooks] Failed to set core.hooksPath.\n",
);
proc.exit(setHooksPath.status ?? 1);
}
proc.stdout.write("[hooks] Installed git hooks path: .githooks\n");
const hooks = ["pre-commit", "pre-push", "commit-msg"];
for (const hook of hooks) {
const fullPath = join(proc.cwd(), ".githooks", hook);
if (!existsSync(fullPath)) continue;
try {
chmodSync(fullPath, 0o755);
} catch {
// Best effort on platforms/filesystems that ignore POSIX perms.
}
}