2022-08-23 08:47:14 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
|
2023-10-13 07:11:41 -05:00
|
|
|
const cachedListPath = path.join(__dirname, '../public/app/core/icons/cached.json');
|
2022-08-23 08:47:14 -05:00
|
|
|
const iconsList = require(cachedListPath);
|
|
|
|
|
2023-10-13 07:11:41 -05:00
|
|
|
const iconsBundleJsTemplatePath = path.join(__dirname, '../public/app/core/icons/iconBundle.ts.template');
|
|
|
|
const iconsBundleJsPath = path.join(__dirname, '../public/app/core/icons/iconBundle.ts');
|
2022-08-23 08:47:14 -05:00
|
|
|
|
|
|
|
const iconsBundleJsTemplate = fs.readFileSync(iconsBundleJsTemplatePath).toString();
|
|
|
|
|
|
|
|
const importsStatements = [];
|
|
|
|
const cacheStatements = [];
|
|
|
|
|
2023-10-13 07:11:41 -05:00
|
|
|
const grafanaIconsPublicPath = '../../../img/icons/';
|
2022-08-23 08:47:14 -05:00
|
|
|
|
|
|
|
function generateIconBundle({ outputPath, verbose = false }) {
|
|
|
|
const modulePrefix = 'u';
|
|
|
|
let moduleNameCount = 1000;
|
|
|
|
|
|
|
|
for (iconEntry of iconsList) {
|
|
|
|
// skip empty and commented
|
|
|
|
if (iconEntry === '' || iconEntry.startsWith('#')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
importsStatements.push(
|
|
|
|
`import ${modulePrefix}${moduleNameCount} from '${grafanaIconsPublicPath}${iconEntry}.svg';`
|
|
|
|
);
|
2023-10-13 07:11:41 -05:00
|
|
|
cacheStatements.push(` cacheItem(${modulePrefix}${moduleNameCount}, resolvePath('${iconEntry}.svg'));`);
|
2022-08-23 08:47:14 -05:00
|
|
|
moduleNameCount++;
|
|
|
|
}
|
|
|
|
const output = iconsBundleJsTemplate
|
|
|
|
.replace('//{{imports}}', importsStatements.join('\n'))
|
|
|
|
.replace('//{{cacheItems}}', cacheStatements.join('\n'));
|
|
|
|
|
|
|
|
fs.writeFileSync(outputPath, output);
|
|
|
|
if (verbose) {
|
|
|
|
console.log('The iconsBundle file was successfully written.');
|
|
|
|
console.log(`The file is located at ${outputPath}`);
|
|
|
|
}
|
|
|
|
return outputPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if invoked directly
|
|
|
|
if (require.main === module) {
|
|
|
|
generateIconBundle({ outputPath: iconsBundleJsPath, verbose: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = generateIconBundle;
|