SummaryWriter.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { promises as fs } from 'node:fs'
  2. import path from 'node:path'
  3. /**
  4. * SummaryWriter:写章摘要(M2 定稿流程调用,审稿单定稿版落盘)。
  5. */
  6. export class SummaryWriter {
  7. constructor(repoPath, cache = null) {
  8. this.repoPath = repoPath
  9. this.cache = cache
  10. }
  11. /**
  12. * 写章摘要到 定稿/摘要/章摘要/NNNN.md。
  13. * @param {number} chapterNum
  14. * @param {string} text - 摘要文本(≤200 字,纯文本无 front matter)
  15. * @returns {Promise<{ok: boolean, filePath: string, error: string}>}
  16. */
  17. async writeChapterSummary(chapterNum, text) {
  18. try {
  19. const dir = path.join(this.repoPath, '定稿', '摘要', '章摘要')
  20. await fs.mkdir(dir, { recursive: true })
  21. const filePath = path.join(dir, `${String(chapterNum).padStart(4, '0')}.md`)
  22. await fs.writeFile(filePath, `${String(text).trim()}\n`, 'utf8')
  23. return { ok: true, filePath, error: '' }
  24. } catch (err) {
  25. return { ok: false, filePath: '', error: `写章摘要 ${chapterNum} 失败:${err.message}` }
  26. }
  27. }
  28. }