generate-web-manual-docx.mjs 21.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawnSync } from 'node:child_process'

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const docsDir = path.join(rootDir, 'docs')
const buildDir = path.join(rootDir, '.docx-build-web-manual')
const outputFile = path.join(docsDir, '安博福Web操作说明手册_图文版.docx')
const manualSource = path.join(docsDir, 'Web端操作说明手册.md')

const images = [
  { src: path.join(rootDir, 'public', 'HUAHENG.png'), name: 'image1.png' },
  { src: path.join(rootDir, 'public', 'vuestic-admin-image.png'), name: 'image2.png' },
]

const EMU_PER_INCH = 914400
const PX_PER_INCH = 96

const xmlEscape = (value) =>
  String(value)
    .replace(/&/gu, '&')
    .replace(/</gu, '&lt;')
    .replace(/>/gu, '&gt;')
    .replace(/"/gu, '&quot;')
    .replace(/'/gu, '&apos;')

const pngDimensions = async (filePath) => {
  const buf = await fs.readFile(filePath)
  if (buf.length < 24 || buf.readUInt32BE(0) !== 0x89504e47) {
    throw new Error(`Unsupported PNG file: ${filePath}`)
  }
  return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) }
}

const emuFromInches = (inches) => Math.round(inches * EMU_PER_INCH)
const pxToInches = (px) => px / PX_PER_INCH

const fitImageSize = (widthPx, heightPx, targetWidthInches) => {
  const aspect = heightPx / widthPx
  const width = targetWidthInches
  const height = width * aspect
  return { cx: emuFromInches(width), cy: emuFromInches(height) }
}

const p = (text, opts = {}) => {
  const {
    style = null,
    align = null,
    bold = false,
    size = 22,
    color = '000000',
    spacingBefore = 0,
    spacingAfter = 0,
    keepNext = false,
  } = opts

  const pPr = [
    style ? `<w:pStyle w:val="${style}"/>` : '',
    align ? `<w:jc w:val="${align}"/>` : '',
    (spacingBefore || spacingAfter)
      ? `<w:spacing w:before="${spacingBefore}" w:after="${spacingAfter}"/>`
      : '',
    keepNext ? '<w:keepNext/>' : '',
  ]
    .filter(Boolean)
    .join('')

  const rPr = [
    `<w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>`,
    bold ? '<w:b/>' : '',
    `<w:sz w:val="${size}"/>`,
    `<w:szCs w:val="${size}"/>`,
    `<w:color w:val="${color}"/>`,
  ]
    .filter(Boolean)
    .join('')

  return `<w:p>${pPr ? `<w:pPr>${pPr}</w:pPr>` : ''}<w:r><w:rPr>${rPr}</w:rPr><w:t xml:space="preserve">${xmlEscape(text)}</w:t></w:r></w:p>`
}

const pageBreak = () => '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'

const imageParagraph = (rid, pictureId, name, cx, cy, align = 'center') => {
  const drawing = `
    <w:drawing>
      <wp:inline distT="0" distB="0" distL="0" distR="0">
        <wp:extent cx="${cx}" cy="${cy}"/>
        <wp:docPr id="${pictureId}" name="${xmlEscape(name)}"/>
        <wp:cNvGraphicFramePr>
          <a:graphicFrameLocks noChangeAspect="1"/>
        </wp:cNvGraphicFramePr>
        <a:graphic>
          <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
            <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
              <pic:nvPicPr>
                <pic:cNvPr id="${pictureId}" name="${xmlEscape(name)}"/>
                <pic:cNvPicPr/>
              </pic:nvPicPr>
              <pic:blipFill>
                <a:blip r:embed="${rid}"/>
                <a:stretch><a:fillRect/></a:stretch>
              </pic:blipFill>
              <pic:spPr>
                <a:xfrm>
                  <a:off x="0" y="0"/>
                  <a:ext cx="${cx}" cy="${cy}"/>
                </a:xfrm>
                <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
              </pic:spPr>
            </pic:pic>
          </a:graphicData>
        </a:graphic>
      </wp:inline>
    </w:drawing>`

  return `<w:p><w:pPr><w:jc w:val="${align}"/></w:pPr><w:r>${drawing}</w:r></w:p>`
}

