|
1 | | -import { dsfs } from '../../utils/filesystem.js'; |
2 | | -import type { OutputFile, SizeModes, Theme, TokenSets } from '../types.js'; |
| 1 | +import type { ColorNamesByCategory, OutputFile, TokenSetDimensions, TokenSets } from '../types.js'; |
3 | 2 | import { generate$Designsystemet } from './generators/$designsystemet.js'; |
4 | 3 | import { generate$Metadata } from './generators/$metadata.js'; |
5 | 4 | import { generate$Themes } from './generators/$themes.js'; |
6 | 5 |
|
7 | 6 | export const stringify = (data: unknown) => JSON.stringify(data, null, 2); |
8 | 7 |
|
9 | 8 | type CreateTokenFilesOptions = { |
10 | | - outDir: string; |
11 | | - theme: Theme; |
12 | | - tokenSets: TokenSets; |
| 9 | + tokenSetDimensions: TokenSetDimensions; |
| 10 | + colors: ColorNamesByCategory; |
13 | 11 | themeNames: string[]; |
14 | 12 | }; |
15 | 13 |
|
16 | | -export const createTokenFiles = async (options: CreateTokenFilesOptions) => { |
17 | | - const { |
18 | | - outDir, |
19 | | - tokenSets, |
20 | | - theme: { colors }, |
21 | | - themeNames, |
22 | | - } = options; |
| 14 | +/** |
| 15 | + * Creates system token files (`$themes.json`, `$metadata.json`, `$designsystemet.jsonc`) based on the provided token set dimensions and theme names. |
| 16 | + * |
| 17 | + * `$themes.json` and `$metadata.json` are essential for Token Studio and Style Dictionary to correctly interpret and manage the design tokens. |
| 18 | + */ |
| 19 | +export const createSystemTokenFiles = async (options: CreateTokenFilesOptions) => { |
| 20 | + const { colors, themeNames, tokenSetDimensions } = options; |
23 | 21 |
|
| 22 | + const files: OutputFile[] = []; |
24 | 23 | const $themesPath = '$themes.json'; |
25 | 24 | const $metadataPath = '$metadata.json'; |
26 | 25 | const $designsystemetPath = '$designsystemet.jsonc'; |
27 | | - // let themeObjects: ThemeObject[] = []; |
28 | | - const sizeModes: SizeModes[] = ['small', 'medium', 'large']; |
29 | | - |
30 | | - await dsfs.mkdir(outDir); |
31 | 26 |
|
32 | | - // Create metadata and themes json for Token Studio and build script |
33 | | - const $themes = await generate$Themes(['dark', 'light'], themeNames, colors, sizeModes); |
34 | | - const $metadata = generate$Metadata(['dark', 'light'], themeNames, colors, sizeModes); |
| 27 | + const $themes = await generate$Themes(tokenSetDimensions, themeNames, colors); |
| 28 | + const $metadata = generate$Metadata(tokenSetDimensions, themeNames, colors); |
35 | 29 | const $designsystemet = generate$Designsystemet(); |
36 | 30 |
|
37 | | - const files: OutputFile[] = []; |
38 | | - |
39 | 31 | files.push({ destination: $themesPath, output: stringify($themes) }); |
40 | 32 | files.push({ destination: $metadataPath, output: stringify($metadata) }); |
41 | 33 | files.push({ destination: $designsystemetPath, output: stringify($designsystemet) }); |
42 | 34 |
|
| 35 | + return files; |
| 36 | +}; |
| 37 | + |
| 38 | +export const tokenSetsToFiles = (tokenSets: TokenSets): OutputFile[] => { |
| 39 | + const files: OutputFile[] = []; |
| 40 | + |
43 | 41 | for (const [set, tokens] of tokenSets) { |
44 | 42 | const filePath = `${set}.json`; |
45 | 43 | files.push({ destination: filePath, output: stringify(tokens) }); |
46 | 44 | } |
47 | | - |
48 | 45 | return files; |
49 | 46 | }; |
0 commit comments