|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | + |
| 4 | +const SRC_DIR = path.resolve(__dirname, '../docs/.vitepress/dist'); |
| 5 | +const DEST_DIR = path.resolve(__dirname, '../doc'); |
| 6 | + |
| 7 | +function collectMdFiles(dir, base = '') { |
| 8 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 9 | + const files = []; |
| 10 | + for (const entry of entries) { |
| 11 | + const relPath = base ? `${base}/${entry.name}` : entry.name; |
| 12 | + const fullPath = path.join(dir, entry.name); |
| 13 | + if (entry.isDirectory()) { |
| 14 | + files.push(...collectMdFiles(fullPath, relPath)); |
| 15 | + } else if (entry.name.endsWith('.md')) { |
| 16 | + files.push({ relPath, fullPath, name: entry.name.replace('.md', '') }); |
| 17 | + } |
| 18 | + } |
| 19 | + return files; |
| 20 | +} |
| 21 | + |
| 22 | +if (!fs.existsSync(SRC_DIR)) { |
| 23 | + console.error(`Source directory not found: ${SRC_DIR}`); |
| 24 | + console.error('Please run "npm run docs:build" first.'); |
| 25 | + // eslint-disable-next-line node/prefer-global/process |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +fs.rmSync(DEST_DIR, { recursive: true, force: true }); |
| 30 | +fs.mkdirSync(DEST_DIR, { recursive: true }); |
| 31 | + |
| 32 | +const files = collectMdFiles(SRC_DIR); |
| 33 | + |
| 34 | +const indexLines = ['# Document Index', '']; |
| 35 | + |
| 36 | +for (const file of files) { |
| 37 | + const destSubDir = path.join(DEST_DIR, path.dirname(file.relPath)); |
| 38 | + fs.mkdirSync(destSubDir, { recursive: true }); |
| 39 | + fs.copyFileSync(file.fullPath, path.join(DEST_DIR, file.relPath)); |
| 40 | + indexLines.push(`- [${file.name}](${file.relPath})`); |
| 41 | +} |
| 42 | + |
| 43 | +fs.writeFileSync(path.join(DEST_DIR, 'index.md'), `${indexLines.join('\n')}\n`); |
| 44 | + |
| 45 | +console.log(`Copied ${files.length} .md files to ${DEST_DIR}`); |
| 46 | +console.log(`Generated index.md with ${files.length} entries`); |
0 commit comments