-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathgenerateTokens.mjs
More file actions
344 lines (310 loc) · 12.3 KB
/
generateTokens.mjs
File metadata and controls
344 lines (310 loc) · 12.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { glob } from 'glob';
import { createRequire } from 'node:module';
import { dirname, basename, sep } from 'node:path';
import { parse, stringify } from '@adobe/css-tools';
import { readFileSync } from 'node:fs';
const require = createRequire(import.meta.url);
const pfStylesDir = dirname(require.resolve('@patternfly/patternfly/patternfly.css'));
const version = 'v6';
// Helpers
const formatCustomPropertyName = (key) =>
key.replace(`--pf-${version}-`, '').replace('--pf-t--', 't_').replace(/-+/g, '_');
const getRegexMatches = (string, regex) => {
const res = {};
let matches;
while ((matches = regex.exec(string))) {
res[matches[1]] = matches[2].trim();
}
return res;
};
const getLightThemeDeclarations = (cssAst) =>
cssAst.stylesheet.rules
.filter(
(node) =>
node.type === 'rule' &&
!node.selectors.includes(`.pf-${version}-t-dark`) &&
(!node.selectors || !node.selectors.some((item) => item.includes(`.pf-${version}-theme-dark`))) // exclude dark theme blocks since dark theme variable values override default token values
)
.map((node) => node.declarations.filter((decl) => decl.type === 'declaration'))
.reduce((acc, val) => acc.concat(val), []); // flatten
const getDarkThemeDeclarations = (cssAst) =>
cssAst.stylesheet.rules
.filter(
(node) =>
node.type === 'rule' &&
node.selectors &&
node.selectors.some((item) => item.includes(`:where(.pf-${version}-theme-dark)`))
)
.map((node) => node.declarations.filter((decl) => decl.type === 'declaration'))
.reduce((acc, val) => acc.concat(val), []); // flatten
const formatFilePathToName = (filePath) => {
// const filePathArr = filePath.split('/');
let prefix = '';
if (filePath.includes('components/')) {
prefix = 'c_';
} else if (filePath.includes('layouts/')) {
prefix = 'l_';
}
return `${prefix}${basename(filePath, '.css').replace(/-+/g, '_')}`;
};
const getLocalVarsMap = (cssFiles) => {
const res = {};
cssFiles.forEach((filePath) => {
const cssAst = parse(readFileSync(filePath, 'utf8'));
getLightThemeDeclarations(cssAst).forEach(({ property, value, parent }) => {
if (res[property]) {
// Accounts for multiple delcarations out of root scope.
// TODO: revamp CSS var mapping
return;
}
if (property.startsWith(`--pf-${version}`)) {
res[property] = {
...res[property],
[parent.selectors[0]]: value
};
} else if (property.startsWith('--pf-t')) {
res[property] = {
...res[property],
[parent.selectors[0]]: value
};
}
});
});
return res;
};
const getDarkLocalVarsMap = (cssFiles) => {
const res = {};
cssFiles.forEach((filePath) => {
const cssAst = parse(readFileSync(filePath, 'utf8'));
getDarkThemeDeclarations(cssAst).forEach(({ property, value, parent }) => {
if (property.startsWith(`--pf-${version}`) || property.startsWith('--pf-t')) {
res[property] = {
...res[property],
[parent.selectors[0]]: value
};
}
});
});
return res;
};
/**
* Generates tokens from CSS in node_modules/@patternfly/patternfly/**
*
* @returns {object} of form {
* c_about_modal_box: {
* ".pf-c-about-modal-box" : {
* "global_Color_100": {
* "name": "--pf-${version}-global--Color--100",
* "value": "#fff",
* "values": [
* "--pf-${version}-global--Color--light-100",
* "$pf-global--Color--light-100",
* "$pf-color-white",
* "#fff"
* ]
* },
* },
* ".pf-c-about-modal-box .pf-c-card": {}
* }
* }
*/
export function generateTokens() {
const cssFiles = glob
.sync(['{**/{components,layouts}/**/*.css', '**/patternfly-charts.css', '**/patternfly-variables.css}'].join(','), {
cwd: pfStylesDir,
ignore: ['assets/**', '/**/_index.css'],
absolute: true
})
// Sort to put variables and charts at END of list so getLocalVarsMap returns correct values
.sort((a, b) => (a.split(sep).length < b.split(sep).length ? 1 : -1));
// various lookup tables to resolve variables
const scssVariables = readFileSync(
require.resolve('@patternfly/patternfly/sass-utilities/scss-variables.scss'),
'utf8'
);
const scssVarsMap = getRegexMatches(scssVariables, /(\$.*):\s*([^;^!]+)/g);
const cssGlobalVariablesAst = parse(
readFileSync(require.resolve('@patternfly/patternfly/base/patternfly-variables.css'), 'utf8')
);
// Get dark theme variables map BEFORE filtering out dark theme rules
const cssGlobalVariablesDarkMap = {};
getDarkThemeDeclarations(cssGlobalVariablesAst).forEach(({ property, value }) => {
if (property.startsWith('--pf')) {
cssGlobalVariablesDarkMap[property] = value;
}
});
// Filter light theme variables (exclude dark theme)
cssGlobalVariablesAst.stylesheet.rules = cssGlobalVariablesAst.stylesheet.rules.filter(
(node) => !node.selectors || !node.selectors.some((item) => item.includes(`.pf-${version}-theme-dark`))
);
const cssGlobalVariablesMap = {
...getRegexMatches(stringify(cssGlobalVariablesAst), /(--pf-v6-[\w-]*):\s*([\w -_().]+);/g),
...getRegexMatches(stringify(cssGlobalVariablesAst), /(--pf-t--[\w-]*):\s*([^;]+);/g)
};
const getComputedCSSVarValue = (value, selector, varMap, isDark = false) =>
value.replace(/var\(([\w-]*)(,.*)?\)/g, (full, m1, m2) => {
if (m1.startsWith(`--pf-${version}-global`)) {
if (varMap[m1]) {
return varMap[m1] + (m2 || '');
} else {
return full;
}
} else if (m1.startsWith('--pf-t')) {
// For semantic tokens, check if they exist in the map
if (varMap[m1]) {
return varMap[m1] + (m2 || '');
} else {
// If not found in global map, try to resolve from local variables (e.g., chart tokens)
if (selector) {
return getFromLocalVarsMap(m1, selector, isDark) + (m2 || '');
} else {
return full;
}
}
} else {
if (selector) {
return getFromLocalVarsMap(m1, selector, isDark) + (m2 || '');
}
}
});
const getComputedScssVarValue = (value) =>
value.replace(/\$pf[^,)\s*/]*/g, (match) => {
if (scssVarsMap[match]) {
return scssVarsMap[match];
} else {
return match;
}
});
const getVarsMap = (value, selector, isDark = false) => {
// evaluate the value and follow the variable chain
const varsMap = [value];
const varMapToUse = isDark ? { ...cssGlobalVariablesMap, ...cssGlobalVariablesDarkMap } : cssGlobalVariablesMap;
let computedValue = value;
let finalValue = value;
while (finalValue.includes('var(--pf') || computedValue.includes('var(--pf') || computedValue.includes('$pf-')) {
// keep following the variable chain until we get to a value
if (finalValue.includes('var(--pf')) {
finalValue = getComputedCSSVarValue(finalValue, selector, varMapToUse, isDark);
}
if (computedValue.includes('var(--pf')) {
computedValue = getComputedCSSVarValue(computedValue, selector, varMapToUse, isDark);
} else {
computedValue = getComputedScssVarValue(computedValue);
}
// error out if variable doesn't exist to avoid infinite loop
if (finalValue === value && computedValue === value) {
// eslint-disable-next-line no-console
console.error(`Error: "${value}" variable not found`);
throw Error;
}
varsMap.push(computedValue);
}
const lastElement = varsMap[varsMap.length - 1];
if (lastElement.includes('pf-')) {
varsMap.push(finalValue);
}
// all values should not be boxed by var()
return varsMap.map((variable) => variable.replace(/var\(([\w-]*)\)/g, (_, match) => match));
};
// pre-populate the localVarsMap so we can lookup local variables within or across files, e.g. if we have the declaration:
// --pf-${version}-c-chip-group--MarginBottom: calc(var(--pf-${version}-c-chip-group--c-chip--MarginBottom) * -1);
// then we need to find:
// --pf-${version}-c-chip-group--c-chip--MarginBottom: var(--pf-${version}-global--spacer--xs);
const localVarsMap = getLocalVarsMap(cssFiles);
const darkLocalVarsMap = getDarkLocalVarsMap(cssFiles);
const getFromLocalVarsMap = (match, selector, isDark = false) => {
const varsMap = isDark ? { ...localVarsMap, ...darkLocalVarsMap } : localVarsMap;
if (varsMap[match]) {
// have exact selectors match
if (varsMap[match][selector]) {
return varsMap[match][selector];
} else if (Object.keys(varsMap[match]).length === 1) {
// only one match, return its value
return Object.values(varsMap[match])[0];
} else {
// find the nearest parent selector and return its value
let bestMatch = '';
let bestValue = '';
for (const key in varsMap[match]) {
if (varsMap[match].hasOwnProperty(key)) {
// remove trailing * from key to compare
let sanitizedKey = key.replace(/\*$/, '').trim();
sanitizedKey = sanitizedKey.replace(/>$/, '').trim();
sanitizedKey = sanitizedKey.replace(/\[.*\]$/, '').trim();
// is key a parent of selector?
if (selector.indexOf(sanitizedKey) > -1) {
if (sanitizedKey.length > bestMatch.length) {
// longest matching key is the winner
bestMatch = key;
bestValue = varsMap[match][key];
}
}
}
}
if (!bestMatch) {
// eslint-disable-next-line no-console
console.error(`no matching selector found for ${match} in localVarsMap`);
}
return bestValue;
}
} else {
// eslint-disable-next-line no-console
console.error(`no matching property found for ${match} in localVarsMap`);
}
};
const fileTokens = {};
cssFiles.forEach((filePath) => {
const cssAst = parse(readFileSync(filePath, 'utf8'));
// key is the formatted file name, e.g. c_about_modal_box
const key = formatFilePathToName(filePath);
// darkDeclarations are the dark theme properties within this file
const darkDeclarations = getDarkThemeDeclarations(cssAst);
getLightThemeDeclarations(cssAst)
.filter(({ property }) => property.startsWith('--pf'))
.forEach(({ property, value, parent }) => {
const selector = parent.selectors[0];
const varsMap = getVarsMap(value, selector);
const propertyObj = {
name: property,
value: varsMap[varsMap.length - 1]
};
if (varsMap.length > 1) {
propertyObj.values = varsMap;
}
// Check if there's a dark theme override for this property
const darkDecl = darkDeclarations.find((decl) => decl.property === property);
if (darkDecl) {
try {
const darkVarsMap = getVarsMap(darkDecl.value, selector, true);
propertyObj.darkValue = darkVarsMap[darkVarsMap.length - 1];
if (darkVarsMap.length > 1) {
propertyObj.darkValues = darkVarsMap;
}
} catch (e) {
// Skip dark value if it can't be resolved
// This can happen when dark theme uses variables that don't exist in the light theme
}
} else if (value.includes('var(--pf')) {
// No explicit dark override, but the value references other variables.
// Try to resolve the dark value through the dependency chain.
try {
const darkVarsMap = getVarsMap(value, selector, true);
const resolvedDarkValue = darkVarsMap[darkVarsMap.length - 1];
// Only add darkValue if it differs from the light value
if (resolvedDarkValue !== propertyObj.value) {
propertyObj.darkValue = resolvedDarkValue;
if (darkVarsMap.length > 1) {
propertyObj.darkValues = darkVarsMap;
}
}
} catch (e) {
// Skip if dark value can't be resolved through the chain
}
}
fileTokens[key] = fileTokens[key] || {};
fileTokens[key][selector] = fileTokens[key][selector] || {};
fileTokens[key][selector][formatCustomPropertyName(property)] = propertyObj;
});
});
return fileTokens;
}