GitNexus/.devcontainer/verify-git-hooks.cjs
Gergő Magyar b961e695ca
Some checks failed
Devcontainer Smoke / Config-transform unit tests (push) Has been cancelled
Devcontainer Smoke / Build devcontainer image (default) (push) Has been cancelled
Publish Development Images / publish (core) (push) Has been cancelled
Publish Development Images / publish (ui) (push) Has been cancelled
Devcontainer Smoke / Build devcontainer image (ui) (push) Has been cancelled
CodeQL / Analyze (python) (push) Waiting to run
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / ci (push) Blocked by required conditions
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Skill copy sync / shipped skills drift guard (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix(devcontainer): refresh Node and streamline development (#3610)
2026-10-17 21:05:13 +00:00

63 lines
2.2 KiB
JavaScript

'use strict';
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const { randomUUID } = require('node:crypto');
const { execFileSync } = require('node:child_process');
function verifyGitHooks(root = process.cwd()) {
root = path.resolve(root);
if (process.env.HUSKY === '0') throw new Error('HUSKY=0 disables pre-commit hooks.');
const hooksPath = execFileSync('git', ['config', '--get', 'core.hooksPath'], {
cwd: root,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim();
if (path.resolve(root, hooksPath) !== path.join(root, '.husky', '_')) {
throw new Error('core.hooksPath must point to .husky/_.');
}
for (const [file, mode] of [
['.husky/pre-commit', fs.constants.R_OK],
['.husky/_/h', fs.constants.R_OK],
['.husky/_/pre-commit', fs.constants.R_OK | fs.constants.X_OK],
]) {
const target = path.join(root, file);
if (!fs.statSync(target).isFile()) throw new Error(`${file} must be a file.`);
fs.accessSync(target, mode);
}
// Exercise the installed Husky helper, including its init.sh, against a
// temporary sentinel hook. Never run the real pre-commit or touch the index.
const probe = fs.mkdtempSync(path.join(os.tmpdir(), 'gitnexus-hook-probe-'));
try {
const token = randomUUID();
fs.mkdirSync(path.join(probe, '_'));
fs.writeFileSync(path.join(probe, 'pre-commit'), `printf '%s\\n' '${token}'\n`);
const output = execFileSync(
'sh',
['-c', '. "$1"', path.join(probe, '_/pre-commit'), path.join(root, '.husky/_/h')],
{ cwd: root, encoding: 'utf8', timeout: 5000, stdio: ['ignore', 'pipe', 'pipe'] },
);
if (!output.includes(token)) {
throw new Error('Husky startup configuration disables or skips pre-commit hooks.');
}
} finally {
fs.rmSync(probe, { recursive: true, force: true });
}
}
module.exports = { verifyGitHooks };
if (require.main === module) {
try {
verifyGitHooks();
console.log('[install-deps] Husky pre-commit hook is installed and enabled');
} catch (error) {
console.error(`[install-deps] Husky verification failed: ${error.message}`);
console.error('Run npm run prepare at the repository root and keep HUSKY enabled.');
process.exitCode = 1;
}
}