webnovel-writer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env node
  2. import path from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { readFileSync } from 'node:fs'
  5. import { CacheManager } from '../src/cache/index.js'
  6. import { checkNodeVersion } from '../src/runtime/node-version.js'
  7. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  8. // 版本门槛先行(M0 起的不变量;纯比较在 node-version.js,副作用留这里)
  9. const gate = checkNodeVersion(process.version)
  10. if (!gate.ok) {
  11. console.error(gate.message)
  12. process.exit(1)
  13. }
  14. const argv = process.argv.slice(2)
  15. const command = argv[0]
  16. if (command === '--version' || command === '-v') {
  17. const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
  18. console.log(pkg.version)
  19. process.exit(0)
  20. }
  21. if (!command || command === '--help') {
  22. console.log('用法:webnovel-writer <命令> [选项]')
  23. console.log('')
  24. console.log('精准读取接口(41 个,分布于 21 个命令;逐条清单见任务 prd.md AC2):')
  25. console.log(' read-chapter <章号> [--front-matter|--tail=N|--head=N|--摘要]')
  26. console.log(' read-chapters [--range=a-b --摘要|--recent=N --tail=M]')
  27. console.log(' list-chapters --章定位=推进 [--卷=N]')
  28. console.log(' read-thread <ID> [--履历|--收尾计划|--描述]')
  29. console.log(' list-threads [--悬了太久|--type=<t> [--status=<s>]|--strength=<强>]')
  30. console.log(' read-timeline [--current-and-prev|--current-volume|--卷=N|--在场=名]')
  31. console.log(' read-character <正名> [--front-matter|--section=<标题>]')
  32. console.log(' resolve-alias <别名>')
  33. console.log(' list-characters [--status=<状态>]')
  34. console.log(' read-worldview --section=<标题>')
  35. console.log(' read-secret <ID> [--基本信息|--内容]')
  36. console.log(' list-secrets [--reader-knows=false]')
  37. console.log(' read-outline [--总纲 [--section=<标题>|--结局]|--卷=N [--section=<标题>]]')
  38. console.log(' list-volumes')
  39. console.log(' grep-story <关键词> [--regex=<pattern>]')
  40. console.log(' report-overdue-threads | report-secret-accumulation | report-thread-activity --卷=N')
  41. console.log(' report-weak-hook-streak | report-book-stats | report-style-drift')
  42. console.log('')
  43. console.log('写章流程(M2,零 AI 脚本面):')
  44. console.log(' prepare-chapter <章号> 备料:写出 工作区/本章写作材料.md')
  45. console.log(' mechanical-check <章号> [--draft=<路径>] 机检:字数/禁词/禁句式/复读/新专名/信息差候选')
  46. process.exit(0)
  47. }
  48. // 解析选项与位置参数:--key=value → {key:value},--flag → {flag:true}
  49. function parseArgs(rest) {
  50. const options = {}
  51. const positionalArgs = []
  52. for (const arg of rest) {
  53. if (arg.startsWith('--')) {
  54. const m = arg.match(/^--([^=]+)(?:=(.*))?$/)
  55. if (m) options[m[1]] = m[2] !== undefined ? m[2] : true
  56. } else {
  57. positionalArgs.push(arg)
  58. }
  59. }
  60. return { options, positionalArgs }
  61. }
  62. let cache
  63. try {
  64. const commandPath = path.join(__dirname, '../src/commands', `${command}.js`)
  65. const commandUrl = new URL(`file:///${commandPath.replace(/\\/g, '/')}`).href
  66. const mod = await import(commandUrl)
  67. const { options, positionalArgs } = parseArgs(argv.slice(1))
  68. const repoPath = process.cwd() // M3 状态机后续处理工作目录定位
  69. cache = new CacheManager(path.join(repoPath, '.cache', 'index.db'))
  70. await cache.ensureReady(repoPath)
  71. const result = await mod.run(positionalArgs, options, { repoPath, cache })
  72. if (result.ok) {
  73. if (result.output) console.log(result.output)
  74. process.exitCode = 0
  75. } else {
  76. console.error(result.error)
  77. process.exitCode = 1
  78. }
  79. } catch (err) {
  80. if (err.code === 'ERR_MODULE_NOT_FOUND' || err.code === 'ENOENT') {
  81. console.error(`未知命令「${command}」。运行 webnovel-writer --help 查看可用命令。`)
  82. process.exitCode = 1
  83. } else {
  84. // 永不带栈崩溃(错误规范 §1)
  85. console.error(`执行命令「${command}」时出错:${err.message}`)
  86. process.exitCode = 1
  87. }
  88. } finally {
  89. if (cache) await cache.close() // 显式关闭,避免 Windows 文件锁
  90. }