-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathbuild.js
More file actions
133 lines (110 loc) · 3.15 KB
/
build.js
File metadata and controls
133 lines (110 loc) · 3.15 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable prefer-template */
const path = require('path');
const fs = require('fs');
const format = require('prettier-eslint');
const upperCamelCase = require('uppercamelcase');
const featherIcons = require('feather-icons/dist/icons.json');
const rootDir = path.join(__dirname, '..');
const icons = Object.keys(featherIcons);
const dir = path.join(rootDir, 'src/icons');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const initialTypeDefinitions = `/// <reference types="react" />
import { FC, SVGAttributes } from 'react';
export interface IconProps extends SVGAttributes<SVGElement> {
color?: string;
size?: string | number;
}
export type Icon = FC<IconProps>;
`;
fs.writeFileSync(path.join(rootDir, 'src', 'index.js'), '', 'utf-8');
fs.writeFileSync(
path.join(rootDir, 'src', 'index.d.ts'),
initialTypeDefinitions,
'utf-8'
);
const attrsToString = attrs => {
return Object.keys(attrs)
.map(key => {
if (key === 'width' || key === 'height' || key === 'stroke') {
return key + '={' + attrs[key] + '}';
}
if (key === 'rest') {
return '{...rest}';
}
return key + '="' + attrs[key] + '"';
})
.join(' ');
};
icons.forEach(icon => {
const location = path.join(rootDir, 'src/icons', `${icon}.js`);
const ComponentName = icon === 'github' ? 'GitHub' : upperCamelCase(icon);
const defaultAttrs = {
xmlns: 'http://www.w3.org/2000/svg',
width: 'size',
height: 'size',
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'color',
strokeWidth: 2,
strokeLinecap: 'round',
strokeLinejoin: 'round',
rest: '...rest',
};
const element = `
import React, {forwardRef} from 'react';
import PropTypes from 'prop-types';
const ${ComponentName} = forwardRef(({ color = 'currentColor', size = 24, ...rest }, ref) => {
return (
<svg ref={ref} ${attrsToString(defaultAttrs)}>
${featherIcons[icon]}
</svg>
)
});
${ComponentName}.propTypes = {
color: PropTypes.string,
size: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
}
${ComponentName}.displayName = '${ComponentName}'
export default ${ComponentName}
`;
const component = format({
text: element,
eslintConfig: {
extends: 'airbnb',
},
prettierOptions: {
bracketSpacing: true,
singleQuote: true,
parser: 'flow',
},
});
fs.writeFileSync(location, component, 'utf-8');
console.log('Successfully built', ComponentName);
const exportString = `export { default as ${ComponentName} } from './icons/${icon}';\r\n`;
fs.appendFileSync(
path.join(rootDir, 'src', 'index.js'),
exportString,
'utf-8'
);
const exportTypeString = `export const ${ComponentName}: Icon;\n`;
fs.appendFileSync(
path.join(rootDir, 'src', 'index.d.ts'),
exportTypeString,
'utf-8'
);
const iconTypeDefinition = `import { Icon as IconType } from '../';
declare const Icon: IconType
export default Icon;
`;
fs.writeFileSync(
path.join(rootDir, 'src/icons', `${icon}.d.ts`),
iconTypeDefinition,
'utf-8'
);
});