|
| 1 | +import { readdir, readFile, writeFile } from 'node:fs/promises' |
| 2 | +import { dirname, join, resolve } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import Handlebars from 'handlebars' |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 7 | + |
| 8 | +// This template is used to generate the files. |
| 9 | +const template = Handlebars.compile( |
| 10 | + await readFile(resolve(__dirname, './templates/template.hbs'), { encoding: 'utf8' }), |
| 11 | + { strict: true }, |
| 12 | +) |
| 13 | + |
| 14 | +// Begin traversal at this directory. |
| 15 | +const dirsToScan = [resolve(__dirname, '../src/')] |
| 16 | + |
| 17 | +// Marker for partial templates. |
| 18 | +const PARTIALS_DIR = '_partials' |
| 19 | + |
| 20 | +// Memory to build index files for partials. |
| 21 | +const partialsIndices: Record<string, string[]> = {} |
| 22 | + |
| 23 | +// This template is used to generate the partial index files. |
| 24 | +const partialIndex = Handlebars.compile( |
| 25 | + await readFile(resolve(__dirname, './templates/register.hbs'), { encoding: 'utf8' }), |
| 26 | + { strict: true }, |
| 27 | +) |
| 28 | + |
| 29 | +while (dirsToScan.length > 0) { |
| 30 | + const dir = dirsToScan.shift() |
| 31 | + if (dir) { |
| 32 | + // Get directory contents. |
| 33 | + const entries = await readdir(dir, { withFileTypes: true }) |
| 34 | + |
| 35 | + for (const entry of entries) { |
| 36 | + const fullPath = join(entry.parentPath, entry.name) |
| 37 | + |
| 38 | + if (entry.isDirectory()) { |
| 39 | + // Enqueue directories into the list to check. This does a breadth-first search. |
| 40 | + dirsToScan.push(fullPath) |
| 41 | + } |
| 42 | + else if (entry.name.endsWith('.hbs')) { |
| 43 | + // Precompile the template... |
| 44 | + const templateSpec = Handlebars.precompile(await readFile(fullPath, { encoding: 'utf8' }), { |
| 45 | + strict: true, |
| 46 | + }) |
| 47 | + |
| 48 | + // ... and save it into a file. |
| 49 | + await writeFile( |
| 50 | + `${fullPath}.ts`, |
| 51 | + template({ |
| 52 | + templateSpec, |
| 53 | + }), |
| 54 | + { encoding: 'utf8' }, |
| 55 | + ) |
| 56 | + |
| 57 | + // Check if this is a partial template. |
| 58 | + const partialsIdx = fullPath.indexOf(PARTIALS_DIR) |
| 59 | + if (partialsIdx > -1) { |
| 60 | + const partialDir = fullPath.slice(0, partialsIdx + PARTIALS_DIR.length) |
| 61 | + const partialName = fullPath.slice(partialsIdx + PARTIALS_DIR.length + 1) |
| 62 | + |
| 63 | + const index = partialsIndices[partialDir] ?? (partialsIndices[partialDir] = []) |
| 64 | + index.push(partialName) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Generate index files for partials that register them on import. |
| 72 | +for (const [dir, partials] of Object.entries(partialsIndices)) { |
| 73 | + await writeFile( |
| 74 | + join(dir, 'register__generated.ts'), |
| 75 | + partialIndex({ |
| 76 | + partials, |
| 77 | + }), |
| 78 | + { encoding: 'utf8' }, |
| 79 | + ) |
| 80 | +} |
0 commit comments