t-comm
Version:
专业、稳定、纯粹的工具库
40 lines (37 loc) • 1.68 kB
JavaScript
import { getChildProcess } from '../nodejs/child_process.mjs';
import { CHECK_JS_WHITE_DIR, SUB_PROJECT_CONFIG_JS_REG } from './config.mjs';
import '../nodejs/path.mjs';
function checkJSFiles() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
whiteDir: CHECK_JS_WHITE_DIR,
excludeReg: SUB_PROJECT_CONFIG_JS_REG,
log: false
};
var _a, _b, _c;
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 = getChildProcess().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 };