|
| 1 | +#!/usr/bin/env node |
| 2 | +import fg from 'fast-glob'; |
| 3 | +import {fileURLToPath, pathToFileURL} from 'url'; |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | +const REPO_ROOT = path.resolve(__dirname, '../../../..'); |
| 10 | +const OUT_DIR = path.resolve(__dirname, '../dist/data'); |
| 11 | + |
| 12 | +const ICONS_DIR = path.resolve(REPO_ROOT, 'packages/@react-spectrum/s2/s2wf-icons'); |
| 13 | +const ILLUSTRATIONS_DIR = path.resolve(REPO_ROOT, 'packages/@react-spectrum/s2/spectrum-illustrations/linear'); |
| 14 | +const ICON_ALIASES_JS = path.resolve(REPO_ROOT, 'packages/dev/s2-docs/src/iconAliases.js'); |
| 15 | +const ILLUSTRATION_ALIASES_JS = path.resolve(REPO_ROOT, 'packages/dev/s2-docs/src/illustrationAliases.js'); |
| 16 | + |
| 17 | +function ensureDir(p) { |
| 18 | + fs.mkdirSync(p, {recursive: true}); |
| 19 | +} |
| 20 | + |
| 21 | +function writeJson(file, data) { |
| 22 | + ensureDir(path.dirname(file)); |
| 23 | + fs.writeFileSync(file, JSON.stringify(data, null, 2) + '\n', 'utf8'); |
| 24 | + console.log('Wrote', path.relative(REPO_ROOT, file)); |
| 25 | +} |
| 26 | + |
| 27 | +function buildIconNames() { |
| 28 | + if (!fs.existsSync(ICONS_DIR)) {return null;} |
| 29 | + const files = fg.sync('*.svg', {cwd: ICONS_DIR, absolute: false, suppressErrors: true}); |
| 30 | + const ids = Array.from(new Set( |
| 31 | + files.map(f => f.replace(/\.svg$/i, '').replace(/^S2_Icon_(.*?)(Size\d+)?_2.*/, '$1')) |
| 32 | + )).sort((a, b) => a.localeCompare(b)); |
| 33 | + return ids; |
| 34 | +} |
| 35 | + |
| 36 | +function buildIllustrationNames() { |
| 37 | + if (!fs.existsSync(ILLUSTRATIONS_DIR)) {return null;} |
| 38 | + const files = fg.sync('**/*.svg', {cwd: ILLUSTRATIONS_DIR, absolute: false, suppressErrors: true}); |
| 39 | + const ids = Array.from(new Set( |
| 40 | + files.map(f => { |
| 41 | + const base = f.replace(/\.svg$/i, '').replace(/^S2_lin_(.*)_\d+$/, '$1'); |
| 42 | + return base ? (base.charAt(0).toUpperCase() + base.slice(1)) : base; |
| 43 | + }) |
| 44 | + )).sort((a, b) => a.localeCompare(b)); |
| 45 | + return ids; |
| 46 | +} |
| 47 | + |
| 48 | +async function loadAliases(modPath, exportName) { |
| 49 | + if (!fs.existsSync(modPath)) {return {};} |
| 50 | + const mod = await import(pathToFileURL(modPath).href); |
| 51 | + return mod[exportName] ?? {}; |
| 52 | +} |
| 53 | + |
| 54 | +async function main() { |
| 55 | + const icons = buildIconNames(); |
| 56 | + const illustrations = buildIllustrationNames(); |
| 57 | + const iconAliases = await loadAliases(ICON_ALIASES_JS, 'iconAliases'); |
| 58 | + const illustrationAliases = await loadAliases(ILLUSTRATION_ALIASES_JS, 'illustrationAliases'); |
| 59 | + |
| 60 | + if (icons && icons.length) { |
| 61 | + writeJson(path.join(OUT_DIR, 'icons.json'), icons); |
| 62 | + } |
| 63 | + if (illustrations && illustrations.length) { |
| 64 | + writeJson(path.join(OUT_DIR, 'illustrations.json'), illustrations); |
| 65 | + } |
| 66 | + if (iconAliases && Object.keys(iconAliases).length) { |
| 67 | + writeJson(path.join(OUT_DIR, 'iconAliases.json'), iconAliases); |
| 68 | + } |
| 69 | + if (illustrationAliases && Object.keys(illustrationAliases).length) { |
| 70 | + writeJson(path.join(OUT_DIR, 'illustrationAliases.json'), illustrationAliases); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +main().catch((err) => { |
| 75 | + console.error(err?.stack || String(err)); |
| 76 | + process.exit(1); |
| 77 | +}); |
0 commit comments