-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-tokens.cjs
138 lines (131 loc) · 3.96 KB
/
build-tokens.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const StyleDictionary = require("style-dictionary");
const {
registerTransforms,
transforms,
} = require("@tokens-studio/sd-transforms");
const { promises } = require("node:fs");
registerTransforms(StyleDictionary, {
"ts/color/modifiers": {
format: "hex",
},
});
StyleDictionary.registerTransformGroup({
name: "custom/tokens-studio",
transforms: [...transforms, "attribute/cti", "name/cti/kebab"],
});
// filters only tokens originating from core.json
const coreFilter = (token) => token.filePath.endsWith("Core.json");
const coreWithoutFilter = (token) => !token.filePath.includes("Core.json");
// filters tokens by attributes.category (first key in the token hierachy, see attributes/cti transform for more info)
// must match per component name, in this repository we currently only have "button"
const componentFilter = (cat) => (token) => token.attributes.category === cat;
// for each component (currently only button), filter those specific component tokens and output them
// to the component folder where the component source code will live
const generateComponentFiles = (tokensCategories, theme, format, ext) => {
return tokensCategories.map((cat) => ({
destination: `${cat}/${cat}-${theme.toLowerCase()}.${ext}`,
format: format,
filter: coreWithoutFilter,
options: {
selector: ":host",
fileHeader: "autoGeneratedFileHeader",
},
}));
};
async function run() {
const $themes = JSON.parse(await promises.readFile("tokens/$themes.json"));
const configs = $themes.map((theme) => ({
source: Object.entries(theme.selectedTokenSets)
.filter(([, val]) => val !== "disabled")
.map(([tokenset]) => `tokens/${tokenset}.json`),
fileHeader: {
autoGeneratedFileHeader: () => {
return [`Do not edit directly, this file was auto-generated`];
},
},
platforms: {
css: {
transformGroup: "custom/tokens-studio",
buildPath: "build/css/",
files: [
// core/semantic tokens, e.g. for application developer
{
destination: "style.css",
format: "css/variables",
},
// component tokens, e.g. for design system developer
...generateComponentFiles(
["fiber"],
theme.name,
"css/variables",
"css"
),
],
},
js: {
transformGroup: "tokens-studio",
buildPath: "build/js/",
files: [
{
destination: "variables.js",
format: "javascript/es6",
},
...generateComponentFiles(
["fiber"],
theme.name,
"javascript/es6",
"js"
),
],
},
JSON: {
transformGroup: "tokens-studio",
buildPath: "build/json/",
files: [
{
destination: "variables.json",
format: "json",
},
...generateComponentFiles(["fiber"], theme.name, "json", "json"),
],
},
android: {
transforms: [
"attribute/cti",
"name/cti/snake",
"color/hex",
"size/remToSp",
"size/remToDp",
"asset/path",
"content/quote",
"color/css"
],
buildPath: "build/android/",
files: [
{
destination: "style_dictionary_colors.xml",
format: "android/resources",
},
...generateComponentFiles(["fiber"], theme.name, "android/resources", "xml"),
],
},
scss:{
transformGroup: "tokens-studio",
buildPath: "build/scss/",
files: [
{
destination: "variables.scss",
format: "scss/variables",
},
...generateComponentFiles(["fiber"], theme.name, "scss/variables", "scss"),
],
}
},
}));
configs.forEach((cfg) => {
const sd = StyleDictionary.extend(cfg);
sd.cleanAllPlatforms();
sd.buildAllPlatforms();
});
}
run();