|
| 1 | +import glob from 'glob' |
| 2 | +import fs from 'fs' |
| 3 | +// Utils |
| 4 | +import { findTargetString, dashToCamel } from '../utils/stringUtils' |
| 5 | + |
| 6 | +/* --- Template ------------------------------------------------------------------------------- */ |
| 7 | + |
| 8 | +let iconRegistry = `// -i- Auto generated with 'yarn ats collect-icons' |
| 9 | +{{iconRegistryImports}} |
| 10 | +
|
| 11 | +/* --- Exports --------------------------------------------------------------------------------- */ |
| 12 | +
|
| 13 | +export const REGISTERED_ICONS = { |
| 14 | + {{iconRegistryExports}} |
| 15 | +} as const // prettier-ignore |
| 16 | +` |
| 17 | + |
| 18 | +/* --- collect-resolvers ----------------------------------------------------------------------- */ |
| 19 | + |
| 20 | +const collectResolvers = () => { |
| 21 | + try { |
| 22 | + // Get all resolver file paths in the next app's api folder |
| 23 | + const featureIconRegistries = glob.sync('../../features/**/icons/registry.tsx') |
| 24 | + const packageIconRegistries = glob.sync('../../packages/**/icons/registry.tsx') |
| 25 | + const allIconRegistries = [...featureIconRegistries, ...packageIconRegistries] |
| 26 | + |
| 27 | + // Collect all icon registry export parts |
| 28 | + const iconRegistryImports = [] as string[] |
| 29 | + const iconRegistryExports = [] as string[] |
| 30 | + allIconRegistries.forEach((iconRegistryPath) => { |
| 31 | + const importPath = iconRegistryPath.replace('.tsx', '') |
| 32 | + const importType = iconRegistryPath.includes('features') ? 'features' : 'packages' |
| 33 | + const importWorkspace = findTargetString(importPath, `${importType}/$target$/icons/registry`) |
| 34 | + const importAlias = `${dashToCamel(importWorkspace!)}Icons` |
| 35 | + iconRegistryImports.push(`import { iconRegistry as ${importAlias} } from '${importPath}'`) |
| 36 | + iconRegistryExports.push(`...${importAlias},`) |
| 37 | + }) |
| 38 | + |
| 39 | + // Write iconRegistry file to 'packages/@registries/icons.generated.ts' |
| 40 | + iconRegistry = iconRegistry.replace('{{iconRegistryImports}}', iconRegistryImports.join('\n')) |
| 41 | + iconRegistry = iconRegistry.replace('{{iconRegistryExports}}', iconRegistryExports.join('\n ')) // prettier-ignore |
| 42 | + fs.writeFileSync('../../packages/@registries/icons.generated.ts', iconRegistry) |
| 43 | + |
| 44 | + // Log success |
| 45 | + console.log('-----------------------------------------------------------------') |
| 46 | + console.log('-i- Successfully created icon registry at:') |
| 47 | + console.log('-----------------------------------------------------------------') |
| 48 | + console.log(' ✅ packages/@registries/icons.generated.ts') |
| 49 | + } catch (err) { |
| 50 | + console.log(err) |
| 51 | + console.error(err) |
| 52 | + process.exit(1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/* --- init ------------------------------------------------------------------------------------ */ |
| 57 | + |
| 58 | +collectResolvers() |
0 commit comments