Files
lucide/scripts/build/generateIconFiles.js
Eric Fennis f964dff64d Lucide Svelte Package! (#476)
* init svelte project

* Add export script for lucide-svelte

* make lucide-svelte wokring

* make ready for first release

* update lock file

* bump version

* some fixes in the build

* Add tests

* Finish tests

* Update Readme

* update lock file

* Add svelte to gh actions

* Add svetle to website docs

* Add test ci script

* adjust action

* add on PR trigger

* remove dep

* fix tests

* Add svelte entry in package.json

* update snapshots
2022-02-17 17:46:55 +01:00

46 lines
1.4 KiB
JavaScript

/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import { promises } from 'stream';
import { toPascalCase } from '../helpers';
export default function({ 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}`)
})
}