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';
|
2021-09-17 15:51:18 +02:00
|
|
|
import { promises } from 'stream';
|
2020-12-02 13:48:39 +01:00
|
|
|
import { toPascalCase } from '../helpers';
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2022-02-17 17:46:55 +01:00
|
|
|
export default function({ iconNodes, outputDirectory, template, showLog = true, iconFileExtention = '.js', pretty = true }) {
|
2021-03-23 19:26:50 +01:00
|
|
|
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-09-17 15:51:18 +02:00
|
|
|
const writeIconFiles = icons.map(async iconName => {
|
2021-04-11 19:04:24 +02:00
|
|
|
const location = path.join(iconsDistDirectory, `${iconName}${iconFileExtention}`);
|
2021-03-23 19:26:50 +01:00
|
|
|
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 });
|
2022-02-17 17:46:55 +01:00
|
|
|
const output =
|
|
|
|
|
pretty
|
|
|
|
|
? prettier.format(elementTemplate, {
|
|
|
|
|
singleQuote: true,
|
|
|
|
|
trailingComma: 'all',
|
|
|
|
|
parser: 'babel',
|
|
|
|
|
})
|
|
|
|
|
: elementTemplate
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2022-02-17 17:46:55 +01:00
|
|
|
await fs.promises.writeFile(location, output, 'utf-8');
|
2021-09-17 15:51:18 +02:00
|
|
|
});
|
2020-10-06 20:23:26 +02:00
|
|
|
|
2021-09-17 15:51:18 +02:00
|
|
|
Promise.all(writeIconFiles)
|
2022-02-17 17:46:55 +01:00
|
|
|
.then(() => {
|
|
|
|
|
if(showLog) {
|
|
|
|
|
console.log('Successfully built', icons.length, 'icons.');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
throw new Error(`Something went wrong generating icon files,\n ${error}`)
|
|
|
|
|
})
|
2020-10-06 20:23:26 +02:00
|
|
|
}
|