-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as R from 'ramda'; | ||
import type { TransformedToken, Format } from 'style-dictionary/types'; | ||
import { fileHeader, createPropertyFormatter } from 'style-dictionary/utils'; | ||
|
||
const groupByType = R.groupBy((token: TransformedToken) => token.type as string); | ||
|
||
/** Add token name with prefix to list for removal */ | ||
const removeUnwatedTokens = R.filter( | ||
(token: TransformedToken) => !['fds-base_spacing', 'fds-base_sizing'].includes(token.name), | ||
); | ||
|
||
const toCssVarName = R.pipe(R.split(':'), R.head, R.trim); | ||
|
||
/** | ||
* Format for displaying tokens in storefront | ||
*/ | ||
export const groupedTokens: Format = { | ||
name: 'groupedTokens', | ||
format: async function ({ dictionary, file }) { | ||
const format = createPropertyFormatter({ | ||
dictionary, | ||
format: 'css', | ||
}); | ||
|
||
const formatTokens = R.map((token: TransformedToken) => ({ | ||
...token, | ||
name: toCssVarName(format(token)), | ||
})); | ||
|
||
const processTokens = R.pipe(removeUnwatedTokens, formatTokens, groupByType); | ||
|
||
const tokens = processTokens(dictionary.allTokens); | ||
|
||
const content = Object.entries(tokens) | ||
.map( | ||
([name, token]) => `export const ${name} = ${JSON.stringify(token, null, 2).replace(/"([^"]+)":/g, '$1:')} \n`, | ||
) | ||
.join('\n'); | ||
|
||
return fileHeader({ file }).then((fileHeaderText) => fileHeaderText + content); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters