-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-icons.js
100 lines (78 loc) · 3.14 KB
/
generate-icons.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const fs = require('fs/promises');
const STRING_CAMELIZE_REGEXP = /(-|_|\.|\s)+(.)?/g;
const iconsSvgFolders = [
'node_modules/feather-icons/dist/icons',
'../node_modules/feather-icons/dist/icons',
'custom-icons',
];
const prefixPath = 'projects/step-core/src/lib/modules/step-icons';
const iconsDestFolder = `${prefixPath}/icons/svg`;
const indexFile = `${prefixPath}/icons/index.ts`;
const allFile = `${prefixPath}/icons/all.ts`;
const templateFile = `${prefixPath}/templates/icon-template.ts.tpl`;
const camelize = (str) =>
str
.replace(STRING_CAMELIZE_REGEXP, (_match, _separator, chr) => {
return chr ? chr.toUpperCase() : '';
})
.replace(/^([A-Z])/, (match) => match.toLowerCase());
const upperCamelize = (str) => {
const camelCase = camelize(str);
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
};
const isFileExisting = async (path) => {
let isExisting;
try {
await fs.access(path);
isExisting = true;
} catch {
isExisting = false;
}
return { path, isExisting };
};
const getExistingFolders = async () => {
const folders = await Promise.all(iconsSvgFolders.map((path) => isFileExisting(path)));
return folders.filter((f) => f.isExisting).map((f) => f.path);
};
const removeExtension = (name) => name.substr(0, name.lastIndexOf('.'));
const run = async () => {
let exportAllString = `\nexport const allIcons = {\n`;
const iconTemplate = await fs.readFile(templateFile, { encoding: 'utf-8' });
await fs.rm(iconsDestFolder, { force: true, recursive: true });
await fs.rm(indexFile, { force: true });
await fs.rm(allFile, { force: true });
await fs.mkdir(iconsDestFolder);
const proceedFile = async (folder, file) => {
const iconName = removeExtension(file);
const exportName = upperCamelize(iconName);
const markup = await fs.readFile(`${folder}/${file}`, { encoding: 'utf-8' });
const payload = String(markup)
.trim()
.match(/^<svg[^>]+?>(.+)<\/svg>$/);
if (!payload) {
console.log('FAILED TO EXTRACT SVG FROM:', `${folder}/${file}`);
console.log('MARKUP', markup.trim());
console.log('---------------------------------');
return;
}
const output = iconTemplate
.replace(/__EXPORT_NAME__/g, exportName)
.replace(/__ICON_NAME__/g, iconName)
.replace(/__PAYLOAD__/, payload[1]);
await fs.writeFile(`${iconsDestFolder}/${iconName}.ts`, output, { encoding: 'utf-8' });
await fs.appendFile(indexFile, `export { ${exportName} } from './svg/${iconName}';\n`);
await fs.appendFile(allFile, `import { ${exportName} } from './svg/${iconName}';\n`);
exportAllString += ` ${exportName}, \n`;
};
const proceedFolder = async (folder) => {
const files = await fs.readdir(folder);
const filePromises = files.map((file) => proceedFile(folder, file));
return Promise.all(filePromises);
};
const folders = await getExistingFolders();
await Promise.all(folders.map((f) => proceedFolder(f)));
exportAllString += `};\n`;
await fs.appendFile(allFile, exportAllString);
await fs.appendFile(indexFile, `\nexport { allIcons } from './all';\n`);
};
return run().catch((err) => console.error(err));