import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { generateSvgIcon } from './generate-icon'; import { getSvgFiles, readSvgCategories, writeFile } from './helper'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); interface BuildOptions { /** svg 资源目录 */ svgAssetDir: string; /** 生成组件路径 */ outputIconPath: string; } async function buildNativeSvgIcon(options: BuildOptions) { if (!fs.existsSync(options.svgAssetDir)) { throw new Error('❌ Not found svg icons.'); } const svgModules: Record = {}; let exportIconsCode = '/** Export All Svg Icons */\n\n'; const svgCategories = await readSvgCategories(options.svgAssetDir); for (const category of svgCategories) { svgModules[category] = await getSvgFiles( path.resolve(options.svgAssetDir, category), ); } if (!fs.existsSync(options.outputIconPath)) { fs.mkdirSync(options.outputIconPath); } const iconsDirectory = path.basename(options.outputIconPath); for (const [category, svgFiles] of Object.entries(svgModules)) { for (const svgFile of svgFiles) { const [component, moduleName] = await generateSvgIcon({ category, source: svgFile, output: options.outputIconPath, }); exportIconsCode += `export { default as ${component} } from './${iconsDirectory}/${moduleName}';\n`; } } const exportModulePath = path.dirname(options.outputIconPath); writeFile(path.join(exportModulePath, 'index.ts'), exportIconsCode); } buildNativeSvgIcon({ svgAssetDir: path.resolve(__dirname, '../icons'), outputIconPath: path.resolve(__dirname, '../src/icons'), });