const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
class MonorepoCleanManager {
constructor() {
this.packagesDir = path.join(__dirname, '../packages');
this.packages = this.discoverPackages();
}
discoverPackages() {
return fs.readdirSync(this.packagesDir)
.filter(dir => {
const packagePath = path.join(this.packagesDir, dir, 'package.json');
return fs.existsSync(packagePath);
})
.map(dir => {
const packagePath = path.join(this.packagesDir, dir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
return {
name: packageJson.name,
path: path.join(this.packagesDir, dir),
hasCleanScript: !!(packageJson.scripts && packageJson.scripts.clean)
};
});
}
async cleanPackage(pkg) {
console.log(`๐งน Cleaning ${pkg.name}...`);
try {
if (pkg.hasCleanScript) {
execSync('pnpm run clean', {
cwd: pkg.path,
stdio: 'inherit'
});
} else {
// ้ป่ฎคๆธ
็ dist ๅ node_modules
const distPath = path.join(pkg.path, 'dist');
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true, force: true });
console.log(` โ
Removed dist directory`);
}
}
console.log(`โ
${pkg.name} cleaned successfully`);
} catch (error) {
console.error(`โ Failed to clean ${pkg.name}:`, error.message);
}
}
async cleanAll() {
console.log('๐งน Starting monorepo cleanup...');
console.log('๐ Discovered packages:', this.packages.map(p => p.name).join(', '));
// ๅนถ่กๆธ
็ๆๆๅ
await Promise.all(
this.packages.map(pkg => this.cleanPackage(pkg))
);
// ๆธ
็ๆ น็ฎๅฝ node_modules
console.log('๐งน Cleaning root node_modules...');
const rootNodeModules = path.join(__dirname, '../node_modules');
if (fs.existsSync(rootNodeModules)) {
fs.rmSync(rootNodeModules, { recursive: true, force: true });
console.log('โ
Root node_modules cleaned');
}
console.log('๐ All packages cleaned successfully!');
}
async cleanBuildArtifacts() {
console.log('๐งน Cleaning build artifacts only...');
for (const pkg of this.packages) {
const distPath = path.join(pkg.path, 'dist');
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true, force: true });
console.log(`โ
Cleaned ${pkg.name}/dist`);
}
}
console.log('โ
All build artifacts cleaned!');
}
}
// ๆง่กๆธ
็
if (require.main === module) {
const args = process.argv.slice(2);
const manager = new MonorepoCleanManager();
if (args.includes('--build-only')) {
manager.cleanBuildArtifacts().catch(error => {
console.error('๐ฅ Clean failed:', error);
process.exit(1);
});
} else {
manager.cleanAll().catch(error => {
console.error('๐ฅ Clean failed:', error);
process.exit(1);
});
}
}
module.exports = MonorepoCleanManager;