const bullet = (text, level = 0) => p(`- ${text}`, { spacingAfter: 80, size: 21 })

const cell = (text, opts = {}) => {
  const { bold = false, shade = null, align = 'left', width = null } = opts
  const tcPr = [
    width !== null ? `<w:tcW w:w="${width}" w:type="dxa"/>` : '',
    shade ? `<w:shd w:val="clear" w:color="auto" w:fill="${shade}"/>` : '',
    '<w:vAlign w:val="center"/>',
  ]
    .filter(Boolean)
    .join('')

  return `<w:tc><w:tcPr>${tcPr}</w:tcPr>${p(text, {
    align,
    bold,
    size: 21,
    spacingBefore: 60,
    spacingAfter: 60,
  })}</w:tc>`
}

const table = (rows, widths) => {
  const tblGrid = widths.map((w) => `<w:gridCol w:w="${w}"/>`).join('')
  const trRows = rows
    .map((row, rowIndex) => {
      const isHeader = rowIndex === 0
      const tcs = row
        .map((text, colIndex) =>
          cell(text, {
            bold: isHeader,
            shade: isHeader ? 'D9E2F3' : null,
            width: widths[colIndex],
          }),
        )
        .join('')
      return `<w:tr>${tcs}</w:tr>`
    })
    .join('')

  return `
    <w:tbl>
      <w:tblPr>
        <w:tblW w:w="0" w:type="auto"/>
        <w:tblBorders>
          <w:top w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:left w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:bottom w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:right w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:insideH w:val="single" w:sz="4" w:space="0" w:color="D9D9D9"/>
          <w:insideV w:val="single" w:sz="4" w:space="0" w:color="D9D9D9"/>
        </w:tblBorders>
      </w:tblPr>
      <w:tblGrid>${tblGrid}</w:tblGrid>
      ${trRows}
    </w:tbl>`
}

const buildDocumentXml = (bodyXml) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
  xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
  xmlns:w10="urn:schemas-microsoft-com:office:word"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
  xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
  xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
  xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
  xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
  mc:Ignorable="w14 wp14">
  <w:body>
    ${bodyXml}
    <w:sectPr>
      <w:pgSz w:w="11906" w:h="16838"/>
      <w:pgMar w:top="1440" w:right="1134" w:bottom="1440" w:left="1134" w:header="708" w:footer="708" w:gutter="0"/>
      <w:cols w:space="708"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>`

const stylesXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:docDefaults>
    <w:rPrDefault>
      <w:rPr>
        <w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>
        <w:sz w:val="22"/>
        <w:szCs w:val="22"/>
      </w:rPr>
    </w:rPrDefault>
    <w:pPrDefault>
      <w:pPr>
        <w:spacing w:after="120"/>
      </w:pPr>
    </w:pPrDefault>
  </w:docDefaults>
  <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
    <w:name w:val="Normal"/>
    <w:qFormat/>
  </w:style>
  <w:style w:type="paragraph" w:styleId="Title">
    <w:name w:val="Title"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:uiPriority w:val="10"/>
    <w:qFormat/>
    <w:rPr>
      <w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>
      <w:b/>
      <w:color w:val="1F4E78"/>
      <w:sz w:val="34"/>
      <w:szCs w:val="34"/>
    </w:rPr>
    <w:pPr>
      <w:jc w:val="center"/>
      <w:spacing w:after="180"/>
    </w:pPr>
  </w:style>
  <w:style w:type="paragraph" w:styleId="Heading1">
    <w:name w:val="Heading 1"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:uiPriority w:val="9"/>
    <w:qFormat/>
    <w:rPr>
      <w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>
      <w:b/>
      <w:color w:val="1F1F1F"/>
      <w:sz w:val="28"/>
      <w:szCs w:val="28"/>
    </w:rPr>
    <w:pPr>
      <w:spacing w:before="180" w:after="120"/>
    </w:pPr>
  </w:style>
  <w:style w:type="paragraph" w:styleId="Heading2">
    <w:name w:val="Heading 2"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:uiPriority w:val="9"/>
    <w:qFormat/>
    <w:rPr>
      <w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>
      <w:b/>
      <w:color w:val="305496"/>
      <w:sz w:val="24"/>
      <w:szCs w:val="24"/>
    </w:rPr>
    <w:pPr>
      <w:spacing w:before="120" w:after="100"/>
    </w:pPr>
  </w:style>
  <w:style w:type="paragraph" w:styleId="Caption">
    <w:name w:val="Caption"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:rPr>
      <w:rFonts w:ascii="Microsoft YaHei" w:hAnsi="Microsoft YaHei" w:eastAsia="Microsoft YaHei" w:cs="Microsoft YaHei"/>
      <w:i/>
      <w:color w:val="666666"/>
      <w:sz w:val="20"/>
      <w:szCs w:val="20"/>
    </w:rPr>
    <w:pPr>
      <w:jc w:val="center"/>
      <w:spacing w:before="60" w:after="120"/>
    </w:pPr>
  </w:style>
</w:styles>`

const contentTypesXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  <Default Extension="xml" ContentType="application/xml"/>
  <Default Extension="png" ContentType="image/png"/>
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
  <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
  <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
  <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
</Types>`

const rootRelsXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>`

const docPropsCoreXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:dcterms="http://purl.org/dc/terms/"
  xmlns:dcmitype="http://purl.org/dc/dcmitype/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <dc:title>安博福Web操作说明手册</dc:title>
  <dc:subject>Web端操作说明</dc:subject>
  <dc:creator>Codex</dc:creator>
  <cp:keywords>Web, 操作说明, 机器人, 地图, 任务</cp:keywords>
  <dc:description>基于当前项目代码整理的图文并茂操作手册</dc:description>
  <cp:lastModifiedBy>Codex</cp:lastModifiedBy>
  <dcterms:created xsi:type="dcterms:W3CDTF">${new Date().toISOString()}</dcterms:created>
  <dcterms:modified xsi:type="dcterms:W3CDTF">${new Date().toISOString()}</dcterms:modified>
</cp:coreProperties>`

const docPropsAppXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
  xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  <Application>Microsoft Word</Application>
  <DocSecurity>0</DocSecurity>
  <ScaleCrop>false</ScaleCrop>
  <HeadingPairs>
    <vt:vector size="2" baseType="variant">
      <vt:variant><vt:lpstr>Sections</vt:lpstr></vt:variant>
      <vt:variant><vt:i4>5</vt:i4></vt:variant>
    </vt:vector>
  </HeadingPairs>
  <TitlesOfParts>
    <vt:vector size="5" baseType="lpstr">
      <vt:lpstr>封面</vt:lpstr>
      <vt:lpstr>系统概览</vt:lpstr>
      <vt:lpstr>模块速览</vt:lpstr>
      <vt:lpstr>图文示例</vt:lpstr>
      <vt:lpstr>使用注意事项</vt:lpstr>
    </vt:vector>
  </TitlesOfParts>
  <Company>Huaheng</Company>
  <LinksUpToDate>false</LinksUpToDate>
  <SharedDoc>false</SharedDoc>
  <HyperlinksChanged>false</HyperlinksChanged>
  <AppVersion>16.0000</AppVersion>
</Properties>`

