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); });
|