2022-09-20 19:15:14 +02:00
|
|
|
/**
|
|
|
|
* Generate icon names
|
|
|
|
*
|
|
|
|
* Reads all the icon svg files in app/images/icons
|
|
|
|
* and returns an object of icon name key value pairs
|
|
|
|
* stored in the environment variable ICON_NAMES
|
2022-11-16 23:15:08 +01:00
|
|
|
* Used with the Icon component in ./ui/components/component-library/icon/icon.js
|
2022-09-20 19:15:14 +02:00
|
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const SVG_ICONS_FOLDER = './app/images/icons';
|
|
|
|
const ASSET_EXT = '.svg';
|
|
|
|
|
2023-01-24 18:39:46 +01:00
|
|
|
const getIconNameKebabCase = (fileName) => path.basename(fileName, ASSET_EXT);
|
2022-09-20 19:15:14 +02:00
|
|
|
|
|
|
|
const getIconNameInSnakeCase = (fileName) =>
|
2023-01-24 18:39:46 +01:00
|
|
|
path.basename(fileName, ASSET_EXT).replace(/-/gu, '_').toUpperCase();
|
2022-09-20 19:15:14 +02:00
|
|
|
|
2022-11-18 17:58:38 +01:00
|
|
|
const generateIconNames = () => {
|
2022-09-20 19:15:14 +02:00
|
|
|
const iconNames = {};
|
|
|
|
|
|
|
|
const svgIconsFolderPath = path.join(__dirname, `../${SVG_ICONS_FOLDER}`);
|
|
|
|
|
2022-11-18 17:58:38 +01:00
|
|
|
const fileList = fs.readdirSync(svgIconsFolderPath);
|
2022-09-20 19:15:14 +02:00
|
|
|
|
|
|
|
const svgIconsFileList = fileList.filter(
|
|
|
|
(fileName) => path.extname(fileName) === ASSET_EXT,
|
|
|
|
);
|
|
|
|
|
|
|
|
svgIconsFileList.forEach(
|
|
|
|
(fileName) =>
|
|
|
|
(iconNames[getIconNameInSnakeCase(fileName)] =
|
|
|
|
getIconNameKebabCase(fileName)),
|
|
|
|
);
|
|
|
|
|
2022-11-18 17:58:38 +01:00
|
|
|
const iconNamesStringified = JSON.stringify(iconNames);
|
|
|
|
|
|
|
|
return iconNamesStringified;
|
2022-09-20 19:15:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { generateIconNames };
|