From 72b828d98d747e68ea6d743dead79ca13671a74e Mon Sep 17 00:00:00 2001 From: jorenbroekema Date: Thu, 19 Oct 2023 11:42:52 +0200 Subject: [PATCH] fix: order enabled sets after source sets for SD integration --- .changeset/tame-buckets-push.md | 5 +++++ src/permutateThemes.ts | 17 ++++++++++++++--- test/spec/permutateThemes.spec.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .changeset/tame-buckets-push.md diff --git a/.changeset/tame-buckets-push.md b/.changeset/tame-buckets-push.md new file mode 100644 index 0000000..fa599fb --- /dev/null +++ b/.changeset/tame-buckets-push.md @@ -0,0 +1,5 @@ +--- +'@tokens-studio/sd-transforms': patch +--- + +Fix permutateThemes to order "enabled" sets below "source" sets so enabled sets overrides are prioritized in Style-Dictionary. diff --git a/src/permutateThemes.ts b/src/permutateThemes.ts index cb662a9..e6b3c78 100644 --- a/src/permutateThemes.ts +++ b/src/permutateThemes.ts @@ -53,9 +53,20 @@ export function permutateThemes(themes: ThemeObject[], { separator = '-' } = {} } function filterTokenSets(tokensets: Record) { - return Object.entries(tokensets) - .filter(([, val]) => val !== 'disabled') - .map(entry => entry[0]); + return ( + Object.entries(tokensets) + .filter(([, val]) => val !== 'disabled') + // ensure source type sets are always ordered before enabled type sets + .sort((a, b) => { + if (a[1] === 'source' && b[1] === 'enabled') { + return -1; + } else if (a[1] === 'enabled' && b[1] === 'source') { + return 1; + } + return 0; + }) + .map(entry => entry[0]) + ); } // cartesian permutations: [[1,2], [3,4]] -> [[1,3], [1,4], [2,3], [2,4]] diff --git a/test/spec/permutateThemes.spec.ts b/test/spec/permutateThemes.spec.ts index f8c9bb0..83da0fc 100644 --- a/test/spec/permutateThemes.spec.ts +++ b/test/spec/permutateThemes.spec.ts @@ -144,6 +144,36 @@ describe('transform dimension', () => { }); }); + it('orders enabled sets to the bottom compared to source sets', () => { + expect( + permutateThemes([ + { + id: '0', + name: 'light', + group: 'theme', + selectedTokenSets: { + light: enabled, + theme: enabled, + core: source, + }, + }, + { + id: '1', + name: 'dark', + group: 'theme', + selectedTokenSets: { + dark: enabled, + theme: enabled, + core: source, + }, + }, + ]), + ).to.eql({ + dark: ['core', 'dark', 'theme'], + light: ['core', 'light', 'theme'], + }); + }); + it('throws when not all themes have a group property', () => { expect(() => permutateThemes([