t-comm
Version:
专业、稳定、纯粹的工具库
42 lines (39 loc) • 1.6 kB
JavaScript
import { execSync } from 'child_process';
import { CHECK_JS_WHITE_DIR, SUB_PROJECT_CONFIG_JS_REG } from './config.js';
import 'path';
function checkJSFiles(options) {
var _a, _b, _c;
if (options === void 0) {
options = {
whiteDir: CHECK_JS_WHITE_DIR,
excludeReg: SUB_PROJECT_CONFIG_JS_REG,
log: false
};
}
var whiteDir = (_a = options === null || options === void 0 ? void 0 : options.whiteDir) !== null && _a !== void 0 ? _a : CHECK_JS_WHITE_DIR;
var excludeReg = (_b = options === null || options === void 0 ? void 0 : options.excludeReg) !== null && _b !== void 0 ? _b : SUB_PROJECT_CONFIG_JS_REG;
var log = (_c = options === null || options === void 0 ? void 0 : options.log) !== null && _c !== void 0 ? _c : false;
var stagedFiles = execSync('git diff --cached --name-only --diff-filter=d', {
encoding: 'utf-8'
}).split('\n').filter(Boolean);
if (log) {
console.log('[stagedFiles]', stagedFiles);
}
var hasJsFiles = stagedFiles.filter(function (item) {
return whiteDir === null || whiteDir === void 0 ? void 0 : whiteDir.find(function (prefix) {
return item.startsWith(prefix);
});
}).filter(function (item) {
return !(excludeReg === null || excludeReg === void 0 ? void 0 : excludeReg.test(item));
}).some(function (file) {
return /\.(js|jsx)$/.test(file);
});
if (hasJsFiles) {
console.error('❌ 错误:禁止提交 .js 文件,请使用 .ts 或 .tsx!');
process.exit(1); // 阻止提交
}
if (log) {
console.log('✅ 允许提交(无 .js 文件)');
}
}
export { checkJSFiles };