|
1
|
import fse from "fs-extra" |
|
2
|
import path from "path" |
|
3
|
import type { CSSProperties } from "react" |
|
4
|
import { Node, PaintGradient, PaintType } from "../../figma" |
|
5
|
|
|
6
|
import { |
|
7
|
FIGMA_FILE_ID, |
|
8
|
FONT_FAMILY_MONO, |
|
9
|
FONT_FAMILY_SANS, |
|
10
|
} from "../../constants" |
|
11
|
import { client } from "../../figma-client" |
|
12
|
import { logger } from "../../logger" |
|
13
|
import { |
|
14
|
colorToRGBA, |
|
15
|
createLinearGradientComponent, |
|
16
|
gradientValues, |
|
17
|
} from "./utils/colors" |
|
18
|
import { createDropShadowVariable } from "./utils/effects" |
|
19
|
|
|
20
|
type GenerateTokensArgs = { |
|
21
|
output: string |
|
22
|
} |
|
23
|
|
|
24
|
type Tokens = { |
|
25
|
colors: { |
|
26
|
dark: Record<string, string> |
|
27
|
light: Record<string, string> |
|
28
|
} |
|
29
|
effects: { |
|
30
|
dark: Record<string, string> |
|
31
|
light: Record<string, string> |
|
32
|
} |
|
33
|
components: { |
|
34
|
dark: Record<string, CSSProperties> |
|
35
|
light: Record<string, CSSProperties> |
|
36
|
} |
|
37
|
} |
|
38
|
|
|
39
|
export async function generateTokens({ output }: GenerateTokensArgs) { |
|
40
|
logger.info("Fetching file styles") |
|
41
|
|
|
42
|
const res = await client.getFileStyles(FIGMA_FILE_ID).catch((err) => { |
|
43
|
logger.error(`Failed to fetch file styles: ${err.message}`) |
|
44
|
process.exit(1) |
|
45
|
}) |
|
46
|
|
|
47
|
logger.success("Fetched file styles successfully") |
|
48
|
|
|
49
|
const colorNodeIds: string[] = [] |
|
50
|
|
|
51
|
const textNodeIds: string[] = [] |
|
52
|
|
|
53
|
const effectNodeIds: string[] = [] |
|
54
|
|
|
55
|
res.meta?.styles.forEach((style) => { |
|
56
|
if (style.style_type === "FILL") { |
|
57
|
colorNodeIds.push(style.node_id) |
|
58
|
} |
|
59
|
|
|
60
|
if (style.style_type === "TEXT") { |
|
61
|
textNodeIds.push(style.node_id) |
|
62
|
} |
|
63
|
|
|
64
|
if (style.style_type === "EFFECT") { |
|
65
|
effectNodeIds.push(style.node_id) |
|
66
|
} |
|
67
|
}) |
|
68
|
|
|
69
|
logger.info("Fetching file nodes") |
|
70
|
const [colorStyles, textStyles, effectStyles] = await Promise.all([ |
|
71
|
client.getFileNodes<"RECTANGLE">(FIGMA_FILE_ID, { |
|
72
|
ids: colorNodeIds, |
|
73
|
}), |
|
74
|
client.getFileNodes(FIGMA_FILE_ID, { |
|
75
|
ids: textNodeIds, |
|
76
|
}), |
|
77
|
client.getFileNodes<"RECTANGLE">(FIGMA_FILE_ID, { |
|
78
|
ids: effectNodeIds, |
|
79
|
}), |
|
80
|
]) |
|
81
|
.catch((err) => { |
|
82
|
logger.error(`Failed to fetch file nodes: ${err.message}`) |
|
83
|
process.exit(1) |
|
84
|
}) |
|
85
|
.finally(() => { |
|
86
|
logger.success("Fetched file nodes successfully") |
|
87
|
}) |
|
88
|
|
|
89
|
const themeNode: Tokens = { |
|
90
|
colors: { |
|
91
|
dark: {}, |
|
92
|
light: {}, |
|
93
|
}, |
|
94
|
effects: { |
|
95
|
dark: {}, |
|
96
|
light: {}, |
|
97
|
}, |
|
98
|
components: { |
|
99
|
dark: {}, |
|
100
|
light: {}, |
|
101
|
}, |
|
102
|
} |
|
103
|
|
|
104
|
const typo = Object.values(textStyles.nodes).reduce((acc, curr) => { |
|
105
|
if (!curr) { |
|
106
|
return acc |
|
107
|
} |
|
108
|
|
|
109
|
const node = curr.document as unknown as Node<"TEXT"> |
|
110
|
|
|
111
|
const isText = node.name.startsWith("Text") |
|
112
|
|
|
113
|
if (isText) { |
|
114
|
const [_parent, identifier] = node.name.split("/") |
|
115
|
const { lineHeightPx, fontWeight, fontSize } = node.style |
|
116
|
|
|
117
|
const name = "." + identifier.toLowerCase().replace("text", "txt") |
|
118
|
|
|
119
|
const style: CSSProperties = { |
|
120
|
fontSize: `${fontSize / 16}rem`, |
|
121
|
lineHeight: `${lineHeightPx / 16}rem`, |
|
122
|
fontWeight: `${fontWeight}`, |
|
123
|
fontFamily: FONT_FAMILY_SANS.join(", "), |
|
124
|
} |
|
125
|
|
|
126
|
acc[name] = style |
|
127
|
|
|
128
|
return acc |
|
129
|
} |
|
130
|
|
|
131
|
const isHeader = node.name.startsWith("Headers") |
|
132
|
|
|
133
|
if (isHeader) { |
|
134
|
const [theme, identifier] = node.name.split("/") |
|
135
|
|
|
136
|
const formattedTheme = theme.toLowerCase().replace("headers ", "") |
|
137
|
const formattedIdentifier = identifier.toLowerCase() |
|
138
|
|
|
139
|
const name = "." + `${formattedIdentifier}-${formattedTheme}` |
|
140
|
|
|
141
|
const { lineHeightPx, fontSize, fontWeight } = node.style |
|
142
|
|
|
143
|
const style: CSSProperties = { |
|
144
|
fontSize: `${fontSize / 16}rem`, |
|
145
|
lineHeight: `${lineHeightPx / 16}rem`, |
|
146
|
fontWeight: `${fontWeight}`, |
|
147
|
fontFamily: FONT_FAMILY_SANS.join(", "), |
|
148
|
} |
|
149
|
|
|
150
|
acc[name] = style |
|
151
|
|
|
152
|
return acc |
|
153
|
} |
|
154
|
|
|
155
|
const isCodeBlock = node.name.startsWith("Code") |
|
156
|
|
|
157
|
if (isCodeBlock) { |
|
158
|
const [_parent, identifier] = node.name.split("/") |
|
159
|
|
|
160
|
const formattedIdentifier = "." + "code-" + identifier.toLowerCase() |
|
161
|
|
|
162
|
const { lineHeightPx, fontSize, fontWeight } = node.style |
|
163
|
|
|
164
|
const style: CSSProperties = { |
|
165
|
fontSize: `${fontSize / 16}rem`, |
|
166
|
lineHeight: `${lineHeightPx / 16}rem`, |
|
167
|
fontWeight: `${fontWeight}`, |
|
168
|
fontFamily: FONT_FAMILY_MONO.join(", "), |
|
169
|
} |
|
170
|
|
|
171
|
acc[formattedIdentifier] = style |
|
172
|
|
|
173
|
return acc |
|
174
|
} |
|
175
|
|
|
176
|
return acc |
|
177
|
}, {} as Record<string, CSSProperties>) |
|
178
|
|
|
179
|
const typoPath = path.join(output, "tokens", "typography.ts") |
|
180
|
|
|
181
|
logger.info(`Writing typography tokens to file`) |
|
182
|
await fse |
|
183
|
.outputFile( |
|
184
|
typoPath, |
|
185
|
`export const typography = ${JSON.stringify(typo, null, 2)}` |
|
186
|
) |
|
187
|
.then(() => { |
|
188
|
logger.success(`Typography tokens written to file successfully`) |
|
189
|
}) |
|
190
|
.catch((err) => { |
|
191
|
logger.error(`Failed to write typography tokens to file: ${err.message}`) |
|
192
|
process.exit(1) |
|
193
|
}) |
|
194
|
|
|
195
|
Object.values(colorStyles.nodes).reduce((acc, curr) => { |
|
196
|
if (!curr) { |
|
197
|
return acc |
|
198
|
} |
|
199
|
|
|
200
|
const [theme, _, variable] = curr.document.name.split("/") |
|
201
|
|
|
202
|
const lowerCaseTheme = theme.toLowerCase() |
|
203
|
|
|
204
|
if (lowerCaseTheme !== "light" && lowerCaseTheme !== "dark") { |
|
205
|
return acc |
|
206
|
} |
|
207
|
|
|
208
|
const fills = curr.document.fills |
|
209
|
|
|
210
|
const solid = fills.find((fill) => fill.type === "SOLID") |
|
211
|
const gradient = fills.find((fill) => fill.type === "GRADIENT_LINEAR") |
|
212
|
|
|
213
|
if (!solid && !gradient) { |
|
214
|
return acc |
|
215
|
} |
|
216
|
|
|
217
|
const solidVariable = `--${variable}` |
|
218
|
const gradientIdentifier = `${variable}-gradient` |
|
219
|
|
|
220
|
if (solid && solid.color) { |
|
221
|
acc["colors"][lowerCaseTheme][solidVariable] = colorToRGBA( |
|
222
|
solid.color, |
|
223
|
solid.opacity |
|
224
|
) |
|
225
|
} |
|
226
|
|
|
227
|
if (gradient) { |
|
228
|
const values = gradientValues(gradient as PaintGradient) |
|
229
|
|
|
230
|
if (values) { |
|
231
|
if (values.type === PaintType.GRADIENT_LINEAR) { |
|
232
|
const toVariable = `--${gradientIdentifier}-to` |
|
233
|
const fromVariable = `--${gradientIdentifier}-from` |
|
234
|
|
|
235
|
const component = createLinearGradientComponent({ |
|
236
|
...values, |
|
237
|
to: toVariable, |
|
238
|
from: fromVariable, |
|
239
|
}) |
|
240
|
|
|
241
|
if (component) { |
|
242
|
acc["colors"][lowerCaseTheme][fromVariable] = values.from |
|
243
|
acc["colors"][lowerCaseTheme][toVariable] = values.to |
|
244
|
|
|
245
|
acc["components"][lowerCaseTheme][`.${gradientIdentifier}`] = |
|
246
|
component |
|
247
|
} |
|
248
|
} else { |
|
249
|
logger.warn(`Unsupported gradient type: ${values.type}`) |
|
250
|
} |
|
251
|
} |
|
252
|
} |
|
253
|
|
|
254
|
return acc |
|
255
|
}, themeNode) |
|
256
|
|
|
257
|
Object.values(effectStyles.nodes).reduce((acc, curr) => { |
|
258
|
if (!curr) { |
|
259
|
return acc |
|
260
|
} |
|
261
|
|
|
262
|
const [theme, type, variable] = curr.document.name.split("/") |
|
263
|
|
|
264
|
if (!type || !variable) { |
|
265
|
return acc |
|
266
|
} |
|
267
|
|
|
268
|
const lowerCaseTheme = theme.toLowerCase() |
|
269
|
const lowerCaseType = type.toLowerCase() |
|
270
|
|
|
271
|
if (lowerCaseTheme !== "light" && lowerCaseTheme !== "dark") { |
|
272
|
return acc |
|
273
|
} |
|
274
|
|
|
275
|
const effects = curr.document.effects |
|
276
|
|
|
277
|
if (!effects) { |
|
278
|
return acc |
|
279
|
} |
|
280
|
|
|
281
|
const identifier = `--${lowerCaseType}-${variable}` |
|
282
|
|
|
283
|
/** |
|
284
|
* Figma returns effects in reverse order |
|
285
|
* so we need to reverse them back to get the correct order |
|
286
|
*/ |
|
287
|
const reversedEffects = effects.reverse() |
|
288
|
|
|
289
|
const value = createDropShadowVariable(reversedEffects, identifier) |
|
290
|
|
|
291
|
if (!value) { |
|
292
|
return acc |
|
293
|
} |
|
294
|
|
|
295
|
acc["effects"][lowerCaseTheme][identifier] = value |
|
296
|
|
|
297
|
return acc |
|
298
|
}, themeNode) |
|
299
|
|
|
300
|
logger.info("Writing tokens to files") |
|
301
|
logger.info("Writing colors to file") |
|
302
|
|
|
303
|
const colorTokensPath = path.join(output, "tokens", "colors.ts") |
|
304
|
|
|
305
|
await fse |
|
306
|
.outputFile( |
|
307
|
colorTokensPath, |
|
308
|
`export const colors = ${JSON.stringify(themeNode.colors, null, 2)}` |
|
309
|
) |
|
310
|
.then(() => { |
|
311
|
logger.success("Wrote colors to file successfully") |
|
312
|
}) |
|
313
|
.catch((err) => { |
|
314
|
logger.error(`Failed to write colors to file: ${err.message}`) |
|
315
|
process.exit(1) |
|
316
|
}) |
|
317
|
|
|
318
|
logger.info("Writing effects to file") |
|
319
|
|
|
320
|
const effectTokensPath = path.join(output, "tokens", "effects.ts") |
|
321
|
await fse |
|
322
|
.outputFile( |
|
323
|
effectTokensPath, |
|
324
|
`export const effects = ${JSON.stringify(themeNode.effects, null, 2)}` |
|
325
|
) |
|
326
|
.then(() => { |
|
327
|
logger.success("Wrote effects to file successfully") |
|
328
|
}) |
|
329
|
.catch((err) => { |
|
330
|
logger.error(`Failed to write effects to file: ${err.message}`) |
|
331
|
process.exit(1) |
|
332
|
}) |
|
333
|
|
|
334
|
logger.info("Writing components to file") |
|
335
|
|
|
336
|
const componentTokensPath = path.join(output, "tokens", "components.ts") |
|
337
|
await fse |
|
338
|
.outputFile( |
|
339
|
componentTokensPath, |
|
340
|
`export const components = ${JSON.stringify( |
|
341
|
themeNode.components, |
|
342
|
null, |
|
343
|
2 |
|
344
|
)}` |
|
345
|
) |
|
346
|
.then(() => { |
|
347
|
logger.success("Wrote components to file successfully") |
|
348
|
}) |
|
349
|
.catch((err) => { |
|
350
|
logger.error(`Failed to write components to file: ${err.message}`) |
|
351
|
process.exit(1) |
|
352
|
}) |
|
353
|
|
|
354
|
logger.success("Wrote tokens to files successfully") |
|
355
|
|
|
356
|
logger.info("Extending Tailwind config") |
|
357
|
|
|
358
|
const colorsExtension: Record<string, any> = {} |
|
359
|
|
|
360
|
Object.keys(themeNode.colors.light).reduce((acc, curr) => { |
|
361
|
const [prefix, style, state, context, ...others] = |
|
362
|
curr.split(/(?<=\w)-(?=\w)/) |
|
363
|
|
|
364
|
if ( |
|
365
|
state === "gradient" || |
|
366
|
context === "gradient" || |
|
367
|
(others.length > 0 && others.includes("gradient")) |
|
368
|
) { |
|
369
|
// We don't want to add gradients to the tailwind config, as they are added as components |
|
370
|
return acc |
|
371
|
} |
|
372
|
|
|
373
|
const fixedPrefix = prefix.replace("--", "") |
|
374
|
|
|
375
|
if (!acc[fixedPrefix]) { |
|
376
|
acc[fixedPrefix] = {} |
|
377
|
} |
|
378
|
|
|
379
|
if (!acc[fixedPrefix][style]) { |
|
380
|
acc[fixedPrefix][style] = {} |
|
381
|
} |
|
382
|
|
|
383
|
if (!state) { |
|
384
|
acc[fixedPrefix][style] = { |
|
385
|
...acc[fixedPrefix][style], |
|
386
|
DEFAULT: `var(${curr})`, |
|
387
|
} |
|
388
|
|
|
389
|
return acc |
|
390
|
} |
|
391
|
|
|
392
|
if (!acc[fixedPrefix][style][state]) { |
|
393
|
acc[fixedPrefix][style][state] = {} |
|
394
|
} |
|
395
|
|
|
396
|
if (!context) { |
|
397
|
acc[fixedPrefix][style][state] = { |
|
398
|
...acc[fixedPrefix][style][state], |
|
399
|
DEFAULT: `var(${curr})`, |
|
400
|
} |
|
401
|
|
|
402
|
return acc |
|
403
|
} |
|
404
|
|
|
405
|
if (context === "gradient") { |
|
406
|
// We don't want to add gradients to the tailwind config, as they are added as components |
|
407
|
return acc |
|
408
|
} |
|
409
|
|
|
410
|
if (!acc[fixedPrefix][style][state][context]) { |
|
411
|
acc[fixedPrefix][style][state][context] = {} |
|
412
|
} |
|
413
|
|
|
414
|
acc[fixedPrefix][style][state][context] = { |
|
415
|
...acc[fixedPrefix][style][state][context], |
|
416
|
DEFAULT: `var(${curr})`, |
|
417
|
} |
|
418
|
|
|
419
|
return acc |
|
420
|
}, colorsExtension) |
|
421
|
|
|
422
|
const boxShadowExtension: Record<string, any> = {} |
|
423
|
|
|
424
|
Object.keys(themeNode.effects.light).reduce((acc, curr) => { |
|
425
|
const key = `${curr.replace("--", "")}` |
|
426
|
|
|
427
|
acc[key] = `var(${curr})` |
|
428
|
|
|
429
|
return acc |
|
430
|
}, boxShadowExtension) |
|
431
|
|
|
432
|
const themeExtension = { |
|
433
|
extend: { |
|
434
|
colors: { |
|
435
|
ui: colorsExtension, |
|
436
|
}, |
|
437
|
boxShadow: boxShadowExtension, |
|
438
|
}, |
|
439
|
} |
|
440
|
|
|
441
|
const tailwindConfigPath = path.join(output, "extension", "theme.ts") |
|
442
|
await fse |
|
443
|
.outputFile( |
|
444
|
tailwindConfigPath, |
|
445
|
`export const theme = ${JSON.stringify(themeExtension, null, 2)}` |
|
446
|
) |
|
447
|
.then(() => { |
|
448
|
logger.success("Wrote Tailwind config extension successfully") |
|
449
|
}) |
|
450
|
.catch((err) => { |
|
451
|
logger.error(`Failed to write Tailwind config extension: ${err.message}`) |
|
452
|
process.exit(1) |
|
453
|
}) |
|
454
|
|
|
455
|
logger.success("Extended Tailwind config successfully") |
|
456
|
|
|
457
|
// TODO: Add text styles |
|
458
|
} |