forked from quadient/design-tokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
153 lines (143 loc) · 4.21 KB
/
config.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const StyleDictionary = require("style-dictionary");
const tinycolor = require("tinycolor2");
const path = require("path");
const fs = require("fs");
const isDarkThemeConfiguration = process.argv[3] && process.argv[3] === './configDarkTheme.js';
StyleDictionary.registerAction({
name: "convertToRGBa",
do: (_dictionary, config) => {
/**
* Due to tool transform limitation, color with opacity token is written as a 'getRGBa(color, opacity)' string on output (see opacity transformation)
* This action parses the string color with opacity and convert it into rgba color format
*/
const filePath = path.join(config.buildPath, config.files[0].destination);
var fileContent = fs.readFileSync(filePath, "utf-8");
// looking e.g. for: getRGBa(#FFFFFF, 0.68);
const convertToRGBaRegex = /getRGBa\((.{7}?)\,(.*?)\)/gm;
fileContent = fileContent.replace(convertToRGBaRegex, (_match, color, opacity) => {
return tinycolor(color).setAlpha(opacity).toRgbString();
});
/**
* Due to tool transform limitation, opacity has to be written as a string in the input configuration
* This action convert opacity in string back to number
* looking for: "0.??" and return just number
*/
const opacityToNumberRegex = /\"(0.[0-9][0-9]?)\"/gm;
fileContent = fileContent.replace(opacityToNumberRegex, (_match, opacity) => {
return opacity;
});
fs.writeFileSync(filePath, fileContent, "utf-8");
console.log(`RGBa conversion and opacity conversion for ${filePath} finished`);
},
undo: () => {},
});
let source = [
"src/brand.json",
"src/colorPrimaryVariant.json",
"src/color.json",
"src/colorBase.json",
"src/border.json",
"src/font.json",
"src/opacity.json",
"src/opacityBase.json",
"src/radius.json",
"src/spacing.json",
"src/sizingIcon.json",
"src/shadow.json",
]
if(isDarkThemeConfiguration) {
source.push(
"src/darkTheme/color.json",
"src/darkTheme/colorPrimaryVariant.json"
)
}
module.exports = {
source: source,
transform: {
pxUnit: {
type: "value",
matcher: (prop) => {
return prop.unit === "px";
},
transformer: (prop) => {
return `${prop.value}px`;
},
},
opacity: {
type: "value",
matcher: (prop) => {
return prop.opacity !== undefined;
},
transformer: (prop) => {
// prepare color value with opacity for convertToRGBa action
return `getRGBa(${prop.color},${prop.opacity})`;
},
}
},
platforms: {
scss: {
transforms: StyleDictionary.transformGroup.scss.concat(
"pxUnit",
"opacity"
),
buildPath: "build/scss/",
files: [
{
destination: isDarkThemeConfiguration ? "variablesDarkTheme.scss": "variables.scss",
format: "scss/variables",
},
],
actions: ["convertToRGBa"],
},
less: {
transforms: StyleDictionary.transformGroup.less.concat(
"pxUnit",
"opacity"
),
buildPath: "build/less/",
files: [
{
destination: isDarkThemeConfiguration ? "variablesDarkTheme.less": "variables.less",
format: "less/variables",
},
],
actions: ["convertToRGBa"],
},
css: {
transforms: StyleDictionary.transformGroup.css.concat(
"pxUnit",
"opacity"
),
buildPath: "build/css/",
files: [
{
destination: isDarkThemeConfiguration ? "variablesDarkTheme.css": "variables.css",
format: "css/variables",
},
],
actions: ["convertToRGBa"],
},
js: {
transforms: StyleDictionary.transformGroup.js.concat("opacity"),
buildPath: "build/js/",
files: [
{
destination: isDarkThemeConfiguration ? "variablesDarkTheme.js": "variables.js",
format: "javascript/es6",
},
],
actions: ["convertToRGBa"],
},
json: {
transforms: StyleDictionary.transformGroup.js.concat("opacity"),
buildPath: "build/json/",
files: [
{
destination: isDarkThemeConfiguration ? "variablesDarkTheme.json": "variables.json",
format: "json/flat",
},
],
actions: ["convertToRGBa"],
},
},
};