knowledge-check.mjs 1.19 KB
/**
 * @file 重新扫描源码并检查受管知识产物是否漂移。
 */

import path from 'node:path'
import { buildKnowledgeGraph } from './build-knowledge-graph.mjs'
import { checkKnowledgeArtifacts } from './knowledge-artifacts.mjs'

function rootArgument(argv) {
  const index = argv.indexOf('--root')
  if (index === -1) return process.cwd()
  if (!argv[index + 1]) throw new Error('--root requires a directory')
  return path.resolve(argv[index + 1])
}

/**
 * 检查指定项目的知识图谱与 Markdown 视图是否需要重新生成。
 *
 * @param {{rootDir: string}} options 项目根目录。
 * @returns {Promise<string[]>} 漂移文件列表。
 */
export async function checkCurrentKnowledgeArtifacts({ rootDir }) {
  const graph = await buildKnowledgeGraph({ rootDir })
  return checkKnowledgeArtifacts({ graph, rootDir })
}

const rootDir = rootArgument(process.argv.slice(2))
const drift = await checkCurrentKnowledgeArtifacts({ rootDir })
if (drift.length) {
  console.error('knowledge artifacts are stale:')
  for (const file of drift) console.error(`- ${file}`)
  console.error('run npm run knowledge:build')
  process.exitCode = 1
} else {
  console.log('knowledge artifacts are current')
}