| 1 |
import { join } from 'path'; |
| 2 |
import { outputFileSync, ensureDirSync } from 'fs-extra/esm'; |
| 3 |
import { generateIcons } from './generateIcons.mjs'; |
| 4 |
import { createElement } from 'react'; |
| 5 |
import { renderToString } from 'react-dom/server'; |
| 6 |
import { pfToRhIcons } from './icons/pfToRhIcons.mjs'; |
| 7 |
import * as url from 'url'; |
| 8 |
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
| 9 |
|
| 10 |
// Import createIconBase from compiled dist (build:esm must run first) |
| 11 |
const createIconModule = await import('../dist/esm/createIcon.js'); |
| 12 |
const createIconBase = createIconModule.createIconBase; |
| 13 |
|
| 14 |
const outDir = join(__dirname, '../dist'); |
| 15 |
const staticDir = join(outDir, 'static'); |
| 16 |
|
| 17 |
const removeSnake = (s) => s.toUpperCase().replace('-', '').replace('_', ''); |
| 18 |
const toCamel = (s) => `${s[0].toUpperCase()}${s.substr(1).replace(/([-_][\w])/gi, removeSnake)}`; |
| 19 |
|
| 20 |
const writeCJSExport = (fname, jsName, icon, rhUiIcon = null) => { |
| 21 |
outputFileSync( |
| 22 |
join(outDir, 'js/icons', `${fname}.js`), |
| 23 |
`"use strict" |
| 24 |
exports.__esModule = true; |
| 25 |
exports.${jsName}Config = { |
| 26 |
name: '${jsName}', |
| 27 |
icon: ${JSON.stringify(icon)}, |
| 28 |
rhUiIcon: ${rhUiIcon ? JSON.stringify(rhUiIcon) : 'null'}, |
| 29 |
}; |
| 30 |
exports.${jsName} = require('../createIcon').createIconBase(exports.${jsName}Config); |
| 31 |
exports["default"] = exports.${jsName}; |
| 32 |
`.trim() |
| 33 |
); |
| 34 |
}; |
| 35 |
|
| 36 |
const writeESMExport = (fname, jsName, icon, rhUiIcon = null) => { |
| 37 |
outputFileSync( |
| 38 |
join(outDir, 'esm/icons', `${fname}.js`), |
| 39 |
`import { createIconBase } from '../createIcon.js'; |
| 40 |
|
| 41 |
export const ${jsName}Config = { |
| 42 |
name: '${jsName}', |
| 43 |
icon: ${JSON.stringify(icon)}, |
| 44 |
rhUiIcon: ${rhUiIcon ? JSON.stringify(rhUiIcon) : 'null'}, |
| 45 |
}; |
| 46 |
|
| 47 |
export const ${jsName} = createIconBase(${jsName}Config); |
| 48 |
|
| 49 |
export default ${jsName}; |
| 50 |
`.trim() |
| 51 |
); |
| 52 |
}; |
| 53 |
|
| 54 |
const writeDTSExport = (fname, jsName, icon, rhUiIcon = null) => { |
| 55 |
const text = `import { ComponentClass } from 'react'; |
| 56 |
import { SVGIconProps } from '../createIcon.js'; |
| 57 |
export declare const ${jsName}Config: { |
| 58 |
name: '${jsName}', |
| 59 |
icon: ${JSON.stringify(icon)}, |
| 60 |
rhUiIcon: ${rhUiIcon ? JSON.stringify(rhUiIcon) : 'null'}, |
| 61 |
}; |
| 62 |
export declare const ${jsName}: ComponentClass<SVGIconProps>; |
| 63 |
export default ${jsName}; |
| 64 |
`.trim(); |
| 65 |
const filename = `${fname}.d.ts`; |
| 66 |
outputFileSync(join(outDir, 'js/icons', filename), text); |
| 67 |
outputFileSync(join(outDir, 'esm/icons', filename), text); |
| 68 |
}; |
| 69 |
|
| 70 |
/** |
| 71 |
* Generates a static SVG string from icon data using createIconBase |
| 72 |
* @param {string} iconName The name of the icon |
| 73 |
* @param {object} icon The icon data object |
| 74 |
* @returns {string} Static SVG markup |
| 75 |
*/ |
| 76 |
function generateStaticSVG(iconName, icon) { |
| 77 |
const jsName = `${toCamel(iconName)}Icon`; |
| 78 |
|
| 79 |
// Create icon component using createIconBase |
| 80 |
const IconComponent = createIconBase({ |
| 81 |
name: jsName, |
| 82 |
icon |
| 83 |
}); |
| 84 |
|
| 85 |
// Render the component to string |
| 86 |
const svgString = renderToString(createElement(IconComponent)); |
| 87 |
|
| 88 |
// Convert React's className to class for static SVG |
| 89 |
return svgString.replace(/className=/g, 'class='); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Writes static SVG files to dist/static directory |
| 94 |
* @param {object} icons icons from generateIcons |
| 95 |
*/ |
| 96 |
function writeStaticSVGs(icons) { |
| 97 |
ensureDirSync(staticDir); |
| 98 |
|
| 99 |
Object.entries(icons).forEach(([iconName, icon]) => { |
| 100 |
const svgContent = generateStaticSVG(iconName, icon); |
| 101 |
const svgFileName = `${iconName}.svg`; |
| 102 |
outputFileSync(join(staticDir, svgFileName), svgContent, 'utf-8'); |
| 103 |
}); |
| 104 |
|
| 105 |
// eslint-disable-next-line no-console |
| 106 |
console.log(`Wrote ${Object.keys(icons).length} static SVG files to ${staticDir}`); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Writes CJS and ESM icons to `dist` directory |
| 111 |
* |
| 112 |
* @param {any} icons icons from generateIcons |
| 113 |
*/ |
| 114 |
function writeIcons(icons) { |
| 115 |
const index = []; |
| 116 |
Object.entries(icons).forEach(([iconName, icon]) => { |
| 117 |
const fname = `${iconName}-icon`; |
| 118 |
const jsName = `${toCamel(iconName)}Icon`; |
| 119 |
|
| 120 |
const altIcon = pfToRhIcons[jsName] ? pfToRhIcons[jsName].icon : null; |
| 121 |
writeESMExport(fname, jsName, icon, altIcon); |
| 122 |
writeCJSExport(fname, jsName, icon, altIcon); |
| 123 |
writeDTSExport(fname, jsName, icon, altIcon); |
| 124 |
|
| 125 |
index.push({ fname, jsName }); |
| 126 |
}); |
| 127 |
|
| 128 |
const esmIndexString = index |
| 129 |
.map(({ fname, jsName }) => `export { ${jsName}, ${jsName}Config } from './${fname}.js';`) |
| 130 |
.sort() |
| 131 |
.join('\n'); |
| 132 |
outputFileSync(join(outDir, 'esm', 'icons/index.js'), esmIndexString); |
| 133 |
outputFileSync(join(outDir, 'esm', 'icons/index.d.ts'), esmIndexString); |
| 134 |
outputFileSync(join(outDir, 'js', 'icons/index.d.ts'), esmIndexString); |
| 135 |
outputFileSync( |
| 136 |
join(outDir, 'js', 'icons/index.js'), |
| 137 |
`"use strict"; |
| 138 |
function __export(m) { |
| 139 |
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; |
| 140 |
} |
| 141 |
exports.__esModule = true; |
| 142 |
${index |
| 143 |
.map(({ fname }) => `__export(require('./${fname}'));`) |
| 144 |
.sort() |
| 145 |
.join('\n')} |
| 146 |
`.trim() |
| 147 |
); |
| 148 |
|
| 149 |
// eslint-disable-next-line no-console |
| 150 |
console.log('Wrote', index.length * 3 + 3, 'icon files.'); |
| 151 |
} |
| 152 |
|
| 153 |
const icons = generateIcons(); |
| 154 |
writeIcons(icons); |
| 155 |
writeStaticSVGs(icons); |