|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +const RECENT_REQUIREMENT_ARTIFACT_DAYS = 2 |
| 7 | +const REQUIREMENT_FILES = ['01-需求概述.md', '04-实施计划.md', '05-实施进度.md'] |
| 8 | + |
| 9 | +function hasText(filePath, needle) { |
| 10 | + return fs.readFileSync(filePath, 'utf8').includes(needle) |
| 11 | +} |
| 12 | + |
| 13 | +function hasAnyText(filePath, needles) { |
| 14 | + const text = fs.readFileSync(filePath, 'utf8') |
| 15 | + return needles.some(needle => text.includes(needle)) |
| 16 | +} |
| 17 | + |
| 18 | +function hasRecentArtifact(dirPath, nowMs, recentDays) { |
| 19 | + const cutoff = nowMs - recentDays * 24 * 60 * 60 * 1000 |
| 20 | + return REQUIREMENT_FILES |
| 21 | + .map(name => path.join(dirPath, name)) |
| 22 | + .filter(filePath => fs.existsSync(filePath)) |
| 23 | + .some(filePath => fs.statSync(filePath).mtimeMs >= cutoff) |
| 24 | +} |
| 25 | + |
| 26 | +function checkRequirementDir(dirPath) { |
| 27 | + const issues = [] |
| 28 | + const relDir = path.basename(dirPath) |
| 29 | + const requirementFile = path.join(dirPath, '01-需求概述.md') |
| 30 | + const planFile = path.join(dirPath, '04-实施计划.md') |
| 31 | + const progressFile = path.join(dirPath, '05-实施进度.md') |
| 32 | + |
| 33 | + if (fs.existsSync(requirementFile) && !hasText(requirementFile, '## 目录导航')) { |
| 34 | + issues.push(`${relDir}/01-需求概述.md missing "## 目录导航"`) |
| 35 | + } |
| 36 | + |
| 37 | + if (fs.existsSync(planFile)) { |
| 38 | + if (!hasText(planFile, '## 目录导航')) { |
| 39 | + issues.push(`${relDir}/04-实施计划.md missing "## 目录导航"`) |
| 40 | + } |
| 41 | + if (!hasText(planFile, '计划模式')) { |
| 42 | + issues.push(`${relDir}/04-实施计划.md missing plan mode`) |
| 43 | + } |
| 44 | + if (!hasAnyText(planFile, ['验证路线', '独立验证方式', '验证方式'])) { |
| 45 | + issues.push(`${relDir}/04-实施计划.md missing validation section`) |
| 46 | + } |
| 47 | + if (!hasAnyText(planFile, ['回滚摘要', '回滚触发', '回滚方案'])) { |
| 48 | + issues.push(`${relDir}/04-实施计划.md missing rollback section`) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (fs.existsSync(progressFile)) { |
| 53 | + const requiredNeedles = [ |
| 54 | + '当前轮次', |
| 55 | + '当前 CP', |
| 56 | + '当前批次', |
| 57 | + '## 目录导航', |
| 58 | + '进度总览', |
| 59 | + '支撑产物状态', |
| 60 | + '本轮验证结果', |
| 61 | + '阻塞与恢复', |
| 62 | + '下一步', |
| 63 | + '变更记录' |
| 64 | + ] |
| 65 | + for (const needle of requiredNeedles) { |
| 66 | + if (!hasText(progressFile, needle)) { |
| 67 | + issues.push(`${relDir}/05-实施进度.md missing "${needle}"`) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return issues |
| 73 | +} |
| 74 | + |
| 75 | +function collectRecentRequirementArtifactIssues({ |
| 76 | + activeRoot, |
| 77 | + recentDays = RECENT_REQUIREMENT_ARTIFACT_DAYS, |
| 78 | + nowMs = Date.now() |
| 79 | +}) { |
| 80 | + const requirementsRoot = path.join(activeRoot, 'requirements') |
| 81 | + const checkedDirs = [] |
| 82 | + const issues = [] |
| 83 | + |
| 84 | + if (!fs.existsSync(requirementsRoot)) { |
| 85 | + return { checkedDirs, issues } |
| 86 | + } |
| 87 | + |
| 88 | + for (const entry of fs.readdirSync(requirementsRoot, { withFileTypes: true })) { |
| 89 | + if (!entry.isDirectory()) continue |
| 90 | + const dirPath = path.join(requirementsRoot, entry.name) |
| 91 | + const hasTrackedArtifact = REQUIREMENT_FILES.some(name => fs.existsSync(path.join(dirPath, name))) |
| 92 | + if (!hasTrackedArtifact) continue |
| 93 | + if (!hasRecentArtifact(dirPath, nowMs, recentDays)) continue |
| 94 | + checkedDirs.push(entry.name) |
| 95 | + issues.push(...checkRequirementDir(dirPath)) |
| 96 | + } |
| 97 | + |
| 98 | + return { checkedDirs, issues } |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = { |
| 102 | + RECENT_REQUIREMENT_ARTIFACT_DAYS, |
| 103 | + checkRequirementDir, |
| 104 | + collectRecentRequirementArtifactIssues |
| 105 | +} |
0 commit comments