Skip to content

Commit cfa1694

Browse files
authored
Add script that counts the number of styled-components imports compared with original number (#10616)
1 parent 94eda8a commit cfa1694

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
"start": "node dist/server",
189189
"start:ci": "nyc --silent --exclude \".next/**\" node server",
190190
"start:e2e": "cross-env TZ=UTC NODE_ENV=production OC_ENV=e2e node server",
191+
"styled-components-migration-progress": "tsx scripts/styled-components-migration-progress.ts",
191192
"styleguide:build": "storybook build",
192193
"styleguide:dev": "storybook dev -p 6006",
193194
"test": "npm run test:jest",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)