|
| 1 | +import { readdir, rename } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | + |
| 4 | +const DIST_DIR = 'dist/apps/blog'; |
| 5 | +const SKIP_FOLDERS = ['assets', 'images', 'styles', 'scripts']; |
| 6 | + |
| 7 | +async function renamePrerenderedFiles(dirPath = DIST_DIR) { |
| 8 | + try { |
| 9 | + const entries = await readdir(dirPath, { withFileTypes: true }); |
| 10 | + |
| 11 | + for (const entry of entries) { |
| 12 | + if (entry.isDirectory()) { |
| 13 | + const currentDirPath = join(dirPath, entry.name); |
| 14 | + |
| 15 | + if (SKIP_FOLDERS.includes(entry.name)) { |
| 16 | + continue; |
| 17 | + } |
| 18 | + |
| 19 | + const files = await readdir(currentDirPath); |
| 20 | + if (files.length === 0) { |
| 21 | + continue; |
| 22 | + } |
| 23 | + |
| 24 | + if (files.length > 1) { |
| 25 | + console.error(`Multiple files in ${currentDirPath}:`, files); |
| 26 | + await renamePrerenderedFiles(currentDirPath); |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const indexPath = join(currentDirPath, 'index.html'); |
| 31 | + |
| 32 | + try { |
| 33 | + await rename(indexPath, join(currentDirPath, `${entry.name}.html`)); |
| 34 | + console.log(`Renamed ${indexPath} to ${entry.name}.html`); |
| 35 | + } catch (error) { |
| 36 | + if (error.code !== 'ENOENT') { |
| 37 | + console.error(`Error processing ${currentDirPath}:`, error); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + console.error('Error during post-prerender processing:', error); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +renamePrerenderedFiles(); |
0 commit comments