-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-devup.ts
More file actions
86 lines (73 loc) · 2.62 KB
/
import-devup.ts
File metadata and controls
86 lines (73 loc) · 2.62 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
import { uploadFile } from '../../utils/upload-file'
import { applyTypography } from './apply-typography'
import { buildTargetStyleNames } from './build-target-style-names'
import type { Devup } from './types'
import { getDevupColorCollection } from './utils/get-devup-color-collection'
import { uploadDevupXlsx } from './utils/upload-devup-xlsx'
export async function importDevup(input: 'json' | 'excel') {
const devup = await loadDevup(input)
await importColors(devup)
await importTypography(devup)
}
async function loadDevup(input: 'json' | 'excel'): Promise<Devup> {
return input === 'json'
? JSON.parse(await uploadFile('.json'))
: await uploadDevupXlsx()
}
async function importColors(devup: Devup) {
const colors = devup.theme?.colors
if (!colors) return
const collection =
(await getDevupColorCollection()) ??
(await figma.variables.createVariableCollection('Devup Colors'))
const variables = await figma.variables.getLocalVariablesAsync()
const variablesByName = new Map<string, Variable>()
for (const variable of variables) {
if (!variablesByName.has(variable.name)) {
variablesByName.set(variable.name, variable)
}
}
const modeIdsByName = new Map(
collection.modes.map((mode) => [mode.name, mode.modeId] as const),
)
const themes = new Set<string>()
const colorNames = new Set<string>()
for (const [theme, value] of Object.entries(colors)) {
let modeId = modeIdsByName.get(theme)
if (!modeId) {
modeId = collection.addMode(theme)
modeIdsByName.set(theme, modeId)
}
for (const [colorKey, colorValue] of Object.entries(value)) {
let variable = variablesByName.get(colorKey)
if (!variable) {
variable = figma.variables.createVariable(colorKey, collection, 'COLOR')
variablesByName.set(colorKey, variable)
variables.push(variable)
}
variable.setValueForMode(modeId, figma.util.rgba(colorValue))
colorNames.add(colorKey)
}
themes.add(theme)
}
for (const theme of collection.modes.filter(
(mode) => !themes.has(mode.name),
)) {
collection.removeMode(theme.modeId)
}
for (const variable of variables) {
if (colorNames.has(variable.name)) continue
variable.remove()
}
}
async function importTypography(devup: Devup) {
const typography = devup.theme?.typography
if (!typography) return
const styles = await figma.getLocalTextStylesAsync()
for (const [style, value] of Object.entries(typography)) {
const targetStyleNames = buildTargetStyleNames(style, value)
for (const [target, typo] of targetStyleNames) {
await applyTypography(target, typo, styles)
}
}
}