-
Notifications
You must be signed in to change notification settings - Fork 0
/
figma-importer.mjs
281 lines (253 loc) · 7.84 KB
/
figma-importer.mjs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import fs, { readdirSync, rmSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'url';
import { loadConfig, optimize } from 'svgo';
import * as cheerio from 'cheerio';
import cliProgress from 'cli-progress';
import color from 'ansi-colors';
import ora from 'ora';
import 'dotenv/config';
// Set __dirname to the directory name of the current module
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Load SVG optimization configuration
const svgConfig = await loadConfig();
// Get environment variables
const { TOKEN, FILE } = process.env;
/**
* Convert kebab-case to camelCase.
* @param {string} s - The string to convert.
* @returns {string} - The camelCase string.
*/
const camelize = (s) => s.replace(/-./g, (x) => x[1].toUpperCase());
const captalize = (s) => {
const string = camelize(s);
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Fetch the Figma file from the API.
* @param {string} key - The Figma file key.
* @returns {Promise<object>} - The Figma file data as JSON.
*/
async function fetchFigmaFile(key) {
const spinner = ora('Loading Figma file').start();
try {
const response = await fetch(`https://api.figma.com/v1/files/${key}`, {
headers: { 'X-Figma-Token': TOKEN },
});
spinner.succeed('File loaded');
return await response.json();
} catch (e) {
console.error(e);
spinner.fail('Failed to load file');
return {};
}
}
/**
* Flatten nested arrays.
* @param {Array} acc - The accumulator array.
* @param {Array} cur - The current array.
* @returns {Array} - The flattened array.
*/
const flatten = (acc, cur) => [...acc, ...cur];
/**
* Get components from Figma node.
* @param {object} node - The Figma node.
* @returns {Array} - Array of component nodes.
*/
const getComponentsFromNode = (node) => {
if (node.type === 'COMPONENT') {
return [node];
}
if ('children' in node) {
return node.children.map(getComponentsFromNode).reduce(flatten, []);
}
return [];
};
/**
* Format the component name.
* @param {string} name - The component name.
* @returns {string} - The formatted name.
*/
const formatName = (name) =>
name
.split('/')
.pop()
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/\//g, '_')
.trim();
/**
* Get SVGs from components.
* @param {string} key - The Figma file key.
* @returns {function(Array): Promise<Array>} - Function that returns a Promise of SVG components.
*/
const getSVGsFromComponents = (key) => async (components) => {
const progressBar = new cliProgress.SingleBar({
format: `Downloading icons | ${color.cyan(`{bar}`)} | {percentage}% || {value}/{total}`,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
});
const ids = components.map(({ id }) => id);
progressBar.start(ids.length, 0);
try {
const response = await fetch(
`https://api.figma.com/v1/images/${key}?ids=${ids.join()}&format=svg`,
{ headers: { 'X-Figma-Token': TOKEN } }
);
const { images } = await response.json();
const result = await Promise.all(
components.map(async ({ id, name }) => {
const response = await fetch(images[id]);
const svg = await response.text();
progressBar.increment();
return {
name: `Icon${captalize(camelize(formatName(name)))}`,
fileName: formatName(name),
svg: formatIconsSVG(svg)
};
})
);
progressBar.stop();
return result;
} catch (e) {
console.error(e);
progressBar.stop();
return [];
}
};
/**
* Optimize SVG data.
* @param {string} svg - The raw SVG data.
* @returns {string} - The optimized SVG data.
*/
const formatIconsSVG = (svg) => optimize(svg, svgConfig).data;
/**
* Generate the icon component template.
* @param {string} svgRaw - The raw SVG data.
* @param {string} componentName - The component name.
* @returns {string} - The icon component template as a string.
*/
const iconComponentTemplate = (svgRaw, componentName) => {
const svg = cheerio.load(svgRaw, { xmlMode: true });
svg('*').each((_, el) => {
Object.keys(el.attribs).forEach((attrKey) => {
if (attrKey.includes('-')) {
svg(el)
.attr(camelize(attrKey), el.attribs[attrKey])
.removeAttr(attrKey);
}
if (attrKey === 'class') {
svg(el).attr('className', el.attribs[attrKey]).removeAttr(attrKey);
}
if (attrKey === 'xmlns:xlink') {
svg(el).removeAttr(attrKey);
}
});
});
const content = svg("svg")
.attr("props", "...")
.toString()
.replace(
/stroke=['"](#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}|rgb\([0-9, ]+\)|rgba\([0-9, ]+\)|hsl\([0-9, %, ]+\))['"]/g,
"stroke='currentcolor'"
)
.replace(
/fill=['"](#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}|rgb\([0-9, ]+\)|rgba\([0-9, ]+\)|hsl\([0-9, %, ]+\))['"]/g,
"fill='currentcolor'"
)
.replace('props="..."', "{...props}");
return `import React from 'react';
export const ${componentName} = (props: React.SVGProps<SVGSVGElement>) => (${content});
`;
};
/**
* Organize the output directory by creating or cleaning the `src` folder.
* @returns {Promise<void>} - Promise representing the completion of directory organization.
*/
async function organizeDirectory() {
const spinner = ora('Organizing directory').start();
try {
const folderName = path.join(__dirname, 'src');
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
} else {
readdirSync(folderName).forEach((file) =>
rmSync(`${folderName}/${file}`)
);
}
} catch (err) {
console.error(err);
}
spinner.stop();
}
/**
* Generate the index.tsx file.
* @param {Array} icons - Array of icon objects.
* @returns {Promise<void>} - Promise representing the completion of index file generation.
*/
async function generateIndexFile(icons) {
const spinner = ora('Generating index.tsx').start();
const indexContent = Array.from(
new Set(icons.map(({ fileName }) => fileName))
)
.map((fileName) => icons.find((icon) => icon.fileName === fileName))
.map(({ name, filePath }) => `export {${name}} from '${filePath}';`)
.join('\n');
fs.writeFileSync(path.join(__dirname, 'src', 'index.ts'), indexContent, {
flag: 'w+'
});
spinner.succeed('Index file generated successfully!');
}
/**
* Main function to generate Figma icons.
* @returns {Promise<void>} - Promise representing the completion of icon generation.
*/
export async function generateFigmaIcons() {
if (!TOKEN) {
console.error(
'The Figma API token is not defined. You need to set an environment variable `FIGMA_API_TOKEN` to run the script'
);
return;
}
const data = await fetchFigmaFile(FILE);
const spinner = ora('Parsing the file').start();
const components = getComponentsFromNode(
data?.document?.children
.filter(({ name }) => name.includes('Icons'))
?.pop()
.children?.pop()
);
spinner.stop();
const icons = await getSVGsFromComponents(FILE)(components);
await organizeDirectory();
const progressBar = new cliProgress.SingleBar({
format: `Generating icons | ${color.cyan(`{bar}`)} | {percentage}% || {value}/{total}`,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});
progressBar.start(icons.length, 0);
const generatedIcons = icons.map((icon) => {
try {
const content = iconComponentTemplate(icon.svg, icon.name);
let filePath = `src/${icon.fileName}.tsx`;
fs.writeFileSync(path.join(__dirname, filePath), content, {
flag: 'w+'
});
progressBar.increment();
filePath = filePath.replace('src/', './').replace('.tsx', '');
return {
...icon,
filePath
};
} catch (err) {
console.error(err);
return '';
}
});
progressBar.stop();
await generateIndexFile(generatedIcons);
}
generateFigmaIcons();