diff --git a/src/commands/devup/__tests__/import-devup.test.ts b/src/commands/devup/__tests__/import-devup.test.ts index c0d98d7..ceac7d4 100644 --- a/src/commands/devup/__tests__/import-devup.test.ts +++ b/src/commands/devup/__tests__/import-devup.test.ts @@ -79,6 +79,72 @@ describe('import-devup (standalone file)', () => { expect(loadFontAsync).toHaveBeenCalled() }) + test('removes only variables from the Devup color collection', async () => { + const uploadXlsx = mock(() => + Promise.resolve({ + theme: { + colors: { Light: { primary: '#111111' } }, + }, + }), + ) + spyOn(uploadXlsxModule, 'uploadDevupXlsx').mockImplementation(uploadXlsx) + + const removeDevupVariable = mock(() => {}) + const removeOtherVariable = mock(() => {}) + const devupVariable = { + id: 'devup-primary', + name: 'legacy', + setValueForMode: mock(() => {}), + remove: removeDevupVariable, + } as unknown as Variable + const otherCollectionVariable = { + id: 'other-primary', + name: 'legacy', + setValueForMode: mock(() => {}), + remove: removeOtherVariable, + } as unknown as Variable + const createdVariable = { + id: 'created-primary', + name: 'primary', + setValueForMode: mock(() => {}), + remove: mock(() => {}), + } as unknown as Variable + const createVariable = mock(() => createdVariable) + const collection = { + variableIds: ['devup-primary'], + modes: [{ modeId: 'light-id', name: 'Light' }], + addMode: mock(() => 'light-id'), + removeMode: mock(() => {}), + } as unknown as VariableCollection + + ;(globalThis as { figma?: unknown }).figma = { + util: { rgba: (v: unknown) => v }, + variables: { + getLocalVariableCollectionsAsync: async () => [collection], + getLocalVariablesAsync: async () => [ + devupVariable, + otherCollectionVariable, + ], + createVariableCollection: () => collection, + createVariable, + }, + getLocalTextStylesAsync: async () => [], + createTextStyle: mock( + () => + ({ + name: '', + }) as unknown as TextStyle, + ), + loadFontAsync: mock(() => Promise.resolve()), + } as unknown as typeof figma + + await importDevup('excel') + + expect(removeDevupVariable).toHaveBeenCalledTimes(1) + expect(removeOtherVariable).not.toHaveBeenCalled() + expect(createVariable).toHaveBeenCalledWith('primary', collection, 'COLOR') + }) + test('imports array typography and skips nulls', async () => { const uploadXlsx = mock(() => Promise.resolve({ diff --git a/src/commands/devup/import-devup.ts b/src/commands/devup/import-devup.ts index 6916d97..832fb0f 100644 --- a/src/commands/devup/import-devup.ts +++ b/src/commands/devup/import-devup.ts @@ -25,9 +25,13 @@ async function importColors(devup: Devup) { (await getDevupColorCollection()) ?? (await figma.variables.createVariableCollection('Devup Colors')) const variables = await figma.variables.getLocalVariablesAsync() + const collectionVariableIds = new Set(collection.variableIds) const variablesByName = new Map() for (const variable of variables) { - if (!variablesByName.has(variable.name)) { + if ( + collectionVariableIds.has(variable.id) && + !variablesByName.has(variable.name) + ) { variablesByName.set(variable.name, variable) } } @@ -51,6 +55,7 @@ async function importColors(devup: Devup) { variable = figma.variables.createVariable(colorKey, collection, 'COLOR') variablesByName.set(colorKey, variable) variables.push(variable) + collectionVariableIds.add(variable.id) } variable.setValueForMode(modeId, figma.util.rgba(colorValue)) @@ -66,7 +71,12 @@ async function importColors(devup: Devup) { } for (const variable of variables) { - if (colorNames.has(variable.name)) continue + if ( + !collectionVariableIds.has(variable.id) || + colorNames.has(variable.name) + ) { + continue + } variable.remove() } }