|
| 1 | +// This script counts the number of styled-components imports in the project and compares it to the number of imports before TailwindCSS was introduced. |
| 2 | + |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +const SEARCH_DIR = path.resolve(__dirname, '..'); |
| 7 | +const ORIGINAL_IMPORT_COUNT = 343; |
| 8 | + |
| 9 | +function countStyledComponentsImports(searchDir: string): number { |
| 10 | + let count = 0; |
| 11 | + |
| 12 | + function searchDirectory(directory: string) { |
| 13 | + const files = fs.readdirSync(directory); |
| 14 | + |
| 15 | + for (const file of files) { |
| 16 | + const fullPath = path.join(directory, file); |
| 17 | + let stat; |
| 18 | + try { |
| 19 | + stat = fs.statSync(fullPath); |
| 20 | + } catch (error) { |
| 21 | + continue; // skip entries that cannot be accessed |
| 22 | + } |
| 23 | + |
| 24 | + if (stat.isDirectory()) { |
| 25 | + if (file !== 'node_modules' && file[0] !== '.') { |
| 26 | + // skip node_modules and hidden directories |
| 27 | + searchDirectory(fullPath); |
| 28 | + } |
| 29 | + } else if ( |
| 30 | + fullPath.endsWith('.js') || |
| 31 | + fullPath.endsWith('.jsx') || |
| 32 | + fullPath.endsWith('.ts') || |
| 33 | + fullPath.endsWith('.tsx') |
| 34 | + ) { |
| 35 | + const content = fs.readFileSync(fullPath, 'utf-8'); |
| 36 | + const lines = content.split('\n'); |
| 37 | + |
| 38 | + for (const line of lines) { |
| 39 | + if (line.match(/import .* from 'styled-components'/) || line.match(/require\('styled-components'\)/)) { |
| 40 | + count++; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + searchDirectory(searchDir); |
| 48 | + return count; |
| 49 | +} |
| 50 | + |
| 51 | +function calculateProgress(currentCount: number, originalCount: number): number { |
| 52 | + return 100 - (currentCount / originalCount) * 100; |
| 53 | +} |
| 54 | + |
| 55 | +const currentCount = countStyledComponentsImports(SEARCH_DIR); |
| 56 | +const progress = calculateProgress(currentCount, ORIGINAL_IMPORT_COUNT); |
| 57 | + |
| 58 | +console.log(`Current number of styled-components imports: ${currentCount}`); // eslint-disable-line no-console |
| 59 | +console.log(`Original number of styled-components imports: ${ORIGINAL_IMPORT_COUNT}`); // eslint-disable-line no-console |
| 60 | +console.log(`Migration Progress: ${progress.toFixed(2)}%`); // eslint-disable-line no-console |
0 commit comments