|
| 1 | +import * as fs from "fs" |
| 2 | +import * as path from "path" |
| 3 | + |
| 4 | +export const extractAllCSSVariables = (): Record<string, string> => { |
| 5 | + const variables: Record<string, string> = {} |
| 6 | + |
| 7 | + // There are too many of these and we don't need them |
| 8 | + const isExcludedVariable = (property: string): boolean => { |
| 9 | + return property.includes("-icon-") && (property.includes("-content") || property.includes("-font-family")) |
| 10 | + } |
| 11 | + |
| 12 | + const extractFromStyleRule = (style: CSSStyleDeclaration): void => { |
| 13 | + for (let i = 0; i < style.length; i++) { |
| 14 | + const property = style[i] |
| 15 | + if (property.startsWith("--") && !isExcludedVariable(property)) { |
| 16 | + const value = style.getPropertyValue(property).trim() |
| 17 | + if (value) { |
| 18 | + variables[property] = value |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + const extractFromStylesheets = (): void => { |
| 25 | + for (let i = 0; i < document.styleSheets.length; i++) { |
| 26 | + const sheet = document.styleSheets[i] |
| 27 | + try { |
| 28 | + const rules = sheet.cssRules || [] |
| 29 | + for (const rule of Array.from(rules)) { |
| 30 | + if (rule instanceof CSSStyleRule) { |
| 31 | + extractFromStyleRule(rule.style) |
| 32 | + } |
| 33 | + } |
| 34 | + } catch (_e) { |
| 35 | + // Skip inaccessible stylesheets |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + const extractFromComputedStyles = (): void => { |
| 41 | + const rootStyles = getComputedStyle(document.documentElement) |
| 42 | + for (let i = 0; i < rootStyles.length; i++) { |
| 43 | + const property = rootStyles[i] |
| 44 | + if (property.startsWith("--") && !isExcludedVariable(property)) { |
| 45 | + const value = rootStyles.getPropertyValue(property).trim() |
| 46 | + if (value && !variables[property]) { |
| 47 | + variables[property] = value |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + extractFromStylesheets() |
| 54 | + extractFromComputedStyles() |
| 55 | + |
| 56 | + return variables |
| 57 | +} |
| 58 | + |
| 59 | +export const generateCSSOutput = (allVariables: Record<string, string>): string => { |
| 60 | + const sortedEntries = Object.entries(allVariables).sort(([a], [b]) => a.localeCompare(b)) |
| 61 | + const cssLines = sortedEntries.map(([key, value]) => `${key}: ${value};`) |
| 62 | + |
| 63 | + return `/* All CSS Variables - Auto-extracted from VS Code via Playwright */ |
| 64 | +/* Generated on ${new Date().toISOString()} */ |
| 65 | +
|
| 66 | +${cssLines.map((line) => ` ${line}`).join("\n")} |
| 67 | +` |
| 68 | +} |
| 69 | + |
| 70 | +export const saveVariablesToFile = async (cssOutput: string, finalFilename: string): Promise<string> => { |
| 71 | + const outputDir = path.join(process.cwd(), "../storybook/generated-theme-styles") |
| 72 | + await fs.promises.mkdir(outputDir, { recursive: true }) |
| 73 | + const outputPath = path.join(outputDir, finalFilename) |
| 74 | + await fs.promises.writeFile(outputPath, cssOutput) |
| 75 | + return outputPath |
| 76 | +} |
| 77 | + |
0 commit comments