File: /var/www/design.system/src/icons/utils/generateIconsMap.ts
import fs from 'fs';
import path from 'path';
const sizes = [16, 20, 24];
const iconsDir = './src/icons';
const outputDir = './src/icons/iconsMap';
sizes.forEach((size) => {
const sizeDir = path.join(iconsDir, `${size}`);
const files = fs.readdirSync(sizeDir).filter((f) => f.endsWith('.svg') || f.endsWith('.tsx'));
const imports: string[] = [];
const exports: string[] = [];
files.forEach((file) => {
const nameWithoutExt = path.basename(file, path.extname(file));
const importName = toPascalCase(nameWithoutExt);
const importPath = `../${size}/${nameWithoutExt}.svg?react`;
imports.push(`import ${importName} from '${importPath}';`);
exports.push(` '${nameWithoutExt}': ${importName},`);
});
const content = `// AUTO-GENERATED FILE. DO NOT EDIT MANUALLY.
${imports.join('\n')}
export const icons${size} = {
${exports.join('\n')}
} as const;
`;
fs.writeFileSync(path.join(outputDir, `${size}.ts`), content, 'utf-8');
});
function toPascalCase(str: string) {
return str.replace(/(^\w|[-_]\w)/g, (match) => match.replace(/[-_]/, '').toUpperCase());
}