Skip to content

Commit 3eba6f0

Browse files
author
Amine
committed
chore: add helper script to clean up node_modules directories in the monorepo
1 parent 26722fb commit 3eba6f0

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

scripts/reset-node-modules.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env node
2+
3+
import { existsSync, readdirSync, rmSync, statSync } from 'fs';
4+
import * as path from 'path';
5+
import { fileURLToPath } from 'url';
6+
7+
// This script can be helpful to clean up the monorepo from all the node_module folders to prepare a fresh install
8+
// to run it, you can use the following command from the root of the project:
9+
// npx tsx ./scripts/reset-node-modules.ts
10+
11+
12+
const __filename = fileURLToPath(import.meta.url);
13+
const __dirname = path.dirname(__filename);
14+
const projectRoot = path.join(__dirname, '..');
15+
16+
function removeNodeModules(dir: string, label: string): void {
17+
const nodeModulesPath = path.join(dir, 'node_modules');
18+
if (existsSync(nodeModulesPath)) {
19+
console.log(`🗑️ Removing ${label}: ${nodeModulesPath}`);
20+
try {
21+
rmSync(nodeModulesPath, { recursive: true, force: true });
22+
console.log(`✅ Removed ${label}`);
23+
} catch (error) {
24+
console.error(`❌ Failed to remove ${label}:`, error);
25+
}
26+
} else {
27+
console.log(`⏭️ ${label} not found, skipping`);
28+
}
29+
}
30+
31+
function removeNodeModulesInDirectory(parentDir: string, label: string): void {
32+
const fullPath = path.join(projectRoot, parentDir);
33+
34+
if (!existsSync(fullPath)) {
35+
console.log(`⏭️ ${label} directory not found, skipping`);
36+
return;
37+
}
38+
39+
const entries = readdirSync(fullPath);
40+
41+
for (const entry of entries) {
42+
const entryPath = path.join(fullPath, entry);
43+
44+
try {
45+
const stat = statSync(entryPath);
46+
if (stat.isDirectory()) {
47+
removeNodeModules(entryPath, `${label}/${entry}`);
48+
}
49+
} catch (error) {
50+
console.error(`❌ Error processing ${entryPath}:`, error);
51+
}
52+
}
53+
}
54+
55+
console.log('Starting cleanup of all node_modules directories...\n');
56+
57+
// Clean root node_modules
58+
console.log('Cleaning root node_modules...');
59+
removeNodeModules(projectRoot, 'root');
60+
console.log('');
61+
62+
// Clean demos node_modules
63+
console.log('Cleaning demos node_modules...');
64+
removeNodeModulesInDirectory('demos', 'demos');
65+
console.log('');
66+
67+
// Clean packages node_modules
68+
console.log('Cleaning packages node_modules...');
69+
removeNodeModulesInDirectory('packages', 'packages');
70+
console.log('');
71+
72+
// Clean tools node_modules
73+
console.log('Cleaning tools node_modules...');
74+
removeNodeModulesInDirectory('tools', 'tools');
75+
console.log('');
76+
77+
// Clean docs node_modules
78+
console.log('Cleaning docs node_modules...');
79+
removeNodeModules(path.join(projectRoot, 'docs'), 'docs');
80+
console.log('');
81+
82+
console.log('Cleanup complete!');
83+
console.log('Run "pnpm install" to reinstall dependencies');
84+

0 commit comments

Comments
 (0)