Skip to content

Commit 5d89108

Browse files
fallback (#1130)
1 parent 34a2a25 commit 5d89108

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

scripts/buildFigma.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {PrimerStyleDictionary} from '../src/primerStyleDictionary.js'
33
import {themes} from './themes.config.js'
44
import {figma} from '../src/platforms/index.js'
55
import type {ConfigGeneratorOptions} from '../src/types/styleDictionaryConfigGenerator.js'
6+
import {getFallbackTheme} from './utilities/getFallbackTheme.js'
67

78
const buildFigma = async (buildOptions: ConfigGeneratorOptions): Promise<void> => {
89
/** -----------------------------------
@@ -225,7 +226,9 @@ const buildFigma = async (buildOptions: ConfigGeneratorOptions): Promise<void> =
225226
source,
226227
include,
227228
platforms: {
228-
figma: figma(`figma/shadows/${name}.json`, buildOptions.prefix, buildOptions.buildPath, {theme}),
229+
figma: figma(`figma/shadows/${name}.json`, buildOptions.prefix, buildOptions.buildPath, {
230+
theme: [theme, getFallbackTheme(theme)],
231+
}),
229232
},
230233
})
231234

scripts/buildTokens.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {TokenBuildInput} from '../src/types/tokenBuildInput.js'
1010
import glob from 'fast-glob'
1111
import {themes} from './themes.config.js'
1212
import fs from 'fs'
13+
import {getFallbackTheme} from './utilities/getFallbackTheme.js'
1314

1415
/**
1516
* getStyleDictionaryConfig
@@ -39,10 +40,10 @@ const getStyleDictionaryConfig: StyleDictionaryConfigGenerator = (
3940
Object.entries({
4041
css: css(`css/${filename}.css`, options.prefix, options.buildPath, {
4142
themed: options.themed,
42-
theme: options.theme,
43+
theme: [options.theme, getFallbackTheme(options.theme)],
4344
}),
4445
docJson: docJson(`docs/${filename}.json`, options.prefix, options.buildPath, {
45-
theme: options.theme,
46+
theme: [options.theme, getFallbackTheme(options.theme)],
4647
}),
4748
styleLint: styleLint(`styleLint/${filename}.json`, options.prefix, options.buildPath, {
4849
theme: options.theme,
@@ -66,7 +67,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
6667
platforms: {
6768
css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
6869
themed: true,
69-
theme,
70+
theme: [theme, getFallbackTheme(theme)],
7071
}),
7172
},
7273
})
@@ -88,7 +89,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
8889
`functional/themes/${filename}`,
8990
source,
9091
include,
91-
{...buildOptions, themed: true, theme},
92+
{...buildOptions, themed: true, theme: [theme, getFallbackTheme(theme)]},
9293
// disable fallbacks for themes
9394
{fallbacks: undefined},
9495
),

scripts/diffThemes.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
import {PrimerStyleDictionary} from '../src/primerStyleDictionary.js'
2-
import {flattenTokens} from 'style-dictionary/utils'
32
import {themes as themesConfigArray} from './themes.config.js'
43
import type {Dictionary} from 'style-dictionary/types'
54

65
const tokenNameArray = ({dictionary}: {dictionary: Dictionary}) =>
76
dictionary.allTokens.map(({name}: {name: string}) => name)
87

98
const themesArray = await Promise.all(
10-
themesConfigArray.map(async ({filename, source, include}): Promise<[string, string[]]> => {
9+
themesConfigArray.map(async ({filename, source, include, theme}): Promise<[string, string[]]> => {
1110
const sd = await PrimerStyleDictionary.extend({
1211
source,
1312
include,
13+
log: {
14+
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
15+
verbosity: 'verbose', // 'default' | 'silent' | 'verbose'
16+
errors: {
17+
brokenReferences: 'throw', // 'throw' | 'console'
18+
},
19+
},
1420
platforms: {
1521
json: {
22+
preprocessors: ['themeOverrides'],
1623
transforms: ['name/pathToDotNotation'],
24+
options: {
25+
themeOverrides: {
26+
theme,
27+
},
28+
},
1729
},
1830
},
1931
})
2032

21-
const tokens = await sd.exportPlatform('json')
33+
const dictionary = await sd.getPlatformTokens('json')
2234

2335
return [
2436
filename,
2537
tokenNameArray({
26-
dictionary: {
27-
tokens,
28-
allTokens: await flattenTokens(tokens),
29-
},
38+
dictionary,
3039
}),
3140
]
3241
}),

scripts/utilities/getFallbackTheme.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const getFallbackTheme = (
2+
theme?: string | [string | undefined, string | undefined],
3+
): 'light' | 'dark' | undefined => {
4+
if (Array.isArray(theme)) {
5+
theme = theme[1] || theme[0]
6+
}
7+
return theme ? (theme.startsWith('light') ? 'light' : 'dark') : undefined
8+
}

scripts/utilities/validateTokenWithSchema.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ export type validationErrors = {
66
errorsByPath: Record<string, ZodIssue[]>
77
}
88

9+
const unpackErrorDetails = details => {
10+
const errorObjectByCode = {
11+
//eslint-disable-next-line camelcase
12+
invalid_union: 'unionErrors',
13+
}
14+
return {
15+
...details,
16+
errors: details[errorObjectByCode[details.code]] || [],
17+
}
18+
}
19+
920
export const validateTokenWithSchema = (
1021
fileName: string,
1122
tokens: unknown,
@@ -23,7 +34,7 @@ export const validateTokenWithSchema = (
2334
if (!acc[item.path.join('.')]) acc[item.path.join('.')] = []
2435
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2536
// @ts-ignore
26-
acc[item.path.join('.')].push(item)
37+
acc[item.path.join('.')].push(unpackErrorDetails(item))
2738
return acc
2839
}, {}),
2940
}

scripts/validateTokenJson.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ if (getFlag('--silent') === null) {
7171
// eslint-disable-next-line no-console
7272
console.log(
7373
fail.errorsByPath[path]
74-
.map(({message}) =>
75-
message.replace(/\*\*(.*?)\*\*/g, '- \u001b[31;1m\u001b[1m$1\u001b[0m').replace(/\n(?!-)/g, '\n ↳ '),
74+
.map(
75+
({message, code, errors}) =>
76+
`${message.replace(/\*\*(.*?)\*\*/g, '- \u001b[31;1m\u001b[1m$1\u001b[0m').replace(/\n(?!-)/g, '\n ↳ ')}, code: ${code}, errors:\n ${errors
77+
.map(error => {
78+
return `- ${error.issues[0].code}: ${error.issues[0].message}`
79+
})
80+
.join('\n')}`,
7681
)
7782
.join('\n'),
7883
'\n',

0 commit comments

Comments
 (0)