Skip to content

Commit 09d7088

Browse files
authored
Merge pull request #35 from dev-five-git/optimize-import
Optimize import
2 parents 4c783d7 + ebced7e commit 09d7088

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/commands/devup/__tests__/import-devup.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,72 @@ describe('import-devup (standalone file)', () => {
7979
expect(loadFontAsync).toHaveBeenCalled()
8080
})
8181

82+
test('removes only variables from the Devup color collection', async () => {
83+
const uploadXlsx = mock(() =>
84+
Promise.resolve({
85+
theme: {
86+
colors: { Light: { primary: '#111111' } },
87+
},
88+
}),
89+
)
90+
spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx)
91+
92+
const removeDevupVariable = mock(() => {})
93+
const removeOtherVariable = mock(() => {})
94+
const devupVariable = {
95+
id: 'devup-primary',
96+
name: 'legacy',
97+
setValueForMode: mock(() => {}),
98+
remove: removeDevupVariable,
99+
} as unknown as Variable
100+
const otherCollectionVariable = {
101+
id: 'other-primary',
102+
name: 'legacy',
103+
setValueForMode: mock(() => {}),
104+
remove: removeOtherVariable,
105+
} as unknown as Variable
106+
const createdVariable = {
107+
id: 'created-primary',
108+
name: 'primary',
109+
setValueForMode: mock(() => {}),
110+
remove: mock(() => {}),
111+
} as unknown as Variable
112+
const createVariable = mock(() => createdVariable)
113+
const collection = {
114+
variableIds: ['devup-primary'],
115+
modes: [{ modeId: 'light-id', name: 'Light' }],
116+
addMode: mock(() => 'light-id'),
117+
removeMode: mock(() => {}),
118+
} as unknown as VariableCollection
119+
120+
;(globalThis as { figma?: unknown }).figma = {
121+
util: { rgba: (v: unknown) => v },
122+
variables: {
123+
getLocalVariableCollectionsAsync: async () => [collection],
124+
getLocalVariablesAsync: async () => [
125+
devupVariable,
126+
otherCollectionVariable,
127+
],
128+
createVariableCollection: () => collection,
129+
createVariable,
130+
},
131+
getLocalTextStylesAsync: async () => [],
132+
createTextStyle: mock(
133+
() =>
134+
({
135+
name: '',
136+
}) as unknown as TextStyle,
137+
),
138+
loadFontAsync: mock(() => Promise.resolve()),
139+
} as unknown as typeof figma
140+
141+
await importDevup('excel')
142+
143+
expect(removeDevupVariable).toHaveBeenCalledTimes(1)
144+
expect(removeOtherVariable).not.toHaveBeenCalled()
145+
expect(createVariable).toHaveBeenCalledWith('primary', collection, 'COLOR')
146+
})
147+
82148
test('imports array typography and skips nulls', async () => {
83149
const uploadXlsx = mock(() =>
84150
Promise.resolve({

src/commands/devup/import-devup.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ async function importColors(devup: Devup) {
2525
(await getDevupColorCollection()) ??
2626
(await figma.variables.createVariableCollection('Devup Colors'))
2727
const variables = await figma.variables.getLocalVariablesAsync()
28+
const collectionVariableIds = new Set(collection.variableIds)
2829
const variablesByName = new Map<string, Variable>()
2930
for (const variable of variables) {
30-
if (!variablesByName.has(variable.name)) {
31+
if (
32+
collectionVariableIds.has(variable.id) &&
33+
!variablesByName.has(variable.name)
34+
) {
3135
variablesByName.set(variable.name, variable)
3236
}
3337
}
@@ -51,6 +55,7 @@ async function importColors(devup: Devup) {
5155
variable = figma.variables.createVariable(colorKey, collection, 'COLOR')
5256
variablesByName.set(colorKey, variable)
5357
variables.push(variable)
58+
collectionVariableIds.add(variable.id)
5459
}
5560

5661
variable.setValueForMode(modeId, figma.util.rgba(colorValue))
@@ -66,7 +71,12 @@ async function importColors(devup: Devup) {
6671
}
6772

6873
for (const variable of variables) {
69-
if (colorNames.has(variable.name)) continue
74+
if (
75+
!collectionVariableIds.has(variable.id) ||
76+
colorNames.has(variable.name)
77+
) {
78+
continue
79+
}
7080
variable.remove()
7181
}
7282
}

0 commit comments

Comments
 (0)