|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const PACKAGE_BASE = 'packages/vaadin-lumo-styles'; |
| 5 | + |
| 6 | +const styleModules = ['color', 'sizing', 'spacing', 'style', 'typography', 'user-colors']; |
| 7 | + |
| 8 | +function getCustomProperties() { |
| 9 | + const result = []; |
| 10 | + const uniqueProperties = []; |
| 11 | + |
| 12 | + styleModules.forEach((module) => { |
| 13 | + const content = fs.readFileSync(path.join(PACKAGE_BASE, `${module}.js`), 'utf8'); |
| 14 | + const customPropertyDeclarationRegex = /--[\w\d-]+:.*;/g; |
| 15 | + const propertyDeclarations = content.match(customPropertyDeclarationRegex); |
| 16 | + propertyDeclarations.forEach((propertyDeclaration) => { |
| 17 | + // Avoid duplicate property declarations from dark theme |
| 18 | + const propertyName = propertyDeclaration.split(':')[0]; |
| 19 | + if (!uniqueProperties.includes(propertyName)) { |
| 20 | + result.push(propertyDeclaration); |
| 21 | + uniqueProperties.push(propertyName); |
| 22 | + } |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + return result; |
| 27 | +} |
| 28 | + |
| 29 | +function getUtilityClasses() { |
| 30 | + const result = []; |
| 31 | + |
| 32 | + const modules = fs.readdirSync(path.join(PACKAGE_BASE, 'utilities')); |
| 33 | + modules |
| 34 | + .filter((module) => module.endsWith('.js')) |
| 35 | + .forEach((module) => { |
| 36 | + const content = fs.readFileSync(path.join(PACKAGE_BASE, 'utilities', module), 'utf8'); |
| 37 | + const cssSectionRegex = /css`((.|\s)*?)`/gm; |
| 38 | + let match; |
| 39 | + |
| 40 | + while ((match = cssSectionRegex.exec(content))) { |
| 41 | + let section = match[1]; |
| 42 | + // Remove escape backslash |
| 43 | + section = section.replace(/\\\\/g, '\\'); |
| 44 | + // Unindent |
| 45 | + section = section.replace(/\n\s{2}/g, '\n'); |
| 46 | + result.push(section); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + return result; |
| 51 | +} |
| 52 | + |
| 53 | +const customProperties = getCustomProperties(); |
| 54 | +const utilityClasses = getUtilityClasses(); |
| 55 | + |
| 56 | +let result = `/* This file contains declarations for CSS custom properties and utility |
| 57 | + * classes of the Lumo theme in order to provide auto-complete support |
| 58 | + * for IDEs. |
| 59 | + * |
| 60 | + * NOTE: This file is only intended to provide auto-complete support, |
| 61 | + * do not include it into an HTML page! |
| 62 | + */ |
| 63 | + |
| 64 | + `; |
| 65 | +result += '/* Custom CSS properties */\n'; |
| 66 | +result += ':host {\n'; |
| 67 | +customProperties.forEach((customProperty) => { |
| 68 | + result += ` ${customProperty}\n`; |
| 69 | +}); |
| 70 | +result += '}\n\n'; |
| 71 | + |
| 72 | +result += '/* Utility classes */\n'; |
| 73 | +result += utilityClasses.join(''); |
| 74 | + |
| 75 | +fs.writeFileSync(path.join(PACKAGE_BASE, 'auto-complete.css'), result, 'utf8'); |
0 commit comments