-
Notifications
You must be signed in to change notification settings - Fork 26
/
icon.js
58 lines (50 loc) · 1.36 KB
/
icon.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
const path = require('path');
const globby = require('globby');
const {
withThemeSuffix,
removeTypeTheme,
alias,
} = require('./compatible-icon-utils');
const v4IconModulePath = path.dirname(
require.resolve('@ant-design/icons/lib/icons'),
);
let allV4Icons = [];
function getAllV4IconNames() {
if (allV4Icons.length) {
return allV4Icons;
}
// read allIcons by fs
const iconPaths = globby.sync([`${v4IconModulePath}/*.js`]);
allV4Icons = iconPaths.map(iconPath => path.basename(iconPath, '.js'));
return allV4Icons;
}
function getV4IconComponentName(type, theme) {
const v4IconComponentName = withThemeSuffix(
removeTypeTheme(alias(type)),
theme || 'outlined',
);
const v4Icons = getAllV4IconNames();
// check if component is valid or not in v4 icons
if (v4Icons.includes(v4IconComponentName)) {
return v4IconComponentName;
}
console.warn(
`The icon name '${type}'${
theme ? ` with ${theme}` : ''
} cannot found, please check it at https://ant.design/components/icon`,
);
return '';
}
function createIconJSXElement(j, iconLocalName, attrs = []) {
const openingElement = j.jsxOpeningElement(
j.jsxIdentifier(iconLocalName),
attrs,
);
openingElement.selfClosing = true;
return j.jsxElement(openingElement);
}
module.exports = {
getAllV4IconNames,
getV4IconComponentName,
createIconJSXElement,
};