gaoluyang
6 天以前 80ef3a1fb42f37cde2e9753317e118c50379b095
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
const fs = require('fs');
const path = require('path');
const { Document, Packer, Paragraph, TextRun, HeadingLevel } = require('docx');
 
const ROOT = path.join(__dirname, '..', '..');
const OUT = path.join(ROOT, '系统模块源代码_纯页面.docx');
 
const viewDirs = [
  'src/views/basicData',
  'src/views/salesManagement',
  'src/views/procurementManagement',
  'src/views/personnelManagement',
  'src/views/inventoryManagement',
  'src/views/equipmentManagement',
];
 
const CODE_FONT = { ascii: 'Consolas', hAnsi: 'Consolas', eastAsia: '微软雅黑' };
const PATH_FONT = { ascii: 'Consolas', hAnsi: 'Consolas', eastAsia: '微软雅黑' };
 
function collectVue(relDir) {
  const abs = path.join(ROOT, relDir);
  const result = [];
  (function walk(d) {
    for (const e of fs.readdirSync(d, { withFileTypes: true })) {
      const full = path.join(d, e.name);
      if (e.isDirectory()) walk(full);
      else if (e.name.endsWith('.vue')) result.push(full);
    }
  })(abs);
  return result;
}
 
// 去掉 <style> ... </style> 样式块
function stripStyle(code) {
  return code.replace(/<style[\s\S]*?<\/style\s*>/g, '');
}
 
function codeParagraph(code) {
  const lines = code.split(/\r?\n/);
  while (lines.length && lines[lines.length - 1] === '') lines.pop();
  const runs = lines.map((line, i) => {
    const last = i === lines.length - 1;
    return new TextRun({ text: line, font: CODE_FONT, size: 16, break: last ? undefined : 1 });
  });
  return new Paragraph({ children: runs, spacing: { before: 40, after: 200 } });
}
 
async function main() {
  const files = [];
  for (const d of viewDirs) {
    for (const abs of collectVue(d)) {
      files.push({ abs, rel: path.relative(ROOT, abs).replace(/\\/g, '/') });
    }
  }
  files.sort((a, b) => a.rel.localeCompare(b.rel));
 
  const children = [];
  for (const f of files) {
    children.push(new Paragraph({
      heading: HeadingLevel.HEADING_1,
      children: [new TextRun({ text: f.rel, font: PATH_FONT })],
    }));
    children.push(codeParagraph(stripStyle(fs.readFileSync(f.abs, 'utf8'))));
  }
 
  const doc = new Document({
    styles: { default: { document: { run: { font: CODE_FONT, size: 16 } } } },
    sections: [{ properties: {}, children }],
  });
  const buf = await Packer.toBuffer(doc);
  fs.writeFileSync(OUT, buf);
  console.log('已生成:', OUT);
  console.log('文件数:', files.length);
  console.log('大小:', (buf.length / 1024).toFixed(0), 'KB');
}
 
main().catch((e) => { console.error(e); process.exit(1); });