wms_worker_station_service/script/git-safety-check.cjs
liufumingren 1ab1bc17d5 1
2026-07-25 00:47:03 +08:00

47 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const path = require('node:path');
const { runCommandSafe } = require('./deploy-lib.cjs');
const BLOCKED_PATH_PATTERNS = [
/^config\/config\.local\.js$/,
/^config\/deploy\.config\.local\.cjs$/,
/^tmp\/(?!deploy\/)(?!docs\/).+/,
/^logs\//,
/^run\//,
/\.pem$/i,
/\.key$/i,
/^\.env(?:\..+)?$/,
];
function normalizePath(filePath) {
return filePath.replace(/\\/g, '/');
}
function listStagedFiles() {
const output = runCommandSafe('git', [ 'diff', '--cached', '--name-only', '--diff-filter=ACMR' ]);
if (!output) {
return [];
}
return output.split(/\r?\n/).map(item => normalizePath(item.trim())).filter(Boolean);
}
function main() {
const stagedFiles = listStagedFiles();
const blocked = stagedFiles.filter(filePath => BLOCKED_PATH_PATTERNS.some(pattern => pattern.test(filePath)));
if (blocked.length === 0) {
console.log('git safety check passed');
return;
}
console.error('以下文件禁止提交到 git,请先取消暂存:');
for (const filePath of blocked) {
console.error(`- ${filePath}`);
}
console.error('建议使用 git restore --staged <file> 取消暂存,再继续提交。');
process.exit(1);
}
main();