diff options
Diffstat (limited to 'scripts/build-theme-info.js')
-rw-r--r-- | scripts/build-theme-info.js | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/scripts/build-theme-info.js b/scripts/build-theme-info.js index 4058be942..8aee96ab7 100644 --- a/scripts/build-theme-info.js +++ b/scripts/build-theme-info.js | |||
@@ -23,7 +23,7 @@ async function getRulesFromCssFile(file) { | |||
23 | const cssSrc = (await fs.readFile(file)).toString(); | 23 | const cssSrc = (await fs.readFile(file)).toString(); |
24 | const cssTree = css.parse(cssSrc); | 24 | const cssTree = css.parse(cssSrc); |
25 | 25 | ||
26 | return cssTree.stylesheet.rules; | 26 | return cssTree.stylesheet?.rules; |
27 | } | 27 | } |
28 | 28 | ||
29 | /** | 29 | /** |
@@ -49,25 +49,30 @@ async function getRulesFromCssFile(file) { | |||
49 | function getSelectorsDeclaringValues(rules, values) { | 49 | function getSelectorsDeclaringValues(rules, values) { |
50 | const output = {}; | 50 | const output = {}; |
51 | 51 | ||
52 | rules.forEach((rule) => { | 52 | for (const rule of rules) { |
53 | if (rule.declarations) { | 53 | if (rule.declarations) { |
54 | rule.declarations.forEach((declaration) => { | 54 | for (const declaration of rule.declarations) { |
55 | if (declaration.type === 'declaration' | 55 | if ( |
56 | && values.includes(declaration.value.toLowerCase())) { | 56 | declaration.type === 'declaration' && |
57 | values.includes(declaration.value.toLowerCase()) | ||
58 | ) { | ||
57 | if (!output[declaration.property]) { | 59 | if (!output[declaration.property]) { |
58 | output[declaration.property] = []; | 60 | output[declaration.property] = []; |
59 | } | 61 | } |
60 | output[declaration.property] = output[declaration.property].concat(rule.selectors); | 62 | // eslint-disable-next-line unicorn/prefer-spread |
63 | output[declaration.property] = output[declaration.property].concat( | ||
64 | rule.selectors, | ||
65 | ); | ||
61 | } | 66 | } |
62 | }); | 67 | } |
63 | } | 68 | } |
64 | }); | 69 | } |
65 | 70 | ||
66 | return output; | 71 | return output; |
67 | } | 72 | } |
68 | 73 | ||
69 | async function generateThemeInfo() { | 74 | async function generateThemeInfo() { |
70 | if (!await fs.pathExists(cssFile)) { | 75 | if (!(await fs.pathExists(cssFile))) { |
71 | console.log('Please make sure to build the project first.'); | 76 | console.log('Please make sure to build the project first.'); |
72 | return; | 77 | return; |
73 | } | 78 | } |
@@ -75,23 +80,24 @@ async function generateThemeInfo() { | |||
75 | // Read and parse css bundle | 80 | // Read and parse css bundle |
76 | const rules = await getRulesFromCssFile(cssFile); | 81 | const rules = await getRulesFromCssFile(cssFile); |
77 | 82 | ||
78 | console.log(`Found ${rules.length} rules`); | 83 | console.log(`Found ${rules?.length} rules`); |
79 | 84 | ||
80 | // Get rules specifying the brand colors | 85 | // Get rules specifying the brand colors |
81 | const brandRules = getSelectorsDeclaringValues(rules, accentColors); | 86 | const brandRules = getSelectorsDeclaringValues(rules, accentColors); |
82 | 87 | ||
83 | console.log(`Found ${Object.keys(brandRules).join(', ')} properties that set color to brand color`); | 88 | console.log( |
89 | `Found ${Object.keys(brandRules).join( | ||
90 | ', ', | ||
91 | )} properties that set color to brand color`, | ||
92 | ); | ||
84 | 93 | ||
85 | // Join array of declarations into a single string | 94 | // Join array of declarations into a single string |
86 | Object.keys(brandRules).forEach((rule) => { | 95 | for (const rule of Object.keys(brandRules)) { |
87 | brandRules[rule] = brandRules[rule].join(', '); | 96 | brandRules[rule] = brandRules[rule].join(', '); |
88 | }); | 97 | } |
89 | 98 | ||
90 | // Write object with theme info to file | 99 | // Write object with theme info to file |
91 | fs.writeFile( | 100 | fs.writeFile(outputFile, JSON.stringify(brandRules)); |
92 | outputFile, | ||
93 | JSON.stringify(brandRules), | ||
94 | ); | ||
95 | } | 101 | } |
96 | 102 | ||
97 | generateThemeInfo(); | 103 | generateThemeInfo(); |