-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
70 lines (54 loc) · 1.97 KB
/
generate.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const del = require("del");
const fs = require("fs-extra");
const uppercamelcase = require("uppercamelcase");
const iconsSrcFolder = "node_modules/bootstrap-icons/icons";
const iconsDestFolder = "icons/svg";
const indexFile = "icons/index.ts";
const allFile = "icons/all.ts";
let exportAllString = `\nexport const allIcons = {\n`;
const componentTemplate = fs.readFileSync(
"src/templates/component.ts.tpl",
"utf-8"
);
return (
Promise.resolve()
// delete feather folder and index
.then(() => del([iconsDestFolder, indexFile, allFile]))
// create destination folder
.then(() => fs.mkdirSync(iconsDestFolder))
.then(() => {
fs.readdirSync(`${iconsSrcFolder}`).forEach((filename) => {
"use strict";
const iconName = stringify(stripExtension(filename));
const exportName = stringify(uppercamelcase(iconName));
const markup = fs.readFileSync(`${iconsSrcFolder}/${filename}`);
let payload = String(markup)
.replace(/width="[0-9]+"/, 'width="100%"')
.replace(/height="[0-9]+"/, 'height="100%"')
.match(/^<svg[^>]+?>[^]*<\/svg>$/);
let output = componentTemplate
.replace(/__EXPORT_NAME__/g, exportName)
.replace(/__PAYLOAD__/, payload);
fs.writeFileSync(`${iconsDestFolder}/${iconName}.ts`, output, "utf-8");
fs.appendFileSync(
indexFile,
`export { ${exportName} } from './svg/${iconName}';\n`
);
fs.appendFileSync(
allFile,
`import { ${exportName} } from './svg/${iconName}';\n`
);
exportAllString += ` ${exportName},\n`;
});
exportAllString += `};\n`;
fs.appendFileSync(allFile, exportAllString);
fs.appendFileSync(indexFile, `\nexport { allIcons } from './all';\n`);
})
.catch((err) => console.log(err))
);
function stripExtension(str) {
return str.substr(0, str.lastIndexOf("."));
}
function stringify(str) {
return Number(str) ? `_${str}` : str;
}