create script which generates json file with icon data

This commit is contained in:
Jan Kollars 2021-08-27 12:50:14 +02:00
parent dc81e4561f
commit 4e28aa0981
3 changed files with 41 additions and 2 deletions

1
dist/icons.json vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -4,8 +4,9 @@
"description": "High Quality Web Icons", "description": "High Quality Web Icons",
"scripts": { "scripts": {
"start": "webpack-dev-server --history-api-fallback --hot --open", "start": "webpack-dev-server --history-api-fallback --hot --open",
"build": "webpack -p ", "build": "npm run generate-icon-data && webpack -p ",
"optimize-svg": "svgo --config=svgo.config.js -f svg/regular && svgo --config=svgo.config.js -f svg/logos && svgo --config=svgo.config.js -f svg/solid" "optimize-svg": "svgo --config=svgo.config.js -f svg/regular && svgo --config=svgo.config.js -f svg/logos && svgo --config=svgo.config.js -f svg/solid",
"generate-icon-data": "node write-icon-data.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

37
write-icon-data.js Normal file
View file

@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
/**
* @typedef {Object} IconData
* @property {string[]} type
*/
/** @type {Object.<string, IconData>} */
const icons = {};
const iconRootDirectory = path.join(__dirname, 'svg');
const outputFile = path.join(__dirname, 'dist', 'icons.json');
/** @type {string[]} */
const categories = fs
.readdirSync(iconRootDirectory, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
categories.forEach((category) => {
const foundSVGs = fs
.readdirSync(path.join(iconRootDirectory, category))
.filter(fileName => fileName.endsWith('.svg'));
foundSVGs.forEach((fileName) => {
const svgKey = fileName.slice(fileName.indexOf('-') + 1, -4);
if (Object.hasOwnProperty.call(icons, svgKey)) {
icons[svgKey].types.push(category);
} else {
icons[svgKey] = {
types: [category],
};
}
});
});
fs.writeFileSync(outputFile, JSON.stringify(icons));