|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs/promises' |
| 4 | +import {fileURLToPath} from 'node:url' |
| 5 | +import path from 'node:path' |
| 6 | +import babel from '@babel/core' |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url) |
| 9 | +const __dirname = path.dirname(__filename) |
| 10 | + |
| 11 | +const PACKAGE_ROOT = path.resolve(__dirname, '..') |
| 12 | +async function generateComponentsJson() { |
| 13 | + const components = new Set() |
| 14 | + const types = new Set() |
| 15 | + const utilities = new Set() |
| 16 | + |
| 17 | + const entrypoints = ['src/index.tsx', 'src/deprecated.tsx', 'src/experimental.tsx'] |
| 18 | + |
| 19 | + for (const entrypoint of entrypoints) { |
| 20 | + const filename = path.join(PACKAGE_ROOT, entrypoint) |
| 21 | + const contents = await fs.readFile(filename, 'utf8') |
| 22 | + const ast = babel.parse(contents, { |
| 23 | + sourceFileName: filename, |
| 24 | + sourceType: 'module', |
| 25 | + parserOpts: { |
| 26 | + plugins: ['typescript', 'jsx'], |
| 27 | + }, |
| 28 | + }) |
| 29 | + |
| 30 | + babel.traverse(ast, { |
| 31 | + // export { XYZ } |
| 32 | + // export type { XYZ } |
| 33 | + ExportNamedDeclaration(path) { |
| 34 | + for (const specifier of path.node.specifiers) { |
| 35 | + const exported = specifier.exported.name |
| 36 | + const exportKind = path.node.exportKind === 'type' ? 'type' : specifier.exportKind |
| 37 | + |
| 38 | + if (exportKind === 'type') { |
| 39 | + types.add(exported) |
| 40 | + } else if (exported[0] === exported[0].toLowerCase()) { |
| 41 | + utilities.add(exported) |
| 42 | + } else { |
| 43 | + components.add(exported) |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + await fs.writeFile( |
| 51 | + path.join(PACKAGE_ROOT, 'dist', 'components.json'), |
| 52 | + JSON.stringify( |
| 53 | + { |
| 54 | + components: Array.from(components).sort((a, b) => a.localeCompare(b)), |
| 55 | + types: Array.from(types).sort((a, b) => a.localeCompare(b)), |
| 56 | + utilities: Array.from(utilities).sort((a, b) => a.localeCompare(b)), |
| 57 | + }, |
| 58 | + null, |
| 59 | + 2, |
| 60 | + ), |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +generateComponentsJson() |
0 commit comments