Debian Codegraph @

node/@patternfly/react-styles/scripts/generateClassMaps.mjs

raw
1 import { camelCase } from 'change-case';
2 import { glob } from 'glob';
3 import fs from 'node:fs';
4 import path from 'node:path';
5 import url from 'node:url';
6
7 /**
8 * @param {string} cssString - CSS string
9 */
10 function getCSSClasses(cssString) {
11 return cssString.match(/(\.)(?!\d)([^\s.,{[>+~#:)]*)(?![^{]*})/g);
12 }
13
14 /**
15 * @param {string} className - Class name
16 */
17 function formatClassName(className) {
18 return camelCase(className.replace(/pf-(v6-)?((c|l|m|u|is|has)-)?/g, ''));
19 }
20
21 /**
22 * @param {string} className - Class name
23 */
24 function isModifier(className) {
25 return Boolean(className && className.startsWith) && className.startsWith('.pf-m-');
26 }
27
28 /**
29 * @param {string} cssString - CSS string
30 */
31 function getClassMaps(cssString) {
32 const res = {};
33 const distinctClasses = new Set(getCSSClasses(cssString));
34
35 distinctClasses.forEach((className) => {
36 const key = formatClassName(className);
37 const value = className.replace('.', '').trim();
38 if (isModifier(className)) {
39 res.modifiers = res.modifiers || {};
40 res.modifiers[key] = value;
41 } else {
42 res[key] = value;
43 }
44 });
45
46 const ordered = {};
47 Object.keys(res)
48 .sort()
49 .forEach((key) => (ordered[key] = res[key]));
50
51 return ordered;
52 }
53
54 /**
55 * @returns {any} Map of file names to classMaps
56 */
57 export function generateClassMaps() {
58 const pfStylesDir = path.dirname(url.fileURLToPath(import.meta.resolve('@patternfly/patternfly/patternfly.css')));
59
60 const patternflyCSSFiles = glob.sync('**/*.css', {
61 cwd: pfStylesDir,
62 ignore: ['assets/**', '*.css', 'base/**'],
63 absolute: true
64 });
65 const srcCSSFiles = glob.sync('src/css/**/*.css');
66
67 const res = {};
68 patternflyCSSFiles
69 .concat(srcCSSFiles)
70 .map((file) => path.resolve(file)) // Normalize path for Windows
71 .forEach((file) => {
72 res[file] = getClassMaps(fs.readFileSync(file, 'utf8'));
73 });
74
75 return res;
76 }