-
Notifications
You must be signed in to change notification settings - Fork 578
/
index.js
82 lines (72 loc) · 2.27 KB
/
index.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
const build = require('buildfont/src/executing');
const path = require('path');
const fs = require('fs-extra');
const prettier = require('prettier');
// TODO: twotone
const fonts = ['fill', 'outline'];
const upperName = name => name.charAt(0).toUpperCase() + name.slice(1);
// TODO: move to ../icons/build
fs.ensureDirSync('fonts');
fs.ensureDirSync('iconfont');
fonts.forEach(name => {
const fontName = `ant${name}`;
const uppercaseName = upperName(name);
const svgFolder = path.resolve(process.cwd(), '../icons/svg/', name);
const dist = `iconfont/${name}`;
build(svgFolder, fontName, dist).then(() => {
const json = fs.readFileSync(path.join(dist, `${fontName}.json`), 'utf-8');
fs.copyFileSync(
path.join(dist, `${fontName}.ttf`), `fonts/${fontName}.ttf`);
// TODO: template
const content = `
// Note: this file was generated by 'npm run generate' do not modify it manually
// tslint:disable
import * as React from 'react';
import { Text, TextProps, TextStyle } from 'react-native';
export const ${name}GlyphMap = ${json};
export type ${uppercaseName}GlyphMapType = keyof typeof ${name}GlyphMap;
export interface Icon${uppercaseName}Props extends TextProps {
name: ${uppercaseName}GlyphMapType;
size?: number;
color?: string
}
export default class Icon${uppercaseName} extends React.PureComponent<Icon${
uppercaseName}Props> {
render() {
const {
name,
style,
children,
size = 14,
color = "black",
...props
} = this.props;
const styleOverrides: TextStyle = {
fontFamily: "ant${name}",
fontWeight: "normal",
fontStyle: "normal",
fontSize: size,
color
};
let glyph = name ? ${name}GlyphMap[name] || "?" : "";
if (typeof glyph === "number") {
glyph = String.fromCharCode(glyph);
}
return (
<Text {...props} style={[styleOverrides, style]}>
{glyph}
{children}
</Text>
);
}
}
`;
fs.writeFileSync(
`src/${name}.tsx`, prettier.format(content, {parser: 'typescript'}));
});
});
// index.tsx
const contents = fonts.map(font => {
return `export { default as Icon${upperName(font)}, Icon${upperName(font)}Props, ${upperName(font)}GlyphMapType } from './${font}';\n`;
});
fs.writeFileSync('src/index.tsx', contents.join(''))