-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
239 lines (212 loc) · 7.31 KB
/
generator.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const fs = require('fs');
const path = require('path');
/**
* @type {Object} templates
* @property {string} Icon.tsx - The content of the Icon.tsx file.
* @property {string} index.tsx - The content of the index.tsx file.
*/
const templates = {
'Icon.tsx': `import {CSSProperties, DOMAttributes, FC, FunctionComponent, memo, SVGProps} from 'react';
import * as iconComponents from './icons';
import { IconType } from './types';
export interface IconProps {
className?: string;
name: IconType;
size?: number;
width?: number;
height?: number;
fill?: string;
style?: CSSProperties;
onClick?: DOMAttributes<SVGSVGElement>['onClick'];
}
export const iconTestId = 'icon';
const getIconName = (name: IconType) => \`Icon\${name}\`;
export const Icon: FC<IconProps> = memo(({ className, name, fill = 'currentColor', size, width, height, style, onClick, ...rest }) => {
const IconComponent =
(iconComponents[getIconName(name) as keyof typeof iconComponents] as FunctionComponent<SVGProps<SVGSVGElement>>) ||
null;
return (
IconComponent && (
<IconComponent
onClick={onClick}
data-testid={iconTestId}
fill={fill}
data-name={name}
className={className}
style={{ ...style, width: size ?? width, height: size ?? height }}
{...rest}
/>
)
);
});
`,
'index.tsx': `export * from './Icon';
export * from './types';
`,
};
/**
* Check all the svg files in the input directory and remove width, height and fill attributes.
* @param {string} inputDir - The directory where the svg iocns located.
*/
function cleanAllIcons(inputDir) {
console.log(`🧹 Cleaning SVG icons: ${inputDir}`);
fs.readdir(inputDir, (err, files) => {
files.forEach((file) => {
if (file.endsWith('.svg')) {
fs.readFile(`${inputDir}${file}`, 'utf8', (err, data) => {
if (err) {
return;
}
const result = data.replace(/(width|height|fill)="[^"]*"/g, '');
fs.writeFileSync(`${inputDir}${file}`, result, 'utf8');
});
}
});
if (err) {
console.log(`🚨 Failed to clean SVG icons: ${inputDir}`);
} else {
console.log(`🧹 SVG icons cleaned: ${inputDir}`);
}
});
}
/**
* Creates files from templates if they do not already exist in the specified output directory.
* @param {string} outputDir - The output directory where the files will be created.
*/
function createFilesFromTemplatesIfNotExist(outputDir) {
const files = Object.keys(templates);
files.forEach((file) => {
if (!fs.existsSync(`${outputDir}${file}`)) {
fs.writeFileSync(`${outputDir}${file}`, templates[file]);
console.log(`✅ Created file: ${file}`);
}
});
}
/**
* Converts a text to PascalCase.
* @param {string} text - The text to be converted.
* @returns {string} The converted text in PascalCase.
*/
function toPascalCase(text) {
return text
.replace(/[-\s]+/g, ' ')
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
}
/**
* Creates folders if they do not exist.
* @param {string} inputDir - The input directory path.
* @param {string} outputDir - The output directory path.
*/
function createFoldersIfNotExist(inputDir, outputDir) {
const folders = [inputDir, outputDir];
folders.forEach((folder) => {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true });
console.log(`✅ Created folder: ${folder}`);
}
});
}
/**
* Calculates the export path for a given input and output path.
* @param {string} inputDir - The input path.
* @param {string} outputDir - The output path.
* @returns {string} - The export path.
*/
function getExportPath(inputDir, outputDir) {
const relativePath = path.relative(outputDir, inputDir);
const exportPath = relativePath.startsWith('.')
? relativePath
: `./${relativePath}`;
return exportPath.endsWith('/') ? exportPath : `${exportPath}/`;
}
/**
* Filters an array of files and returns only the files that have a '.svg' extension.
* @param {string[]} files - The array of file names to filter.
* @returns {string[]} - The filtered array of file names.
*/
function getFilteredFiles(files) {
return files.filter((file) => file.endsWith('.svg'));
}
/**
* Generates a typescript file containing the icon types based on the input directory.
* @param {string} inputDir - The input directory path.
* @param {string} outputDir - The output directory path.
* @param {string} [filename='types.ts'] - The name of the generated file.
*/
function createIconTypesFile(inputDir, outputDir, filename = 'types.ts') {
fs.readdir(inputDir, (err, files) => {
try {
const endExport = `
export type IconType = typeof iconNames[number];
`;
const filteredFiles = getFilteredFiles(files);
const iconTypes = filteredFiles.length
? filteredFiles.reduce((prev, cur, idx) => {
return `${prev}
'${cur.replace('.svg', '')}'${
filteredFiles.length === idx + 1
? `,
] as const;${endExport}`
: ','
}`;
}, 'export const iconNames = [')
: `export const iconNames = [] as const;${endExport}`;
fs.writeFileSync(`${outputDir}/${filename}`, iconTypes);
console.log(`🧩 ${filename} file generated`);
} catch {
console.log(`🚨 ${filename} generation failure`);
}
});
}
/**
* Generates an icons file based on the input directory containing SVG files.
* The generated file exports React components for each SVG file.
*
* @param {string} inputDir - The input directory path.
* @param {string} outputDir - The output directory path.
* @param {string} [filename='icons.ts'] - The name of the generated file.
*/
function createIconsFile(inputDir, outputDir, filename = 'icons.ts') {
fs.readdir(inputDir, (err, files) => {
try {
const filteredFiles = getFilteredFiles(files);
const icons = filteredFiles.length
? filteredFiles.reduce((prev, cur) => {
return `${prev}export { ReactComponent as Icon${toPascalCase(
cur.replace('.svg', ''),
)} } from '${getExportPath(inputDir, outputDir)}${cur}';
`;
}, '')
: `export {};
`;
fs.writeFileSync(`${outputDir}/${filename}`, icons);
console.log(`💚 ${filename} file generated`);
} catch {
console.log(`🚨 ${filename} generation failure`);
}
});
}
/**
* Generates SVG icons and related files based on the input directory.
*
* @param {string} inputDir - The input directory containing SVG files.
* @param {string} outputDir - The output directory where the generated files will be saved.
* @param {boolean} cleanSvg - A flag to remove unnecessary attributes from SVG files.
*/
function generate(inputDir, outputDir, cleanSvg) {
// Step 1: Create folders if they do not exist
createFoldersIfNotExist(inputDir, outputDir);
// Step 2: Create files from templates if they do not already exist
createFilesFromTemplatesIfNotExist(outputDir);
// Step 3: Generate a typescript file containing the icon types based on the input directory
createIconTypesFile(inputDir, outputDir);
// Step 4: Generate an icons file based on the input directory containing SVG files
createIconsFile(inputDir, outputDir);
// Step 5: Clean all the svg files in the input directory
if (cleanSvg) {
cleanAllIcons(inputDir);
}
}
module.exports = generate;