const relsXml = (rels) => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  ${rels.join('\n  ')}
</Relationships>`

const createTableParagraphs = (rows) =>
  rows.map((row) => {
    const cells = row.map((item) => `<w:tc><w:tcPr><w:tcW w:w="0" w:type="auto"/></w:tcPr>${p(item, { size: 21, spacingBefore: 40, spacingAfter: 40 })}</w:tc>`).join('')
    return `<w:tr>${cells}</w:tr>`
  })

const tableWithHeader = (rows, widths) => {
  const tblGrid = widths.map((w) => `<w:gridCol w:w="${w}"/>`).join('')
  const trRows = rows
    .map((row, rowIndex) => {
      const isHeader = rowIndex === 0
      const cells = row
        .map(
          (item, colIndex) => `<w:tc><w:tcPr><w:tcW w:w="${widths[colIndex]}" w:type="dxa"/>${isHeader ? '<w:shd w:val="clear" w:color="auto" w:fill="D9E2F3"/>' : ''}</w:tcPr>${p(item, {
            size: 21,
            bold: isHeader,
            spacingBefore: 40,
            spacingAfter: 40,
          })}</w:tc>`,
        )
        .join('')
      return `<w:tr>${cells}</w:tr>`
    })
    .join('')

  return `
    <w:tbl>
      <w:tblPr>
        <w:tblW w:w="0" w:type="auto"/>
        <w:tblBorders>
          <w:top w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:left w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:bottom w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:right w:val="single" w:sz="4" w:space="0" w:color="BFBFBF"/>
          <w:insideH w:val="single" w:sz="4" w:space="0" w:color="D9D9D9"/>
          <w:insideV w:val="single" w:sz="4" w:space="0" w:color="D9D9D9"/>
        </w:tblBorders>
      </w:tblPr>
      <w:tblGrid>${tblGrid}</w:tblGrid>
      ${trRows}
    </w:tbl>`
}

const buildDocument = async () => {
  const [logoDim, heroDim] = await Promise.all(images.map(async (img) => ({ ...(await pngDimensions(img.src)), ...img })))
  const logoSize = fitImageSize(logoDim.width, logoDim.height, 2.0)
  const heroSize = fitImageSize(heroDim.width, heroDim.height, 5.8)

  const coverParts = [
    p('安博福Web端操作说明手册', { style: 'Title' }),
    p('图文并茂版', { align: 'center', bold: true, size: 24, color: '5B9BD5', spacingAfter: 160 }),
    p('适用对象:系统管理员、实施运维、调度人员、现场测试人员', {
      align: 'center',
      size: 22,
      spacingAfter: 120,
    }),
    imageParagraph('rId1', 1, 'HUAHENG Logo', logoSize.cx, logoSize.cy),
    p('生成说明:基于当前项目代码与页面结构整理', { align: 'center', size: 20, color: '666666', spacingAfter: 60 }),
    p(`源文件:${path.relative(rootDir, manualSource)}`, { align: 'center', size: 20, color: '666666' }),
    pageBreak(),
  ]

  const overviewTable = tableWithHeader(
    [
      ['模块', '主要用途', '典型操作'],
      ['数据监控', '实时查看地图、机器人状态并下发任务', '选地图、选起点/终点、执行任务、控制机器人'],
      ['地图管理', '维护地图基础信息与画布数据', '新增、编辑、复制、进画布、同步资源'],
      ['机器人管理', '维护机器人主数据和缓存储位', '新增、编辑、批量删除、查看状态与错误'],
      ['动作配置', '定义机器人动作及参数', '维护动作类别、执行范围、阻塞类型、参数'],
      ['请求配置', '配置外部接口调用模板', '维护请求方式、地址、参数与验证规则'],
      ['任务模板', '编排完整任务流程', '拖拽移动步骤与动作、保存模板'],
      ['任务管理', '创建与控制业务任务', '新增任务、查看子任务、重试、手动完成'],
      ['储位管理', '查看储位状态与属性', '查询空闲/占用/预留/禁用状态'],
      ['充电桩管理', '维护充电与回充策略', '配置阈值、机器人绑定、优先级'],
      ['疲劳测试', '持续生成任务验证稳定性', '保存配置、启动测试、停止测试'],
      ['日志下载', '按时间范围查询并下载日志', '查询、全选、下载选中'],
    ],
    [2200, 5200, 4200],
  )

  const opSteps = [
    '先维护请求配置、动作配置、储位类型等基础数据。',
    '进入地图管理维护地图,再打开画布完善节点、连线、资源和背景图。',
    '完善机器人、储位、充电桩等基础对象后,再编排任务模板。',
    '进入数据监控页选择地图,确认机器人联机与库位状态正常。',
    '在任务管理页或监控页创建任务,必要时结合日志下载排查问题。',
  ]

  const noteRows = [
    ['注意项', '说明'],
    ['保存优先', '地图画布、模板编辑、机器人编辑等页面修改后必须保存,否则刷新会丢失。'],
    ['批量操作', '批量删除、批量编辑、CSV 导入前建议先在测试数据上验证。'],
    ['前置依赖', '任务模板依赖动作配置和请求配置;疲劳测试依赖地图、机器人和库位。'],
    ['移动端', '移动端只保留了搬运任务和库位释放等轻量功能。'],
  ]

  const manualParts = [
    p('1. 系统概览', { style: 'Heading1' }),
    p('本系统用于机器人业务的地图管理、任务调度、监控联动和基础数据维护。', { size: 22, spacingAfter: 80 }),
    p('推荐使用顺序', { style: 'Heading2' }),
    ...opSteps.map((s) => bullet(s)),
    p('关键注意事项', { style: 'Heading2' }),
    tableWithHeader(noteRows, [2400, 10000]),
    p('2. 模块速览', { style: 'Heading1' }),
    p('下面的表格汇总了各核心模块的作用,便于你快速定位入口。', { size: 22, spacingAfter: 80 }),
    overviewTable,
    pageBreak(),
    p('3. 图文示例', { style: 'Heading1' }),
    p('以下图片取自当前项目资源,可作为封面与视觉说明示例。', { size: 22, spacingAfter: 80 }),
    imageParagraph('rId2', 2, 'Project Hero Image', heroSize.cx, heroSize.cy),
    p('项目视觉图示', { style: 'Caption' }),
    imageParagraph('rId1', 3, 'HUAHENG Logo Duplicate', logoSize.cx, logoSize.cy),
    p('企业标识示例', { style: 'Caption' }),
    p('4. 操作重点', { style: 'Heading1' }),
    p('数据监控页支持选择地图、查看机器人状态、执行任务、重定位和库位释放。', { size: 22, spacingAfter: 60 }),
    p('地图编辑器支持加点、连线、圆弧、复制、平移、批量编辑、导入导出节点等操作。', {
      size: 22,
      spacingAfter: 60,
    }),
    p('任务模板通过拖拽方式组织移动步骤与动作,并在右侧属性面板维护请求与阻塞规则。', {
      size: 22,
      spacingAfter: 60,
    }),
    p('任务管理页可以直接新增任务、查看子任务、重试任务和手动完成任务。', {
      size: 22,
      spacingAfter: 60,
    }),
    p('5. 来源文件', { style: 'Heading1' }),
    p(`源说明文档:${path.relative(rootDir, manualSource)}`, { size: 22 }),
  ]

  const body = [
    ...coverParts,
    ...manualParts,
  ].join('\n')

  const documentXml = buildDocumentXml(body)

  const docRelsXml = relsXml([
    `<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/${images[0].name}"/>`,
    `<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/${images[1].name}"/>`,
  ])

  return { documentXml, docRelsXml, logoDim, heroDim }
}

