forked from fadykhayratblb/new-figma-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-tokens.cjs
80 lines (73 loc) · 2.45 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
const StyleDictionary = require("style-dictionary");
const {
registerTransforms,
transforms,
} = require("@tokens-studio/sd-transforms");
const { promises } = require("node:fs");
registerTransforms(StyleDictionary, {
expand: {
composition:false,
typography: false,
shadow: false,
border: false,
},
'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) => {
return tokensCategories.map((cat) => ({
destination: `${cat}/${cat}-${theme.toLowerCase()}.css`,
format: "css/variables",
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",
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),
],
},
},
}));
configs.forEach((cfg) => {
const sd = StyleDictionary.extend(cfg);
sd.cleanAllPlatforms();
sd.buildAllPlatforms();
});
}
run();