const fs = require('fs'); const path = require('path'); const ROOT = process.cwd(); const MODEL_DIR = path.join(ROOT, 'app', 'model'); const ALLOWED_HOOKS_BY_FILE = { 'entry_items.js': new Set([ 'beforeSave', 'beforeUpdate' ]), }; const ALLOWED_SCOPE_FILES = new Set([ 'print_item.js' ]); const ALLOWED_VIRTUAL_FILES = new Set([]); const FORBIDDEN_PATTERNS = [ { type: 'forbidden-service-call', message: '模型层禁止直接调用 service', regex: /\b(?:ctx|this\.ctx|app|this\.app)\.service\b/, }, { type: 'forbidden-http-call', message: '模型层禁止外部 HTTP 调用', regex: /\b(?:ctx|this\.ctx)\.(?:curl|httpclient)\b|\baxios\b|\bfetch\s*\(/, }, { type: 'forbidden-cache-call', message: '模型层禁止读写 Redis/缓存', regex: /\b(?:ctx|this\.ctx|app|this\.app)\.redis\b/, }, { type: 'forbidden-cross-model-orchestration', message: '模型层禁止通过 ctx.model 进行跨模型编排', regex: /\b(?:ctx|this\.ctx)\.model\./, }, { type: 'forbidden-transaction-control', message: '模型层禁止定义事务控制逻辑', regex: /\b(?:sequelize|Sequelize)\.transaction\b|\bbeginTransaction\b|\bcommit\s*\(\s*\)|\brollback\s*\(\s*\)/, }, ]; function listJsFiles(dir) { if (!fs.existsSync(dir)) return []; const files = []; const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { files.push(...listJsFiles(fullPath)); continue; } if (entry.isFile() && entry.name.endsWith('.js')) { files.push(fullPath); } } return files.sort(); } function normalizeRelPath(filePath) { return path.relative(ROOT, filePath).replace(/\\/g, '/'); } function isCommentLine(line) { const trimmed = line.trim(); return ( trimmed === '' || trimmed.startsWith('//') || trimmed.startsWith('*') || trimmed.startsWith('/*') || trimmed.startsWith('*/') ); } function scanFile(filePath) { const relPath = normalizeRelPath(filePath); const fileName = path.basename(filePath); const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/); const violations = []; const discovered = { hooks: [], hasScopes: false, virtualLines: [], }; for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (isCommentLine(line)) continue; for (const rule of FORBIDDEN_PATTERNS) { if (!rule.regex.test(line)) continue; violations.push({ relPath, line: i + 1, type: rule.type, message: rule.message, content: line.trim(), }); } const hookMatch = line.match(/\b[A-Za-z_]\w*\.((?:before|after)[A-Z]\w*)\s*\(/); if (hookMatch) { const hookName = hookMatch[1]; discovered.hooks.push({ line: i + 1, hookName }); const allowSet = ALLOWED_HOOKS_BY_FILE[fileName]; if (!allowSet || !allowSet.has(hookName)) { violations.push({ relPath, line: i + 1, type: 'disallowed-hook', message: '发现未登记的模型 hook,请先更新 temp/docs/model_boundary_rules.md 与 allowlist', content: line.trim(), }); } } if (/\bscopes\s*:/.test(line)) { discovered.hasScopes = true; if (!ALLOWED_SCOPE_FILES.has(fileName)) { violations.push({ relPath, line: i + 1, type: 'disallowed-scopes', message: '发现未登记的 scopes 定义,请先更新 temp/docs/model_boundary_rules.md 与 allowlist', content: line.trim(), }); } } if (/DataTypes\.VIRTUAL|\bVIRTUAL\s*\(/.test(line)) { discovered.virtualLines.push(i + 1); if (!ALLOWED_VIRTUAL_FILES.has(fileName)) { violations.push({ relPath, line: i + 1, type: 'disallowed-virtual-field', message: '发现未登记的 virtual 字段定义,请先更新 temp/docs/model_boundary_rules.md 与 allowlist', content: line.trim(), }); } } } return { violations, discovered }; } function main() { const files = listJsFiles(MODEL_DIR); const allViolations = []; let hookCount = 0; let scopeFileCount = 0; let virtualCount = 0; for (const file of files) { const { violations, discovered } = scanFile(file); allViolations.push(...violations); hookCount += discovered.hooks.length; if (discovered.hasScopes) scopeFileCount += 1; virtualCount += discovered.virtualLines.length; } console.log('[MODEL-BOUNDARY] scan finished'); console.log(`[MODEL-BOUNDARY] files=${files.length}, hooks=${hookCount}, scopeFiles=${scopeFileCount}, virtualLines=${virtualCount}`); if (allViolations.length > 0) { console.error(`[MODEL-BOUNDARY] violations=${allViolations.length}`); for (const item of allViolations) { console.error(`- ${item.relPath}:${item.line} [${item.type}] ${item.message}`); console.error(` ${item.content}`); } process.exit(1); } console.log('[MODEL-BOUNDARY] PASS'); } main();