forked from 6un9-h0-Dan/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-icon-bundle.js
51 lines (40 loc) · 1.65 KB
/
generate-icon-bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require('fs');
const os = require('os');
const path = require('path');
const cachedListPath = path.join(__dirname, '../public/app/core/icons/cached.json');
const iconsList = require(cachedListPath);
const iconsBundleJsTemplatePath = path.join(__dirname, '../public/app/core/icons/iconBundle.ts.template');
const iconsBundleJsPath = path.join(__dirname, '../public/app/core/icons/iconBundle.ts');
const iconsBundleJsTemplate = fs.readFileSync(iconsBundleJsTemplatePath).toString();
const importsStatements = [];
const cacheStatements = [];
const grafanaIconsPublicPath = '../../../img/icons/';
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';`
);
cacheStatements.push(` cacheItem(${modulePrefix}${moduleNameCount}, resolvePath('${iconEntry}.svg'));`);
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;