@primer/primitives 9.0.3 → 9.1.0-rc.73606a1a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/PrimerStyleDictionary.d.ts +9 -0
- package/dist/build/PrimerStyleDictionary.js +93 -0
- package/dist/build/filters/index.d.ts +13 -0
- package/dist/build/filters/index.js +29 -0
- package/{src/filters/isBorder.ts → dist/build/filters/isBorder.d.ts} +2 -5
- package/dist/build/filters/isBorder.js +12 -0
- package/{src/filters/isColor.ts → dist/build/filters/isColor.d.ts} +2 -5
- package/dist/build/filters/isColor.js +12 -0
- package/dist/build/filters/isColorWithAlpha.d.ts +7 -0
- package/dist/build/filters/isColorWithAlpha.js +13 -0
- package/dist/build/filters/isColorWithMix.d.ts +7 -0
- package/dist/build/filters/isColorWithMix.js +28 -0
- package/{src/filters/isDeprecated.ts → dist/build/filters/isDeprecated.d.ts} +2 -5
- package/dist/build/filters/isDeprecated.js +12 -0
- package/{src/filters/isDimension.ts → dist/build/filters/isDimension.d.ts} +2 -5
- package/dist/build/filters/isDimension.js +12 -0
- package/{src/filters/isDuration.ts → dist/build/filters/isDuration.d.ts} +2 -5
- package/dist/build/filters/isDuration.js +12 -0
- package/{src/filters/isFontFamily.ts → dist/build/filters/isFontFamily.d.ts} +2 -5
- package/dist/build/filters/isFontFamily.js +12 -0
- package/{src/filters/isFontWeight.ts → dist/build/filters/isFontWeight.d.ts} +2 -5
- package/dist/build/filters/isFontWeight.js +12 -0
- package/dist/build/filters/isFromFile.d.ts +8 -0
- package/dist/build/filters/isFromFile.js +14 -0
- package/{src/filters/isNumber.ts → dist/build/filters/isNumber.d.ts} +2 -3
- package/dist/build/filters/isNumber.js +10 -0
- package/{src/filters/isShadow.ts → dist/build/filters/isShadow.d.ts} +2 -5
- package/dist/build/filters/isShadow.js +12 -0
- package/{src/filters/isSource.ts → dist/build/filters/isSource.d.ts} +2 -5
- package/dist/build/filters/isSource.js +12 -0
- package/{src/filters/isTypography.ts → dist/build/filters/isTypography.d.ts} +2 -5
- package/dist/build/filters/isTypography.js +12 -0
- package/dist/build/formats/cssAdvanced.d.ts +2 -0
- package/dist/build/formats/cssAdvanced.js +85 -0
- package/dist/build/formats/cssCustomMedia.d.ts +7 -0
- package/dist/build/formats/cssCustomMedia.js +25 -0
- package/dist/build/formats/index.d.ts +9 -0
- package/dist/build/formats/index.js +21 -0
- package/dist/build/formats/javascriptCommonJs.d.ts +7 -0
- package/dist/build/formats/javascriptCommonJs.js +25 -0
- package/dist/build/formats/javascriptEsm.d.ts +7 -0
- package/dist/build/formats/javascriptEsm.js +25 -0
- package/dist/build/formats/jsonFigma.d.ts +7 -0
- package/dist/build/formats/jsonFigma.js +116 -0
- package/dist/build/formats/jsonNestedPrefixed.d.ts +8 -0
- package/dist/build/formats/jsonNestedPrefixed.js +28 -0
- package/dist/build/formats/jsonOneDimensional.d.ts +8 -0
- package/dist/build/formats/jsonOneDimensional.js +24 -0
- package/dist/build/formats/jsonPostCssFallback.d.ts +8 -0
- package/dist/build/formats/jsonPostCssFallback.js +21 -0
- package/dist/build/formats/typescriptExportDefinition.d.ts +7 -0
- package/dist/build/formats/typescriptExportDefinition.js +166 -0
- package/{src/formats/utilities/jsonToFlat.ts → dist/build/formats/utilities/jsonToFlat.d.ts} +4 -3
- package/dist/build/formats/utilities/jsonToFlat.js +12 -0
- package/dist/build/formats/utilities/jsonToNestedValue.d.ts +8 -0
- package/dist/build/formats/utilities/jsonToNestedValue.js +25 -0
- package/dist/build/formats/utilities/prefixTokens.d.ts +8 -0
- package/dist/build/formats/utilities/prefixTokens.js +17 -0
- package/dist/build/parsers/index.d.ts +1 -0
- package/dist/build/parsers/index.js +5 -0
- package/dist/build/parsers/w3cJsonParser.d.ts +6 -0
- package/dist/build/parsers/w3cJsonParser.js +27 -0
- package/dist/build/platforms/css.d.ts +2 -0
- package/dist/build/platforms/css.js +88 -0
- package/dist/build/platforms/deprecatedJson.d.ts +2 -0
- package/dist/build/platforms/deprecatedJson.js +17 -0
- package/dist/build/platforms/docJson.d.ts +2 -0
- package/dist/build/platforms/docJson.js +35 -0
- package/dist/build/platforms/fallbacks.d.ts +2 -0
- package/dist/build/platforms/fallbacks.js +35 -0
- package/dist/build/platforms/figma.d.ts +2 -0
- package/dist/build/platforms/figma.js +53 -0
- package/dist/build/platforms/index.d.ts +10 -0
- package/dist/build/platforms/index.js +23 -0
- package/dist/build/platforms/javascript.d.ts +2 -0
- package/dist/build/platforms/javascript.js +31 -0
- package/dist/build/platforms/json.d.ts +2 -0
- package/dist/build/platforms/json.js +33 -0
- package/dist/build/platforms/styleLint.d.ts +2 -0
- package/dist/build/platforms/styleLint.js +35 -0
- package/dist/build/platforms/typeDefinitions.d.ts +2 -0
- package/dist/build/platforms/typeDefinitions.js +31 -0
- package/dist/build/platforms/typescript.d.ts +2 -0
- package/dist/build/platforms/typescript.js +31 -0
- package/dist/build/schemas/alphaValue.d.ts +2 -0
- package/dist/build/schemas/alphaValue.js +8 -0
- package/dist/build/schemas/baseToken.d.ts +11 -0
- package/dist/build/schemas/baseToken.js +10 -0
- package/dist/build/schemas/borderToken.d.ts +51 -0
- package/dist/build/schemas/borderToken.js +20 -0
- package/dist/build/schemas/collections.d.ts +6 -0
- package/dist/build/schemas/collections.js +18 -0
- package/dist/build/schemas/colorHexValue.d.ts +2 -0
- package/dist/build/schemas/colorHexValue.js +12 -0
- package/dist/build/schemas/colorToken.d.ts +80 -0
- package/dist/build/schemas/colorToken.js +52 -0
- package/dist/build/schemas/designToken.d.ts +2 -0
- package/dist/build/schemas/designToken.js +37 -0
- package/dist/build/schemas/dimensionToken.d.ts +52 -0
- package/dist/build/schemas/dimensionToken.js +36 -0
- package/dist/build/schemas/dimensionValue.d.ts +2 -0
- package/dist/build/schemas/dimensionValue.js +12 -0
- package/dist/build/schemas/durationToken.d.ts +18 -0
- package/dist/build/schemas/durationToken.js +14 -0
- package/dist/build/schemas/durationValue.d.ts +2 -0
- package/dist/build/schemas/durationValue.js +8 -0
- package/dist/build/schemas/fontFamilyToken.d.ts +52 -0
- package/dist/build/schemas/fontFamilyToken.js +23 -0
- package/dist/build/schemas/fontWeightToken.d.ts +52 -0
- package/dist/build/schemas/fontWeightToken.js +24 -0
- package/dist/build/schemas/fontWeightValue.d.ts +2 -0
- package/dist/build/schemas/fontWeightValue.js +9 -0
- package/dist/build/schemas/numberToken.d.ts +71 -0
- package/dist/build/schemas/numberToken.js +30 -0
- package/dist/build/schemas/referenceValue.d.ts +2 -0
- package/dist/build/schemas/referenceValue.js +8 -0
- package/dist/build/schemas/scopes.d.ts +5 -0
- package/dist/build/schemas/scopes.js +34 -0
- package/dist/build/schemas/shadowToken.d.ts +157 -0
- package/dist/build/schemas/shadowToken.js +46 -0
- package/dist/build/schemas/stringToken.d.ts +18 -0
- package/dist/build/schemas/stringToken.js +13 -0
- package/dist/build/schemas/tokenName.d.ts +2 -0
- package/dist/build/schemas/tokenName.js +8 -0
- package/dist/build/schemas/tokenType.d.ts +3 -0
- package/dist/build/schemas/tokenType.js +6 -0
- package/dist/build/schemas/typographyToken.d.ts +59 -0
- package/dist/build/schemas/typographyToken.js +21 -0
- package/dist/build/schemas/validTokenType.d.ts +5 -0
- package/dist/build/schemas/validTokenType.js +34 -0
- package/dist/build/schemas/viewportRangeToken.d.ts +18 -0
- package/dist/build/schemas/viewportRangeToken.js +13 -0
- package/dist/build/transformers/borderToCss.d.ts +8 -0
- package/dist/build/transformers/borderToCss.js +38 -0
- package/dist/build/transformers/colorToHex.d.ts +8 -0
- package/dist/build/transformers/colorToHex.js +18 -0
- package/dist/build/transformers/colorToHexAlpha.d.ts +8 -0
- package/dist/build/transformers/colorToHexAlpha.js +25 -0
- package/dist/build/transformers/colorToHexMix.d.ts +8 -0
- package/dist/build/transformers/colorToHexMix.js +25 -0
- package/dist/build/transformers/colorToRgbAlpha.d.ts +8 -0
- package/dist/build/transformers/colorToRgbAlpha.js +22 -0
- package/dist/build/transformers/colorToRgbaFloat.d.ts +8 -0
- package/dist/build/transformers/colorToRgbaFloat.js +50 -0
- package/dist/build/transformers/dimensionToPixelUnitless.d.ts +8 -0
- package/dist/build/transformers/dimensionToPixelUnitless.js +50 -0
- package/dist/build/transformers/dimensionToRem.d.ts +8 -0
- package/dist/build/transformers/dimensionToRem.js +47 -0
- package/dist/build/transformers/dimensionToRemPxArray.d.ts +8 -0
- package/dist/build/transformers/dimensionToRemPxArray.js +47 -0
- package/dist/build/transformers/durationToCss.d.ts +8 -0
- package/dist/build/transformers/durationToCss.js +30 -0
- package/dist/build/transformers/figmaAttributes.d.ts +8 -0
- package/dist/build/transformers/figmaAttributes.js +57 -0
- package/dist/build/transformers/floatToPixel.d.ts +15 -0
- package/dist/build/transformers/floatToPixel.js +41 -0
- package/dist/build/transformers/fontFamilyToCss.d.ts +14 -0
- package/dist/build/transformers/fontFamilyToCss.js +40 -0
- package/dist/build/transformers/fontFamilyToFigma.d.ts +14 -0
- package/dist/build/transformers/fontFamilyToFigma.js +46 -0
- package/dist/build/transformers/fontWeightToNumber.d.ts +9 -0
- package/dist/build/transformers/fontWeightToNumber.js +53 -0
- package/dist/build/transformers/index.d.ts +25 -0
- package/dist/build/transformers/index.js +53 -0
- package/dist/build/transformers/jsonDeprecated.d.ts +8 -0
- package/dist/build/transformers/jsonDeprecated.js +16 -0
- package/{src/transformers/namePathToCamelCase.ts → dist/build/transformers/namePathToCamelCase.d.ts} +2 -8
- package/dist/build/transformers/namePathToCamelCase.js +14 -0
- package/dist/build/transformers/namePathToDotNotation.d.ts +8 -0
- package/dist/build/transformers/namePathToDotNotation.js +33 -0
- package/dist/build/transformers/namePathToFigma.d.ts +9 -0
- package/dist/build/transformers/namePathToFigma.js +24 -0
- package/dist/build/transformers/namePathToKebabCase.d.ts +8 -0
- package/dist/build/transformers/namePathToKebabCase.js +18 -0
- package/dist/build/transformers/namePathToPascalCase.d.ts +8 -0
- package/dist/build/transformers/namePathToPascalCase.js +14 -0
- package/dist/build/transformers/namePathToSlashNotation.d.ts +8 -0
- package/dist/build/transformers/namePathToSlashNotation.js +18 -0
- package/dist/build/transformers/shadowToCss.d.ts +8 -0
- package/dist/build/transformers/shadowToCss.js +35 -0
- package/dist/build/transformers/typographyToCss.d.ts +8 -0
- package/dist/build/transformers/typographyToCss.js +29 -0
- package/dist/build/transformers/utilities/alpha.d.ts +9 -0
- package/dist/build/transformers/utilities/alpha.js +20 -0
- package/dist/build/transformers/utilities/checkRequiredTokenProperties.d.ts +7 -0
- package/dist/build/transformers/utilities/checkRequiredTokenProperties.js +17 -0
- package/dist/build/transformers/utilities/getTokenValue.d.ts +2 -0
- package/dist/build/transformers/utilities/getTokenValue.js +18 -0
- package/dist/build/transformers/utilities/hasSpaceInString.d.ts +1 -0
- package/dist/build/transformers/utilities/hasSpaceInString.js +10 -0
- package/dist/build/transformers/utilities/hexToRgbaFloat.d.ts +2 -0
- package/dist/build/transformers/utilities/hexToRgbaFloat.js +33 -0
- package/dist/build/transformers/utilities/invalidTokenError.d.ts +7 -0
- package/dist/build/transformers/utilities/invalidTokenError.js +26 -0
- package/dist/build/transformers/utilities/isRgbaFloat.d.ts +7 -0
- package/dist/build/transformers/utilities/isRgbaFloat.js +24 -0
- package/dist/build/transformers/utilities/mix.d.ts +5 -0
- package/dist/build/transformers/utilities/mix.js +40 -0
- package/dist/build/transformers/utilities/rgbaFloatToHex.d.ts +6 -0
- package/dist/build/transformers/utilities/rgbaFloatToHex.js +15 -0
- package/dist/build/utilities/copyFromDir.d.ts +8 -0
- package/dist/build/utilities/copyFromDir.js +34 -0
- package/dist/build/utilities/filterStringArray.d.ts +1 -0
- package/dist/build/utilities/filterStringArray.js +17 -0
- package/{src/utilities/getFlag.ts → dist/build/utilities/getFlag.d.ts} +1 -5
- package/dist/build/utilities/getFlag.js +16 -0
- package/dist/build/utilities/index.d.ts +3 -0
- package/dist/build/utilities/index.js +9 -0
- package/dist/build/utilities/joinFriendly.d.ts +1 -0
- package/dist/build/utilities/joinFriendly.js +5 -0
- package/dist/build/utilities/schemaErrorMessage.d.ts +1 -0
- package/dist/build/utilities/schemaErrorMessage.js +5 -0
- package/dist/build/utilities/toCamelCase.d.ts +1 -0
- package/dist/build/utilities/toCamelCase.js +20 -0
- package/dist/build/utilities/toPascalCase.d.ts +1 -0
- package/dist/build/utilities/toPascalCase.js +17 -0
- package/dist/build/utilities/treeWalker.d.ts +8 -0
- package/dist/build/utilities/treeWalker.js +27 -0
- package/{src/utilities/upperCaseFirstCharacter.ts → dist/build/utilities/upperCaseFirstCharacter.d.ts} +1 -3
- package/dist/build/utilities/upperCaseFirstCharacter.js +14 -0
- package/package.json +4 -7
- package/src/PrimerStyleDictionary.ts +0 -232
- package/src/filters/index.ts +0 -13
- package/src/filters/isColorWithAlpha.ts +0 -11
- package/src/filters/isColorWithMix.ts +0 -33
- package/src/filters/isFromFile.ts +0 -12
- package/src/formats/cssAdvanced.ts +0 -99
- package/src/formats/cssCustomMedia.ts +0 -24
- package/src/formats/index.ts +0 -9
- package/src/formats/javascriptCommonJs.ts +0 -21
- package/src/formats/javascriptEsm.ts +0 -21
- package/src/formats/jsonFigma.ts +0 -134
- package/src/formats/jsonNestedPrefixed.ts +0 -23
- package/src/formats/jsonOneDimensional.ts +0 -18
- package/src/formats/jsonPostCssFallback.ts +0 -16
- package/src/formats/typescriptExportDefinition.ts +0 -170
- package/src/formats/utilities/jsonToNestedValue.ts +0 -20
- package/src/formats/utilities/prefixTokens.ts +0 -17
- package/src/parsers/index.ts +0 -1
- package/src/parsers/w3cJsonParser.ts +0 -25
- package/src/platforms/css.ts +0 -101
- package/src/platforms/deprecatedJson.ts +0 -16
- package/src/platforms/docJson.ts +0 -34
- package/src/platforms/fallbacks.ts +0 -34
- package/src/platforms/figma.ts +0 -58
- package/src/platforms/index.ts +0 -10
- package/src/platforms/javascript.ts +0 -30
- package/src/platforms/json.ts +0 -32
- package/src/platforms/styleLint.ts +0 -34
- package/src/platforms/typeDefinitions.ts +0 -30
- package/src/platforms/typescript.ts +0 -30
- package/src/schemas/alphaValue.ts +0 -12
- package/src/schemas/baseToken.ts +0 -8
- package/src/schemas/borderToken.ts +0 -19
- package/src/schemas/collections.ts +0 -49
- package/src/schemas/colorHexValue.ts +0 -18
- package/src/schemas/colorToken.ts +0 -50
- package/src/schemas/designToken.ts +0 -38
- package/src/schemas/dimensionToken.ts +0 -34
- package/src/schemas/dimensionValue.ts +0 -16
- package/src/schemas/durationToken.ts +0 -12
- package/src/schemas/durationValue.ts +0 -9
- package/src/schemas/fontFamilyToken.ts +0 -21
- package/src/schemas/fontWeightToken.ts +0 -22
- package/src/schemas/fontWeightValue.ts +0 -14
- package/src/schemas/numberToken.ts +0 -28
- package/src/schemas/referenceValue.ts +0 -12
- package/src/schemas/scopes.ts +0 -40
- package/src/schemas/shadowToken.ts +0 -45
- package/src/schemas/stringToken.ts +0 -11
- package/src/schemas/tokenName.ts +0 -12
- package/src/schemas/tokenType.ts +0 -4
- package/src/schemas/typographyToken.ts +0 -20
- package/src/schemas/validTokenType.ts +0 -44
- package/src/schemas/viewportRangeToken.ts +0 -11
- package/src/test-utilities/getMockDictionary.ts +0 -22
- package/src/test-utilities/getMockFormatterArguments.ts +0 -21
- package/src/test-utilities/getMockParserInput.ts +0 -72
- package/src/test-utilities/getMockToken.ts +0 -25
- package/src/test-utilities/index.ts +0 -4
- package/src/transformers/borderToCss.ts +0 -38
- package/src/transformers/colorToHex.ts +0 -16
- package/src/transformers/colorToHexAlpha.ts +0 -23
- package/src/transformers/colorToHexMix.ts +0 -19
- package/src/transformers/colorToRgbAlpha.ts +0 -19
- package/src/transformers/colorToRgbaFloat.ts +0 -45
- package/src/transformers/dimensionToPixelUnitless.ts +0 -60
- package/src/transformers/dimensionToRem.ts +0 -56
- package/src/transformers/dimensionToRemPxArray.ts +0 -60
- package/src/transformers/durationToCss.ts +0 -29
- package/src/transformers/figmaAttributes.ts +0 -79
- package/src/transformers/floatToPixel.ts +0 -40
- package/src/transformers/fontFamilyToCss.ts +0 -37
- package/src/transformers/fontFamilyToFigma.ts +0 -47
- package/src/transformers/fontWeightToNumber.ts +0 -53
- package/src/transformers/index.ts +0 -24
- package/src/transformers/jsonDeprecated.ts +0 -15
- package/src/transformers/namePathToDotNotation.ts +0 -34
- package/src/transformers/namePathToFigma.ts +0 -28
- package/src/transformers/namePathToKebabCase.ts +0 -19
- package/src/transformers/namePathToSlashNotation.ts +0 -19
- package/src/transformers/shadowToCss.ts +0 -37
- package/src/transformers/typographyToCss.ts +0 -31
- package/src/transformers/utilities/alpha.ts +0 -23
- package/src/transformers/utilities/checkRequiredTokenProperties.ts +0 -16
- package/src/transformers/utilities/getTokenValue.ts +0 -18
- package/src/transformers/utilities/hasSpaceInString.ts +0 -6
- package/src/transformers/utilities/hexToRgbaFloat.ts +0 -31
- package/src/transformers/utilities/invalidTokenError.ts +0 -34
- package/src/transformers/utilities/isRgbaFloat.ts +0 -29
- package/src/transformers/utilities/mix.ts +0 -46
- package/src/transformers/utilities/rgbaFloatToHex.ts +0 -15
- package/src/types/Border.d.ts +0 -5
- package/src/types/BorderTokenValue.d.ts +0 -10
- package/src/types/ColorHex.d.ts +0 -4
- package/src/types/PlatformInitializer.d.ts +0 -11
- package/src/types/Shadow.d.ts +0 -5
- package/src/types/ShadowTokenValue.d.ts +0 -14
- package/src/types/SizeEm.d.ts +0 -4
- package/src/types/SizePx.d.ts +0 -4
- package/src/types/SizeRem.d.ts +0 -4
- package/src/types/StyleDictionaryConfigGenerator.d.ts +0 -15
- package/src/types/TokenBuildInput.d.ts +0 -8
- package/src/types/TypographyTokenValue.d.ts +0 -12
- package/src/types/w3cTransformedToken.d.ts +0 -8
- package/src/utilities/copyFromDir.ts +0 -21
- package/src/utilities/index.ts +0 -3
- package/src/utilities/joinFriendly.ts +0 -2
- package/src/utilities/schemaErrorMessage.ts +0 -2
- package/src/utilities/toCamelCase.ts +0 -29
- package/src/utilities/treeWalker.ts +0 -27
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type {TransformedToken} from 'style-dictionary'
|
|
2
|
-
import StyleDictionary from 'style-dictionary'
|
|
3
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
4
|
-
import syncPrettier from '@prettier/sync'
|
|
5
|
-
import type {LineFormatting} from 'style-dictionary/types/FormatHelpers'
|
|
6
|
-
const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
|
|
7
|
-
|
|
8
|
-
const wrapWithSelector = (css: string, selector: string | false): string => {
|
|
9
|
-
// return without selector
|
|
10
|
-
if (selector === false || selector.trim().length === 0) return css
|
|
11
|
-
// return with selector
|
|
12
|
-
return `${selector} { ${css} }`
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const cssAdvanced: StyleDictionary.Formatter = ({
|
|
16
|
-
dictionary: originalDictionary,
|
|
17
|
-
options = {
|
|
18
|
-
queries: [],
|
|
19
|
-
},
|
|
20
|
-
file,
|
|
21
|
-
platform,
|
|
22
|
-
}: FormatterArguments): string => {
|
|
23
|
-
// get options
|
|
24
|
-
const {outputReferences, descriptions} = options
|
|
25
|
-
// selector
|
|
26
|
-
const selector = file.options?.selector !== undefined ? file.options.selector : ':root'
|
|
27
|
-
// query extension property
|
|
28
|
-
const queryExtProp = file.options?.queryExtensionProperty || 'mediaQuery'
|
|
29
|
-
// get queries from file options
|
|
30
|
-
const queries = file.options?.queries || [
|
|
31
|
-
{
|
|
32
|
-
query: undefined,
|
|
33
|
-
matcher: () => true,
|
|
34
|
-
},
|
|
35
|
-
]
|
|
36
|
-
// set formatting
|
|
37
|
-
const formatting: LineFormatting = {
|
|
38
|
-
commentStyle: descriptions ? 'long' : 'none',
|
|
39
|
-
}
|
|
40
|
-
// clone dictionary
|
|
41
|
-
const dictionary = {...originalDictionary}
|
|
42
|
-
// add prefix to tokens
|
|
43
|
-
if (platform.prefix) {
|
|
44
|
-
dictionary.allTokens = dictionary.allTokens.map(
|
|
45
|
-
token =>
|
|
46
|
-
({
|
|
47
|
-
...token,
|
|
48
|
-
name: `${platform.prefix}-${token.name}`,
|
|
49
|
-
path: [platform.prefix, ...token.path],
|
|
50
|
-
}) as TransformedToken,
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
// get queries from tokens
|
|
54
|
-
for (const designToken of dictionary.allTokens) {
|
|
55
|
-
const query = designToken.$extensions?.[queryExtProp]
|
|
56
|
-
// early abort if query does not exist on token
|
|
57
|
-
if (!query) continue
|
|
58
|
-
// if query exists already from other token
|
|
59
|
-
const currentQueryIndex = queries.findIndex((q: {query: string; matcher: () => boolean}) => q.query === query)
|
|
60
|
-
|
|
61
|
-
// if query exists
|
|
62
|
-
if (currentQueryIndex > -1) {
|
|
63
|
-
queries[currentQueryIndex] = {
|
|
64
|
-
...queries[currentQueryIndex],
|
|
65
|
-
matcher: (token: TransformedToken) =>
|
|
66
|
-
queries[currentQueryIndex].matcher(token) ||
|
|
67
|
-
token.$extensions[queryExtProp] === queries[currentQueryIndex].query,
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// if query does not exist
|
|
71
|
-
else {
|
|
72
|
-
queries.push({
|
|
73
|
-
query,
|
|
74
|
-
matcher: (token: TransformedToken) => token.$extensions?.[queryExtProp] === query,
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
// add file header
|
|
79
|
-
const output = [fileHeader({file})]
|
|
80
|
-
// add single theme css
|
|
81
|
-
for (const query of queries) {
|
|
82
|
-
const {query: queryString, matcher} = query
|
|
83
|
-
// filter tokens to only include the ones that pass the matcher
|
|
84
|
-
const filteredDictionary = {
|
|
85
|
-
...dictionary,
|
|
86
|
-
allTokens: dictionary.allTokens.filter(matcher || (() => true)),
|
|
87
|
-
}
|
|
88
|
-
// early abort if no matches
|
|
89
|
-
if (!filteredDictionary.allTokens.length) continue
|
|
90
|
-
// add tokens into root
|
|
91
|
-
const css = formattedVariables({format: 'css', dictionary: filteredDictionary, outputReferences, formatting})
|
|
92
|
-
// wrap css
|
|
93
|
-
const cssWithSelector = wrapWithSelector(css, query.selector !== undefined ? query.selector : selector)
|
|
94
|
-
// add css with or without query
|
|
95
|
-
output.push(queryString ? `${queryString} { ${cssWithSelector} }` : cssWithSelector)
|
|
96
|
-
}
|
|
97
|
-
// return prettified
|
|
98
|
-
return syncPrettier.format(output.join('\n'), {parser: 'css', printWidth: 500})
|
|
99
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import StyleDictionary from 'style-dictionary'
|
|
2
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
3
|
-
import syncPrettier from '@prettier/sync'
|
|
4
|
-
const {fileHeader} = StyleDictionary.formatHelpers
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @description Converts `StyleDictionary.dictionary.tokens` to css with @custom-media
|
|
8
|
-
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
|
|
9
|
-
* @returns formatted `string`
|
|
10
|
-
*/
|
|
11
|
-
export const cssCustomMedia: StyleDictionary.Formatter = ({
|
|
12
|
-
dictionary,
|
|
13
|
-
options: _options,
|
|
14
|
-
file,
|
|
15
|
-
}: FormatterArguments) => {
|
|
16
|
-
// add file header
|
|
17
|
-
const output = [fileHeader({file})]
|
|
18
|
-
// add single theme css
|
|
19
|
-
dictionary.allTokens.map(({name, value}) => {
|
|
20
|
-
output.push(`@custom-media --${name} ${value};`)
|
|
21
|
-
})
|
|
22
|
-
// return prettified
|
|
23
|
-
return syncPrettier.format(output.join('\n'), {parser: 'css', printWidth: 500})
|
|
24
|
-
}
|
package/src/formats/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export {cssCustomMedia} from './cssCustomMedia'
|
|
2
|
-
export {cssAdvanced} from './cssAdvanced'
|
|
3
|
-
export {jsonFigma} from './jsonFigma'
|
|
4
|
-
export {javascriptCommonJs} from './javascriptCommonJs'
|
|
5
|
-
export {javascriptEsm} from './javascriptEsm'
|
|
6
|
-
export {jsonNestedPrefixed} from './jsonNestedPrefixed'
|
|
7
|
-
export {jsonOneDimensional} from './jsonOneDimensional'
|
|
8
|
-
export {jsonPostCssFallback} from './jsonPostCssFallback'
|
|
9
|
-
export {typescriptExportDefinition} from './typescriptExportDefinition'
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import syncPrettier from '@prettier/sync'
|
|
2
|
-
import {jsonToNestedValue} from './utilities/jsonToNestedValue'
|
|
3
|
-
import {prefixTokens} from './utilities/prefixTokens'
|
|
4
|
-
import StyleDictionary from 'style-dictionary'
|
|
5
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
6
|
-
|
|
7
|
-
const {fileHeader} = StyleDictionary.formatHelpers
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @description Converts `StyleDictionary.dictionary.tokens` to javascript commonJS module
|
|
11
|
-
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
|
|
12
|
-
* @returns formatted `string`
|
|
13
|
-
*/
|
|
14
|
-
export const javascriptCommonJs: StyleDictionary.Formatter = ({dictionary, file, platform}: FormatterArguments) => {
|
|
15
|
-
// add prefix if defined
|
|
16
|
-
const tokens = prefixTokens(dictionary.tokens, platform)
|
|
17
|
-
// add file header and convert output
|
|
18
|
-
const output = `${fileHeader({file})}exports.default = ${JSON.stringify(jsonToNestedValue(tokens), null, 2)}\n`
|
|
19
|
-
// return prettified
|
|
20
|
-
return syncPrettier.format(output, {parser: 'typescript', printWidth: 500})
|
|
21
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
import {jsonToNestedValue} from './utilities/jsonToNestedValue'
|
|
4
|
-
import {prefixTokens} from './utilities/prefixTokens'
|
|
5
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
6
|
-
|
|
7
|
-
const {fileHeader} = StyleDictionary.formatHelpers
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @description Converts `StyleDictionary.dictionary.tokens` to javascript esm (javascript export statement)
|
|
11
|
-
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
|
|
12
|
-
* @returns formatted `string`
|
|
13
|
-
*/
|
|
14
|
-
export const javascriptEsm: StyleDictionary.Formatter = ({dictionary, file, platform}: FormatterArguments) => {
|
|
15
|
-
// add prefix if defined
|
|
16
|
-
const tokens = prefixTokens(dictionary.tokens, platform)
|
|
17
|
-
// add file header and convert output
|
|
18
|
-
const output = `${fileHeader({file})}export default \n${JSON.stringify(jsonToNestedValue(tokens), null, 2)}\n`
|
|
19
|
-
// return prettified
|
|
20
|
-
return syncPrettier.format(output, {parser: 'typescript', printWidth: 500})
|
|
21
|
-
}
|
package/src/formats/jsonFigma.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
4
|
-
import {transformNamePathToFigma} from '../transformers/namePathToFigma'
|
|
5
|
-
import type {ShadowTokenValue} from '../types/ShadowTokenValue'
|
|
6
|
-
import {hexToRgbaFloat} from '../transformers/utilities/hexToRgbaFloat'
|
|
7
|
-
import type {RgbaFloat} from '../transformers/utilities/isRgbaFloat'
|
|
8
|
-
import {isRgbaFloat} from '../transformers/utilities/isRgbaFloat'
|
|
9
|
-
const {sortByReference} = StyleDictionary.formatHelpers
|
|
10
|
-
|
|
11
|
-
const isReference = (string: string): boolean => /^\{([^\\]*)\}$/g.test(string)
|
|
12
|
-
|
|
13
|
-
const getReference = (
|
|
14
|
-
dictionary: StyleDictionary.Dictionary,
|
|
15
|
-
refString: string,
|
|
16
|
-
platform: StyleDictionary.Platform,
|
|
17
|
-
) => {
|
|
18
|
-
if (isReference(refString)) {
|
|
19
|
-
// retrieve reference token
|
|
20
|
-
const refToken = dictionary.getReferences(refString)[0]
|
|
21
|
-
// return full reference
|
|
22
|
-
return [refToken.attributes?.collection, transformNamePathToFigma(refToken, platform)].filter(Boolean).join('/')
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const getFigmaType = (type: string): string => {
|
|
27
|
-
const validTypes = {
|
|
28
|
-
color: 'COLOR',
|
|
29
|
-
dimension: 'FLOAT',
|
|
30
|
-
fontWeight: 'FLOAT',
|
|
31
|
-
number: 'FLOAT',
|
|
32
|
-
fontFamily: 'STRING',
|
|
33
|
-
}
|
|
34
|
-
if (type in validTypes) return validTypes[type as keyof typeof validTypes]
|
|
35
|
-
throw new Error(`Invalid type: ${type}`)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const shadowToVariables = (
|
|
39
|
-
name: string,
|
|
40
|
-
values: Omit<ShadowTokenValue, 'color'> & {color: string | RgbaFloat},
|
|
41
|
-
token: StyleDictionary.TransformedToken,
|
|
42
|
-
) => {
|
|
43
|
-
// floatValue
|
|
44
|
-
const floatValue = (property: 'offsetX' | 'offsetY' | 'blur' | 'spread') => ({
|
|
45
|
-
name: `${name}/${property}`,
|
|
46
|
-
value: parseInt(values[property].replace('px', '')),
|
|
47
|
-
type: 'FLOAT',
|
|
48
|
-
scopes: ['EFFECT_FLOAT'],
|
|
49
|
-
mode,
|
|
50
|
-
collection,
|
|
51
|
-
group,
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const {attributes} = token
|
|
55
|
-
const {mode, collection, group} = attributes || {}
|
|
56
|
-
|
|
57
|
-
return [
|
|
58
|
-
floatValue('offsetX'),
|
|
59
|
-
floatValue('offsetY'),
|
|
60
|
-
floatValue('blur'),
|
|
61
|
-
floatValue('spread'),
|
|
62
|
-
{
|
|
63
|
-
name: `${name}/color`,
|
|
64
|
-
value: isRgbaFloat(values.color)
|
|
65
|
-
? {...values.color, ...(values.alpha ? {a: values.alpha} : {})}
|
|
66
|
-
: hexToRgbaFloat(values.color, values.alpha),
|
|
67
|
-
type: 'COLOR',
|
|
68
|
-
scopes: ['EFFECT_COLOR'],
|
|
69
|
-
mode,
|
|
70
|
-
collection,
|
|
71
|
-
group,
|
|
72
|
-
},
|
|
73
|
-
]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @description Converts `StyleDictionary.dictionary.tokens` to javascript esm (javascript export statement)
|
|
78
|
-
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
|
|
79
|
-
* @returns formatted `string`
|
|
80
|
-
*/
|
|
81
|
-
export const jsonFigma: StyleDictionary.Formatter = ({dictionary, file: _file, platform}: FormatterArguments) => {
|
|
82
|
-
// array to store tokens in
|
|
83
|
-
const tokens: Record<string, unknown>[] = []
|
|
84
|
-
// loop through tokens sorted by reference
|
|
85
|
-
for (const token of dictionary.allTokens.sort(sortByReference(dictionary))) {
|
|
86
|
-
// deconstruct token
|
|
87
|
-
const {attributes, value, $type, comment: description, original, alpha, mix} = token
|
|
88
|
-
const {mode, collection, scopes, group, codeSyntax} = attributes || {}
|
|
89
|
-
// shadows need to be specifically dealt with
|
|
90
|
-
if ($type === 'shadow') {
|
|
91
|
-
const shadowValues = !Array.isArray(value) ? [value] : value
|
|
92
|
-
// if only one set of shadow values is present
|
|
93
|
-
if (shadowValues.length === 1) {
|
|
94
|
-
tokens.push(
|
|
95
|
-
...shadowToVariables(token.name, shadowValues[0], {
|
|
96
|
-
...token,
|
|
97
|
-
...(platform.mode ? {mode: platform.mode} : {}),
|
|
98
|
-
}),
|
|
99
|
-
)
|
|
100
|
-
} else {
|
|
101
|
-
// if multiple shadow sets values are present we need loop through them and add the index to the name
|
|
102
|
-
for (const [index, stepValue] of shadowValues.entries()) {
|
|
103
|
-
tokens.push(
|
|
104
|
-
...shadowToVariables(`${token.name}/${index + 1}`, stepValue, {
|
|
105
|
-
...token,
|
|
106
|
-
...(platform.mode ? {mode: platform.mode} : {}),
|
|
107
|
-
}),
|
|
108
|
-
)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// other tokens just get added to tokens array
|
|
112
|
-
} else {
|
|
113
|
-
tokens.push({
|
|
114
|
-
name: token.name,
|
|
115
|
-
value,
|
|
116
|
-
type: getFigmaType($type),
|
|
117
|
-
alpha,
|
|
118
|
-
isMix: mix ? true : undefined,
|
|
119
|
-
description,
|
|
120
|
-
refId: [collection, token.name].filter(Boolean).join('/'),
|
|
121
|
-
reference: getReference(dictionary, original.value, platform),
|
|
122
|
-
collection,
|
|
123
|
-
mode,
|
|
124
|
-
group,
|
|
125
|
-
scopes,
|
|
126
|
-
codeSyntax,
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
// add file header and convert output
|
|
131
|
-
const output = JSON.stringify(tokens, null, 2)
|
|
132
|
-
// return prettified
|
|
133
|
-
return syncPrettier.format(output, {parser: 'json', printWidth: 500})
|
|
134
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
import {prefixTokens} from './utilities/prefixTokens'
|
|
4
|
-
import {jsonToNestedValue} from './utilities/jsonToNestedValue'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @description Takes a style dictionary token dictionary and converts it to a nested json string.
|
|
8
|
-
* In contrast to the `json/nested` this formatter does add a prefix if provided
|
|
9
|
-
* @param StyleDictionary.FormatterArguments
|
|
10
|
-
* @returns formatted json `string`
|
|
11
|
-
*/
|
|
12
|
-
export const jsonNestedPrefixed: StyleDictionary.Formatter = ({dictionary, file: _file, options, platform}) => {
|
|
13
|
-
const {outputVerbose} = options
|
|
14
|
-
// add prefix if defined
|
|
15
|
-
let tokens = prefixTokens(dictionary.tokens, platform)
|
|
16
|
-
if (!outputVerbose) {
|
|
17
|
-
tokens = jsonToNestedValue(tokens)
|
|
18
|
-
}
|
|
19
|
-
// add file header and convert output
|
|
20
|
-
const output = JSON.stringify(tokens, null, 2)
|
|
21
|
-
// return prettified
|
|
22
|
-
return syncPrettier.format(output, {parser: 'json', printWidth: 500})
|
|
23
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
import {jsonToFlat} from './utilities/jsonToFlat'
|
|
4
|
-
/**
|
|
5
|
-
* @description Takes a style dictionary token dictionary and converts it to a one dimensional json object.
|
|
6
|
-
* @param StyleDictionary.FormatterArguments
|
|
7
|
-
* @param options.nameSeparator - separator to use for nested token name
|
|
8
|
-
* @returns formatted json `string`
|
|
9
|
-
*/
|
|
10
|
-
export const jsonOneDimensional: StyleDictionary.Formatter = ({dictionary, file: _file, options}) => {
|
|
11
|
-
// option to allow verbose output (object) or just the value
|
|
12
|
-
const {outputVerbose} = options
|
|
13
|
-
const tokens = jsonToFlat(dictionary.allTokens, outputVerbose)
|
|
14
|
-
// add file header and convert output
|
|
15
|
-
const output = JSON.stringify(tokens, null, 2)
|
|
16
|
-
// return prettified
|
|
17
|
-
return syncPrettier.format(output, {parser: 'json', printWidth: 500})
|
|
18
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @description Takes a style dictionary token dictionary and converts it to a one dimensional json object.
|
|
6
|
-
* @param StyleDictionary.FormatterArguments
|
|
7
|
-
* @param options.nameSeparator - separator to use for nested token name
|
|
8
|
-
* @returns formatted json `string`
|
|
9
|
-
*/
|
|
10
|
-
export const jsonPostCssFallback: StyleDictionary.Formatter = ({dictionary, file: _file}) => {
|
|
11
|
-
const tokens = Object.fromEntries(dictionary.allTokens.map(token => [`--${token.name}`, token.value]))
|
|
12
|
-
// add file header and convert output
|
|
13
|
-
const output = JSON.stringify(tokens, null, 2)
|
|
14
|
-
// return prettified
|
|
15
|
-
return syncPrettier.format(output, {parser: 'json', printWidth: 500})
|
|
16
|
-
}
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import StyleDictionary from 'style-dictionary'
|
|
2
|
-
import syncPrettier from '@prettier/sync'
|
|
3
|
-
import fs = require('fs')
|
|
4
|
-
import path = require('path')
|
|
5
|
-
import {treeWalker} from '../utilities/treeWalker'
|
|
6
|
-
|
|
7
|
-
import type {FormatterArguments} from 'style-dictionary/types/Format'
|
|
8
|
-
import type {w3cTransformedToken} from '../types/w3cTransformedToken'
|
|
9
|
-
import {prefixTokens} from './utilities/prefixTokens'
|
|
10
|
-
|
|
11
|
-
const {fileHeader} = StyleDictionary.formatHelpers
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* unquoteTypes
|
|
15
|
-
* @description extract types from designTokenTypes (string with type definitions from file) and replace quoted types "Color" with unquoted Color
|
|
16
|
-
* @param output
|
|
17
|
-
* @param designTokenTypes
|
|
18
|
-
* @returns string with unquoted types
|
|
19
|
-
*/
|
|
20
|
-
const unquoteTypes = (output: string, designTokenTypes: string[]): string => {
|
|
21
|
-
// join types for replacement
|
|
22
|
-
const regex = `"(${['number', 'string', 'any', ...designTokenTypes].join('|')})"`
|
|
23
|
-
// remove strings
|
|
24
|
-
return output.replace(new RegExp(regex, 'g'), '$1')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* getTokenType
|
|
29
|
-
* @description extract content from token files
|
|
30
|
-
* @param path string
|
|
31
|
-
*/
|
|
32
|
-
const getTokenType = (tokenTypesPath: string): string => {
|
|
33
|
-
try {
|
|
34
|
-
const designTokenType = fs.readFileSync(path.resolve(tokenTypesPath), {encoding: 'utf-8'})
|
|
35
|
-
return designTokenType
|
|
36
|
-
} catch (e) {
|
|
37
|
-
// eslint-disable-next-line no-console
|
|
38
|
-
throw new Error(`Error trying to load design token type from file "${tokenTypesPath}".`)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @description throws an error stating the token name, type and invalid value
|
|
44
|
-
* @param token w3cTransformedToken
|
|
45
|
-
*/
|
|
46
|
-
const invalidTokenValueError = (token: w3cTransformedToken) => {
|
|
47
|
-
throw new Error(`Invalid token: "${token.name}" with type "${token.$type}" can not have a value of "${token.value}"`)
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* convertPropToType
|
|
51
|
-
* @description return take type attribute and return correct typescript type as string
|
|
52
|
-
* @param type
|
|
53
|
-
* @returns
|
|
54
|
-
*/
|
|
55
|
-
const convertPropToType = (tree: w3cTransformedToken): string => {
|
|
56
|
-
if (!Object.prototype.hasOwnProperty.call(tree, 'value')) {
|
|
57
|
-
throw new Error(`Invalid token: ${tree}`)
|
|
58
|
-
}
|
|
59
|
-
switch (tree.$type) {
|
|
60
|
-
case 'color':
|
|
61
|
-
if (typeof tree.value === 'string' && tree.value[0] === '#') {
|
|
62
|
-
return 'ColorHex'
|
|
63
|
-
}
|
|
64
|
-
return invalidTokenValueError(tree)
|
|
65
|
-
case 'dimension':
|
|
66
|
-
if (typeof tree.value === 'string' && tree.value.substring(tree.value.length - 3) === 'rem') return 'SizeRem'
|
|
67
|
-
if (typeof tree.value === 'string' && tree.value.substring(tree.value.length - 2) === 'em') return 'SizeEm'
|
|
68
|
-
if (typeof tree.value === 'string' && tree.value.substring(tree.value.length - 2) === 'px') return 'SizePx'
|
|
69
|
-
return invalidTokenValueError(tree)
|
|
70
|
-
case 'shadow':
|
|
71
|
-
return 'Shadow'
|
|
72
|
-
case 'border':
|
|
73
|
-
return 'Border'
|
|
74
|
-
default:
|
|
75
|
-
if (typeof tree.value === 'number') return 'number'
|
|
76
|
-
if (typeof tree.value === 'string') return 'string'
|
|
77
|
-
if (typeof tree.value === 'boolean') return 'boolean'
|
|
78
|
-
return 'any'
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* a valid token node has a `value` property
|
|
83
|
-
* @param item object
|
|
84
|
-
* @returns boolean
|
|
85
|
-
*/
|
|
86
|
-
const validTokenNode = (item: w3cTransformedToken | unknown): item is w3cTransformedToken => {
|
|
87
|
-
return typeof item === 'object' && item !== null && 'value' in item
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* returns a set with every used token type
|
|
91
|
-
* @param item object
|
|
92
|
-
* @param validTypes array, all other types will be removed
|
|
93
|
-
* @returns set of all used token types
|
|
94
|
-
*/
|
|
95
|
-
const getUsedTokenTypes = (tokens: StyleDictionary.DesignTokens, validTypes: string[]): Set<string> => {
|
|
96
|
-
// using a set to assure every value only exists once
|
|
97
|
-
const usedTypes = new Set<string>()
|
|
98
|
-
// adds type to set
|
|
99
|
-
const callback = (tree: unknown) => usedTypes.add(convertPropToType(tree as w3cTransformedToken))
|
|
100
|
-
// tree walker adds to usedTypes
|
|
101
|
-
treeWalker(tokens, callback, validTokenNode)
|
|
102
|
-
// clean up types
|
|
103
|
-
for (const type of usedTypes) {
|
|
104
|
-
if (!validTypes.includes(type)) {
|
|
105
|
-
usedTypes.delete(type)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return usedTypes
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* returns the entire token object structure but instead of token values each token just has a typescript type
|
|
112
|
-
* @param item object
|
|
113
|
-
* @returns object
|
|
114
|
-
*/
|
|
115
|
-
const getTokenObjectWithTypes = (tokens: StyleDictionary.DesignTokens): Record<string, unknown> => {
|
|
116
|
-
// TODO: FIX typescript issues
|
|
117
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
118
|
-
// @ts-ignore
|
|
119
|
-
return treeWalker(tokens, convertPropToType, validTokenNode) as Record<string, unknown>
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* getTypeDefinition
|
|
123
|
-
* @description returns type definitions as string
|
|
124
|
-
* @param tokens
|
|
125
|
-
* @param moduleName
|
|
126
|
-
* @param tokenTypesPath
|
|
127
|
-
* @returns string
|
|
128
|
-
*/
|
|
129
|
-
const getTypeDefinition = (
|
|
130
|
-
tokens: StyleDictionary.DesignTokens,
|
|
131
|
-
moduleName: string,
|
|
132
|
-
tokenTypesPath: string,
|
|
133
|
-
): string => {
|
|
134
|
-
const usedTypes = getUsedTokenTypes(tokens, ['ColorHex', 'Shadow', 'Border', 'SizeEm', 'SizeRem', 'SizePx'])
|
|
135
|
-
const tokenObjectWithTypes = getTokenObjectWithTypes(tokens)
|
|
136
|
-
// get token type declaration from file
|
|
137
|
-
const designTokenTypes: string[] = []
|
|
138
|
-
for (const type of usedTypes) {
|
|
139
|
-
// path to type files without trailing slash
|
|
140
|
-
const typePath = tokenTypesPath.replace(new RegExp(/\/$/, 'g'), '')
|
|
141
|
-
|
|
142
|
-
designTokenTypes.push(getTokenType(`${typePath}/${type}.d.ts`))
|
|
143
|
-
}
|
|
144
|
-
// build output
|
|
145
|
-
const output = `${designTokenTypes.join('\n')}
|
|
146
|
-
export type ${moduleName} = ${JSON.stringify(tokenObjectWithTypes, null, 2)}`
|
|
147
|
-
|
|
148
|
-
// JSON stringify will quote strings, because this is a type we need it unquoted.
|
|
149
|
-
return unquoteTypes(output, [...usedTypes])
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* @description Converts `StyleDictionary.dictionary.tokens` to typescript definition
|
|
153
|
-
* @param arguments [FormatterArguments](https://github.com/amzn/style-dictionary/blob/main/types/Format.d.ts)
|
|
154
|
-
* @returns formatted `string`
|
|
155
|
-
*/
|
|
156
|
-
export const typescriptExportDefinition: StyleDictionary.Formatter = ({
|
|
157
|
-
dictionary,
|
|
158
|
-
file,
|
|
159
|
-
options = {},
|
|
160
|
-
platform,
|
|
161
|
-
}: FormatterArguments): string => {
|
|
162
|
-
// extract options
|
|
163
|
-
const {moduleName = `tokens`, tokenTypesPath = `./src/types/`} = options
|
|
164
|
-
// add prefix if defined
|
|
165
|
-
const tokens = prefixTokens(dictionary.tokens, platform)
|
|
166
|
-
// add file header and convert output
|
|
167
|
-
const output = `${fileHeader({file})}\n${getTypeDefinition(tokens, moduleName, tokenTypesPath)}\n`
|
|
168
|
-
// return prettified
|
|
169
|
-
return syncPrettier.format(output, {parser: 'typescript', printWidth: 500})
|
|
170
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type StyleDictionary from 'style-dictionary'
|
|
2
|
-
/**
|
|
3
|
-
* jsonToNestedValue
|
|
4
|
-
* @description creates a nested json tree where every final value is the `.value` prop
|
|
5
|
-
* @param token StyleDictionary.DesignToken
|
|
6
|
-
* @returns nested json three
|
|
7
|
-
*/
|
|
8
|
-
export const jsonToNestedValue = (token: StyleDictionary.DesignToken | Record<string, unknown>) => {
|
|
9
|
-
// is non-object value
|
|
10
|
-
if (typeof token !== 'object') return token
|
|
11
|
-
// is design token
|
|
12
|
-
if ('value' in token) return token.value
|
|
13
|
-
// is obj
|
|
14
|
-
const nextObj = {}
|
|
15
|
-
for (const [prop, value] of Object.entries(token)) {
|
|
16
|
-
// @ts-expect-error: can't predict type
|
|
17
|
-
nextObj[prop] = jsonToNestedValue(value)
|
|
18
|
-
}
|
|
19
|
-
return nextObj
|
|
20
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type StyleDictionary from 'style-dictionary'
|
|
2
|
-
/**
|
|
3
|
-
* @description extract prefix from platform and add to tokens array if defined
|
|
4
|
-
* @param tokens StyleDictionary.TransformedTokens
|
|
5
|
-
* @param platform StyleDictionary.Platform
|
|
6
|
-
* @return StyleDictionary.TransformedTokens
|
|
7
|
-
*/
|
|
8
|
-
export const prefixTokens = (
|
|
9
|
-
tokens: StyleDictionary.TransformedTokens,
|
|
10
|
-
platform: StyleDictionary.Platform = {},
|
|
11
|
-
): StyleDictionary.TransformedTokens => {
|
|
12
|
-
const {prefix} = platform
|
|
13
|
-
if (typeof prefix === 'string') {
|
|
14
|
-
tokens = {[prefix]: tokens}
|
|
15
|
-
}
|
|
16
|
-
return tokens
|
|
17
|
-
}
|
package/src/parsers/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {w3cJsonParser} from './w3cJsonParser'
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {parse as json5Parse} from 'json5'
|
|
2
|
-
import type StyleDictionary from 'style-dictionary'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @description Parses a valid [json5](https://json5.org) file and replaces `$value` with `value` and `$description` with `comment` to make a w3c standard file compatible with style dictionary
|
|
6
|
-
* @pattern supported file extensions `.json` or `.json5`
|
|
7
|
-
*/
|
|
8
|
-
export const w3cJsonParser: StyleDictionary.Parser = {
|
|
9
|
-
pattern: /\.json5?$/,
|
|
10
|
-
parse: ({filePath, contents}: {filePath: string; contents: string}) => {
|
|
11
|
-
// replace $value with value so that style dictionary recognizes it
|
|
12
|
-
try {
|
|
13
|
-
contents = contents
|
|
14
|
-
.replace(/["|']?\$value["|']?:/g, '"value":')
|
|
15
|
-
// convert $description to comment
|
|
16
|
-
.replace(/["|']?\$?description["|']?:/g, '"comment":')
|
|
17
|
-
.replace(/["|']?\$?type["|']?:/g, '"$type":')
|
|
18
|
-
.replace(/["|']?\$?extensions["|']?:/g, '"$extensions":')
|
|
19
|
-
//
|
|
20
|
-
return json5Parse(contents)
|
|
21
|
-
} catch (e) {
|
|
22
|
-
throw new Error(`Invalid json5 file "${filePath}".`)
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
}
|