forked from michaelbazos/angular-feather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
66 lines (50 loc) · 1.78 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
const del = require('del');
const fs = require('fs-extra');
const uppercamelcase = require('uppercamelcase');
const iconsSrcFolder = 'node_modules/feather-icons/dist/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 = stripExtension(filename);
const exportName = uppercamelcase(iconName);
const markup = fs.readFileSync(`${iconsSrcFolder}/${filename}`);
const payload = String(markup).match(/^<svg[^>]+?>(.+)<\/svg>$/);
let output = componentTemplate
.replace(/__EXPORT_NAME__/g, exportName)
.replace(/__ICON_NAME__/g, iconName)
.replace(/__PAYLOAD__/, payload[1]);
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('.'));
}