|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import * as fs from 'node:fs/promises'; |
| 11 | +import * as path from 'node:path'; |
| 12 | + |
| 13 | +const SOURCE_DIRS = ['x-pack', 'src', 'packages']; |
| 14 | +const SOURCE_FILE_REGEX = /(^.?|\.[^d]|[^.]d|[^.][^d])\.tsx?$/; |
| 15 | +const STYLED_COMPONENTS_IMPORT_REGEX = |
| 16 | + /import\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](styled-components)['"]/; |
| 17 | +const EMOTION_STYLED_IMPORT_REGEX = |
| 18 | + /import\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"](@emotion\/styled)['"]/; |
| 19 | + |
| 20 | +export interface Meta { |
| 21 | + path: string; |
| 22 | + usesStyledComponents: boolean; |
| 23 | + usesOnlyStyledComponents?: boolean; |
| 24 | + files?: Meta[]; |
| 25 | +} |
| 26 | + |
| 27 | +const walkDirectory = async (dirPath: string): Promise<Meta> => { |
| 28 | + const files: Meta[] = []; |
| 29 | + let usesStyledComponents = false; |
| 30 | + let usesOnlyStyledComponents = true; |
| 31 | + |
| 32 | + for (const file of await fs.readdir(dirPath, { withFileTypes: true })) { |
| 33 | + const fullPath = path.join(file.path, file.name); |
| 34 | + |
| 35 | + if (file.isDirectory()) { |
| 36 | + const meta = await walkDirectory(fullPath); |
| 37 | + if (meta.usesStyledComponents) { |
| 38 | + usesStyledComponents = true; |
| 39 | + } |
| 40 | + if (usesOnlyStyledComponents && !meta.usesOnlyStyledComponents) { |
| 41 | + usesOnlyStyledComponents = false; |
| 42 | + } |
| 43 | + files.push(meta); |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + if (!SOURCE_FILE_REGEX.test(file.name) || !file.isFile()) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + const meta: Meta = { |
| 52 | + path: fullPath, |
| 53 | + usesStyledComponents: false, |
| 54 | + }; |
| 55 | + |
| 56 | + const contents = await fs.readFile(fullPath, 'utf8'); |
| 57 | + const usesEmotionStyled = EMOTION_STYLED_IMPORT_REGEX.test(contents); |
| 58 | + meta.usesStyledComponents = STYLED_COMPONENTS_IMPORT_REGEX.test(contents); |
| 59 | + |
| 60 | + if (usesEmotionStyled) { |
| 61 | + usesOnlyStyledComponents = false; |
| 62 | + } |
| 63 | + |
| 64 | + if (usesEmotionStyled || meta.usesStyledComponents) { |
| 65 | + files.push(meta); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return { |
| 70 | + path: dirPath, |
| 71 | + files, |
| 72 | + usesStyledComponents, |
| 73 | + usesOnlyStyledComponents: usesStyledComponents && usesOnlyStyledComponents, |
| 74 | + }; |
| 75 | +}; |
| 76 | + |
| 77 | +export const findFiles = async (rootDirPath: string) => { |
| 78 | + return Promise.all(SOURCE_DIRS.map((dir) => walkDirectory(path.join(rootDirPath, dir)))); |
| 79 | +}; |
0 commit comments