1
0

pack-install-e2e.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env node
  2. // M5 出口 D1(AC1 的 CI 半):npm pack 产物 → 干净中文路径工作目录 → init → 建书 → next → update 全链路。
  3. // 经 `npm run e2e:install` 运行(用 npm_execpath 定位 npm,全程 args 数组不走 shell,中文路径安全)。
  4. import { execFile } from 'node:child_process'
  5. import { promisify } from 'node:util'
  6. import { promises as fs } from 'node:fs'
  7. import os from 'node:os'
  8. import path from 'node:path'
  9. import { fileURLToPath } from 'node:url'
  10. const exec = promisify(execFile)
  11. const pkgRoot = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
  12. const npmCli = process.env.npm_execpath
  13. if (!npmCli) {
  14. console.error('请通过 npm run e2e:install 运行(脚本需要 npm_execpath 定位 npm)')
  15. process.exit(1)
  16. }
  17. const npm = (args, opts = {}) =>
  18. exec(process.execPath, [npmCli, ...args], { encoding: 'utf8', maxBuffer: 32 * 1024 * 1024, ...opts })
  19. let failed = false
  20. const check = (cond, msg) => {
  21. console.log(`${cond ? '✓' : '✗'} ${msg}`)
  22. if (!cond) failed = true
  23. }
  24. const exists = async (p) => {
  25. try {
  26. await fs.access(p)
  27. return true
  28. } catch {
  29. return false
  30. }
  31. }
  32. const cleanups = []
  33. try {
  34. // 1. pack(发布产物 = npx 消费的同一份包内容)
  35. const packDest = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-pack-'))
  36. cleanups.push(packDest)
  37. const packOut = await npm(['pack', '--pack-destination', packDest], { cwd: pkgRoot })
  38. const tgzName = packOut.stdout.trim().split('\n').at(-1).trim()
  39. const tgz = path.join(packDest, tgzName)
  40. check(await exists(tgz), `npm pack 产物:${tgzName}`)
  41. // 2. 装进干净沙箱
  42. const sandbox = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-sandbox-'))
  43. cleanups.push(sandbox)
  44. await npm(['install', tgz, '--prefix', sandbox, '--no-audit', '--no-fund'], { cwd: sandbox })
  45. const installedBin = path.join(sandbox, 'node_modules', 'webnovel-writer', 'bin', 'webnovel-writer.js')
  46. check(await exists(installedBin), '安装产物 bin 存在')
  47. // 3. 中文用户名模拟路径里 init(一条命令装出工作目录)
  48. const base = await fs.mkdtemp(path.join(os.tmpdir(), '中文用户名-'))
  49. cleanups.push(base)
  50. const workdir = path.join(base, '工作目录')
  51. await fs.mkdir(workdir, { recursive: true })
  52. const node = (bin, args) =>
  53. exec(process.execPath, [bin, ...args], { cwd: workdir, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 })
  54. const init = await node(installedBin, ['init', '--hosts=claude-code,codex'])
  55. console.log('--- init 报告 ---\n' + init.stdout + '-----------------')
  56. for (const rel of [
  57. '.webnovel/bin/webnovel-writer.js',
  58. '.webnovel/node_modules/js-yaml/package.json',
  59. '.webnovel/manifest.json',
  60. '.webnovel/books.jsonl',
  61. '.claude/skills/webnovel-writer/SKILL.md',
  62. '.claude/agents/事实审查.md',
  63. '.claude/settings.json',
  64. '.codex/agents/事实审查.toml',
  65. 'AGENTS.md',
  66. ]) {
  67. check(await exists(path.join(workdir, rel)), `init 布局:${rel}`)
  68. }
  69. // 4. vendored bin 建第一本书(干净环境一条命令装出并建书 = AC1 链路)
  70. const vendoredBin = path.join(workdir, '.webnovel', 'bin', 'webnovel-writer.js')
  71. await fs.writeFile(
  72. path.join(workdir, '建书.json'),
  73. JSON.stringify({
  74. book: { spec_version: '7.0', 书名: '雪落长安', 卷规模: 40, 体检周期: 50 },
  75. 总纲: '# 总纲\n## 结局\n终成一代大家。',
  76. 卷纲: '# 第1卷\n初入长安。',
  77. }),
  78. 'utf8'
  79. )
  80. const pb = await node(vendoredBin, ['persist-book', '--file=建书.json'])
  81. check(pb.stdout.includes('已登记为当前书'), '建书并登记为当前书(vendored bin)')
  82. check(await exists(path.join(workdir, '雪落长安', 'book.yaml')), '书仓库 book.yaml')
  83. check(await exists(path.join(workdir, '雪落长安', 'AGENTS.md')), '书仓库指路 AGENTS.md')
  84. // 5. next --json → 序6 起草第 1 章
  85. const nx = await node(vendoredBin, ['next', '--json'])
  86. const dto = JSON.parse(nx.stdout)
  87. check(dto.序 === 6 && dto.dto.nextChapter === 1, `next 判定起草第 1 章(实际:序${dto.序})`)
  88. // 6. update 幂等重跑
  89. const up = await node(installedBin, ['update'])
  90. check(/升级完成|安装完成/.test(up.stdout), 'update 幂等可重跑')
  91. if (failed) {
  92. console.error('\n安装链路 e2e 存在失败项')
  93. process.exit(1)
  94. }
  95. console.log('\n安装链路 e2e 全通过:pack → 安装 → 中文路径 init → 建书 → next → update')
  96. } catch (err) {
  97. console.error(`安装链路 e2e 异常:${err.message}`)
  98. if (err.stdout) console.error(err.stdout)
  99. if (err.stderr) console.error(err.stderr)
  100. process.exit(1)
  101. } finally {
  102. for (const d of cleanups) {
  103. try {
  104. await fs.rm(d, { recursive: true, force: true, maxRetries: 3 })
  105. } catch {
  106. // 临时目录清理尽力而为
  107. }
  108. }
  109. }