Files
lucide/scripts/building/generateIconFiles.mjs
Louis Bailleau 55ae908018 Update all packages dependencies in project root (#863)
* Update `svgo` and `svgson` version and fix some tests

* Update eslint-related packages and fix all linter errors

* Update all rollup-related packages version

* Update all rollup-related packages (part 2)

* Update the rest of package which need to be updated

* Fix unwanted comment

* Fix unwanted comment (again)
2022-11-07 22:29:19 +01:00

50 lines
1.3 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import { toPascalCase } from '../helpers.mjs';
export default ({
iconNodes,
outputDirectory,
template,
showLog = true,
iconFileExtention = '.js',
pretty = true,
}) => {
const icons = Object.keys(iconNodes);
const iconsDistDirectory = path.join(outputDirectory, `icons`);
if (!fs.existsSync(iconsDistDirectory)) {
fs.mkdirSync(iconsDistDirectory);
}
const writeIconFiles = icons.map(async (iconName) => {
const location = path.join(iconsDistDirectory, `${iconName}${iconFileExtention}`);
const componentName = toPascalCase(iconName);
let { children } = iconNodes[iconName];
children = children.map(({ name, attributes }) => [name, attributes]);
const elementTemplate = template({ componentName, iconName, children });
const output = pretty
? prettier.format(elementTemplate, {
singleQuote: true,
trailingComma: 'all',
parser: 'babel',
})
: elementTemplate;
await fs.promises.writeFile(location, output, 'utf-8');
});
Promise.all(writeIconFiles)
.then(() => {
if (showLog) {
console.log('Successfully built', icons.length, 'icons.');
}
})
.catch((error) => {
throw new Error(`Something went wrong generating icon files,\n ${error}`);
});
};