|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +/** |
| 5 | + * Generate TypeScript exports for Galaxy custom icons |
| 6 | + */ |
| 7 | +function generateIconsTypeScript() { |
| 8 | + // Read the generated icons JSON |
| 9 | + const iconsJsonPath = path.resolve(__dirname, "../src/assets/icons.json"); |
| 10 | + const outputPath = path.resolve(__dirname, "../src/components/icons/galaxyIcons.ts"); |
| 11 | + |
| 12 | + if (!fs.existsSync(iconsJsonPath)) { |
| 13 | + console.error(`Icons JSON not found at ${iconsJsonPath}`); |
| 14 | + console.error("Run 'npm run icons' first to generate the icons JSON"); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const iconsJson = fs.readFileSync(iconsJsonPath, "utf8"); |
| 19 | + const icons = JSON.parse(iconsJson); |
| 20 | + |
| 21 | + // Generate TypeScript content |
| 22 | + let content = `import type { IconDefinition } from "@fortawesome/fontawesome-svg-core"; |
| 23 | +
|
| 24 | +// Galaxy custom icons |
| 25 | +// Generated from: client/src/assets/icons.json |
| 26 | +// To regenerate: node icons/generate_ts_icons.js |
| 27 | +
|
| 28 | +// Matches FontAwesome's structure |
| 29 | +export interface GalaxyIconDefinition { |
| 30 | + iconName: string; |
| 31 | + prefix: string; |
| 32 | + icon: [number, number, never[], string, string | string[]]; |
| 33 | +} |
| 34 | +
|
| 35 | +// Accepts both FA and Galaxy icons |
| 36 | +export type IconLike = IconDefinition | GalaxyIconDefinition; |
| 37 | +
|
| 38 | +`; |
| 39 | + |
| 40 | + icons.forEach((icon) => { |
| 41 | + // Convert icon name to camelCase for JavaScript export |
| 42 | + const exportName = icon.iconName; |
| 43 | + |
| 44 | + // Format the icon data manually to match the expected style |
| 45 | + const iconData = icon.icon; |
| 46 | + const formattedIcon = `{ |
| 47 | + iconName: "${icon.iconName}", |
| 48 | + prefix: "${icon.prefix}", |
| 49 | + icon: [ |
| 50 | + ${iconData[0]}, |
| 51 | + ${iconData[1]}, |
| 52 | + [], |
| 53 | + "", |
| 54 | + [ |
| 55 | + ${iconData[4].map((path) => `"${path}"`).join(",\n ")}, |
| 56 | + ], |
| 57 | + ], |
| 58 | +};`; |
| 59 | + |
| 60 | + content += `export const ${exportName}: GalaxyIconDefinition = ${formattedIcon} |
| 61 | +
|
| 62 | +`; |
| 63 | + }); |
| 64 | + |
| 65 | + // Write the TypeScript file |
| 66 | + fs.writeFileSync(outputPath, content.trimEnd() + "\n"); |
| 67 | + console.log(`Generated Galaxy icons TypeScript exports at: ${outputPath}`); |
| 68 | + console.log(`Exported ${icons.length} icons: ${icons.map((i) => i.iconName).join(", ")}`); |
| 69 | +} |
| 70 | + |
| 71 | +// Run if called directly |
| 72 | +if (require.main === module) { |
| 73 | + generateIconsTypeScript(); |
| 74 | +} |
| 75 | + |
| 76 | +module.exports = generateIconsTypeScript; |
0 commit comments