webnovel-writer.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. process.exit(0)
  43. }
  44. // 解析选项与位置参数:--key=value → {key:value},--flag → {flag:true}
  45. function parseArgs(rest) {
  46. const options = {}
  47. const positionalArgs = []
  48. for (const arg of rest) {
  49. if (arg.startsWith('--')) {
  50. const m = arg.match(/^--([^=]+)(?:=(.*))?$/)
  51. if (m) options[m[1]] = m[2] !== undefined ? m[2] : true
  52. } else {
  53. positionalArgs.push(arg)
  54. }
  55. }
  56. return { options, positionalArgs }
  57. }
  58. let cache
  59. try {
  60. const commandPath = path.join(__dirname, '../src/commands', `${command}.js`)
  61. const commandUrl = new URL(`file:///${commandPath.replace(/\\/g, '/')}`).href
  62. const mod = await import(commandUrl)
  63. const { options, positionalArgs } = parseArgs(argv.slice(1))
  64. const repoPath = process.cwd() // M3 状态机后续处理工作目录定位
  65. cache = new CacheManager(path.join(repoPath, '.cache', 'index.db'))
  66. await cache.ensureReady(repoPath)
  67. const result = await mod.run(positionalArgs, options, { repoPath, cache })
  68. if (result.ok) {
  69. if (result.output) console.log(result.output)
  70. process.exitCode = 0
  71. } else {
  72. console.error(result.error)
  73. process.exitCode = 1
  74. }
  75. } catch (err) {
  76. if (err.code === 'ERR_MODULE_NOT_FOUND' || err.code === 'ENOENT') {
  77. console.error(`未知命令「${command}」。运行 webnovel-writer --help 查看可用命令。`)
  78. process.exitCode = 1
  79. } else {
  80. // 永不带栈崩溃(错误规范 §1)
  81. console.error(`执行命令「${command}」时出错:${err.message}`)
  82. process.exitCode = 1
  83. }
  84. } finally {
  85. if (cache) await cache.close() // 显式关闭,避免 Windows 文件锁
  86. }