/** * Generate `src/generated/names.ts` from the sources of `dou-ui`, the * packages built on it and `@dou-ui/icons`: every exported component with * where it is imported from and its style entry, and every icon component * name. */ import { mkdir, readdir } from 'node:fs/promises' const root = new URL('../', import.meta.url) const components = new URL('../dou-ui/src/components/', root) const icons = new URL('../icons/icons/', root) const componentDirs: Record = {} for (const dir of (await readdir(components, { withFileTypes: true })).filter((e) => e.isDirectory(), )) { const index = Bun.file(new URL(`${dir.name}/index.ts`, components)) if (!(await index.exists())) continue for (const component of exportedComponents(await index.text())) { componentDirs[component] = dir.name } } const pascal = (name: string) => name.replace(/(^|-)([a-z0-9])/g, (_, __: string, char: string) => char.toUpperCase()) const iconNames = (await readdir(icons)) .filter((file) => file.endsWith('.stroke.svg')) .map((file) => `${pascal(file.replace('.stroke.svg', ''))}Icon`) .toSorted() // Packages built on `dou-ui`: each one ships a `style` entry and exports its // components from the root, or from a subpath (`@dou-ui/gantt/scheduler`). const packageComponents: Record = {} const packagesDir = new URL('../', root) for (const dir of (await readdir(packagesDir, { withFileTypes: true })).filter((e) => e.isDirectory(), )) { const manifest = Bun.file(new URL(`${dir.name}/package.json`, packagesDir)) if (!(await manifest.exists())) continue const { name, exports = {} } = await manifest.json() if (!exports['./style'] || name === 'dou-ui') continue const src = new URL(`${dir.name}/src/`, packagesDir) // Subpaths first: a component that also has a lighter entry of its own // (`@dou-ui/form-builder/renderer`) is imported from there. const entries = Object.keys(exports) .filter((key) => key === '.' || /^\.\/[a-z-]+$/.test(key)) .filter((key) => !['./style', './style.css'].includes(key)) .toSorted((a, b) => (a === '.' ? 1 : b === '.' ? -1 : a < b ? -1 : 1)) for (const key of entries) { const from = key === '.' ? name : `${name}${key.slice(1)}` const index = new URL(key === '.' ? 'index.ts' : `${key.slice(2)}/index.ts`, src) for (const component of await componentsOf(index)) { // A subpath with a style entry of its own brings only what it uses. const style = key !== '.' && exports[`${key}/style`] ? `${from}/style` : `${name}/style` packageComponents[component] ??= { package: name, from, style } } } } /** * The components a module wraps with `withInstall` and exports, and those it * passes on with `export { DouXxx }` from a module of its own (a component * other components render is wrapped apart from the entry, see `dropdown/item-components.ts`). */ function exportedComponents(code: string): string[] { const names = [...code.matchAll(/^export const (Dou[A-Za-z]+) = withInstall/gm)].map((m) => m[1]!) for (const m of code.matchAll(/^export \{([^}]*)\}/gm)) { names.push( ...m[1]! .split(',') .map((name) => name.trim()) .filter((name) => /^Dou[A-Z]/.test(name)), ) } return names } /** The components an entry exports, following `export * from './dir'`. */ async function componentsOf(index: URL): Promise { const file = Bun.file(index) if (!(await file.exists())) return [] const code = await file.text() const names = exportedComponents(code) for (const m of code.matchAll(/^export \* from '(\.[^']+)'/gm)) { names.push(...(await componentsOf(new URL(`${m[1]}/index.ts`, index)))) } return names } const byName = (record: Record) => Object.fromEntries(Object.entries(record).toSorted(([a], [b]) => (a < b ? -1 : 1))) const sorted = byName(componentDirs) const code = `// Generated by scripts/gen.ts. Do not edit. /** Every component \`dou-ui\` exports, with the directory of its style entry. */ export const componentDirs: Record = ${JSON.stringify(sorted, null, 2)} /** Every component of the packages built on \`dou-ui\`, its package, where to import it from and its style entry. */ export const packageComponents: Record = ${JSON.stringify(byName(packageComponents), null, 2)} /** Every icon component \`@dou-ui/icons\` exports. */ export const iconNames: readonly string[] = ${JSON.stringify(iconNames, null, 2)} ` const out = new URL('src/generated/', root) await mkdir(out, { recursive: true }) await Bun.write(new URL('names.ts', out), code)