const writePackage = async () => {
  await fs.rm(buildDir, { recursive: true, force: true })
  await fs.mkdir(path.join(buildDir, '_rels'), { recursive: true })
  await fs.mkdir(path.join(buildDir, 'docProps'), { recursive: true })
  await fs.mkdir(path.join(buildDir, 'word', '_rels'), { recursive: true })
  await fs.mkdir(path.join(buildDir, 'word', 'media'), { recursive: true })

  const { documentXml, docRelsXml } = await buildDocument()

  await fs.writeFile(path.join(buildDir, '[Content_Types].xml'), contentTypesXml, 'utf8')
  await fs.writeFile(path.join(buildDir, '_rels', '.rels'), rootRelsXml, 'utf8')
  await fs.writeFile(path.join(buildDir, 'docProps', 'core.xml'), docPropsCoreXml, 'utf8')
  await fs.writeFile(path.join(buildDir, 'docProps', 'app.xml'), docPropsAppXml, 'utf8')
  await fs.writeFile(path.join(buildDir, 'word', 'document.xml'), documentXml, 'utf8')
  await fs.writeFile(path.join(buildDir, 'word', '_rels', 'document.xml.rels'), docRelsXml, 'utf8')
  await fs.writeFile(path.join(buildDir, 'word', 'styles.xml'), stylesXml, 'utf8')

  for (const img of images) {
    await fs.copyFile(img.src, path.join(buildDir, 'word', 'media', img.name))
  }

  await fs.mkdir(docsDir, { recursive: true })

  const tarResult = spawnSync(
    'tar',
    ['-a', '-c', '-f', outputFile, '-C', buildDir, '[Content_Types].xml', '_rels', 'docProps', 'word'],
    {
      cwd: rootDir,
      stdio: 'inherit',
      shell: false,
    },
  )

  if (tarResult.status !== 0) {
    throw new Error(`Failed to package DOCX, tar exit code: ${tarResult.status}`)
  }
}

try {
  await writePackage()
  console.log(`DOCX generated: ${outputFile}`)
} catch (error) {
  console.error(error)
  process.exitCode = 1
}