-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathbuild.js
More file actions
119 lines (99 loc) · 2.88 KB
/
Copy pathbuild.js
File metadata and controls
119 lines (99 loc) · 2.88 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
/* 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((i) => {
const location = path.join(rootDir, 'src/icons', `${i}.js`);
const ComponentName = (i === 'github') ? 'GitHub' : upperCamelCase(i);
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 = 1em, ...rest }, ref) => {
return (
<svg ref={ref} ${attrsToString(defaultAttrs)}>
${featherIcons[i]}
</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/${i}';\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',
);
});