2020-10-06 20:23:26 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import prettier from 'prettier';
|
2020-12-02 13:48:39 +01:00
|
|
|
import { toPascalCase } from '../helpers';
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2021-03-23 19:26:50 +01:00
|
|
|
export default function(iconNodes, outputDirectory, template, { showLog = true }) {
|
|
|
|
|
const icons = Object.keys(iconNodes);
|
2020-10-06 20:23:26 +02:00
|
|
|
const iconsDistDirectory = path.join(outputDirectory, `icons`);
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(iconsDistDirectory)) {
|
|
|
|
|
fs.mkdirSync(iconsDistDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 19:26:50 +01:00
|
|
|
icons.forEach(iconName => {
|
|
|
|
|
const location = path.join(iconsDistDirectory, `${iconName}.js`);
|
|
|
|
|
const componentName = toPascalCase(iconName);
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2021-03-23 19:26:50 +01:00
|
|
|
let { children } = iconNodes[iconName];
|
|
|
|
|
children = children.map(({name, attributes}) => ([name, attributes]))
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2021-03-23 19:26:50 +01:00
|
|
|
const elementTemplate = template({ componentName, iconName, children });
|
2020-10-06 20:23:26 +02:00
|
|
|
|
|
|
|
|
|
2021-03-23 19:26:50 +01:00
|
|
|
fs.writeFileSync(location, prettier.format(elementTemplate, { singleQuote: true, trailingComma: 'all', parser: 'babel' }), 'utf-8');
|
|
|
|
|
|
|
|
|
|
if(showLog) {
|
|
|
|
|
console.log('Successfully built', componentName);
|
|
|
|
|
}
|
2020-10-06 20:23:26 +02:00
|
|
|
});
|
|
|
|
|
}
|