at main 2.0 kB view raw
1import fs from 'fs'; 2import path from 'path'; 3import { renderIconsObject } from '@lucide/build-icons'; 4import { readSvgDirectory, toCamelCase } from '@lucide/helpers'; 5 6const currentDir = process.cwd(); 7const ICONS_DIR = path.resolve(currentDir, '../icons'); 8const svgFiles = readSvgDirectory(ICONS_DIR); 9const icons = renderIconsObject(svgFiles, ICONS_DIR, true); 10 11const iconNodesDirectory = path.resolve(currentDir, '.vitepress/data', 'iconNodes'); 12 13if (fs.existsSync(iconNodesDirectory)) { 14 fs.rmSync(iconNodesDirectory, { recursive: true, force: true }); 15} 16 17if (!fs.existsSync(iconNodesDirectory)) { 18 fs.mkdirSync(iconNodesDirectory); 19} 20 21const iconIndexFile = path.resolve(iconNodesDirectory, `index.ts`); 22const iconIndexFileImports = []; 23const iconIndexFileExports = []; 24const iconIndexFileDefaultExports = []; 25 26const writeIconFiles = Object.entries(icons).map(async ([iconName, { children }]) => { 27 // We need to use .node.json because the there is a file called package, which is a reserved word for packages. 28 const location = path.resolve(iconNodesDirectory, `${iconName}.node.json`); 29 const iconNode = children.map(({ name, attributes }) => [name, attributes]); 30 31 const output = JSON.stringify(iconNode, null, 2); 32 await fs.promises.writeFile(location, output, 'utf-8'); 33 34 iconIndexFileImports.push( 35 `import ${toCamelCase(iconName)}Node from './${iconName}.node.json' assert { type: "json" };`, 36 ); 37 iconIndexFileExports.push(` ${toCamelCase(iconName)}Node as ${toCamelCase(iconName)},`); 38 iconIndexFileDefaultExports.push(` '${iconName}': ${toCamelCase(iconName)}Node,`); 39}); 40 41try { 42 await Promise.all(writeIconFiles); 43 await fs.promises.writeFile( 44 iconIndexFile, 45 `\ 46${iconIndexFileImports.join('\n')} 47 48export { 49${iconIndexFileExports.join('\n')} 50} 51 52export default { 53${iconIndexFileDefaultExports.join('\n')} 54} 55 `, 56 'utf-8', 57 ); 58 59 console.log('Successfully write', writeIconFiles.length, 'iconNodes.'); 60} catch (error) { 61 throw new Error(`Something went wrong generating iconNode files,\n ${error}`); 62}