|
|
// 注意:此文件不能使用任何NPM依赖,因为它需要在依赖未安装或已损坏的情况下仍能运行
|
|
|
// 引入Node.js内置的子进程同步执行模块
|
|
|
const {execSync} = require('child_process');
|
|
|
|
|
|
// 执行一系列清理操作
|
|
|
cleanYarnCache(); // 清理Yarn缓存
|
|
|
resetNxCache(); // 重置NX构建缓存
|
|
|
deleteNodeModules(); // 删除所有node_modules目录
|
|
|
deleteBuildArtifacts(); // 删除构建产物
|
|
|
console.log('Cleanup complete!'); // 清理完成提示
|
|
|
|
|
|
/**
|
|
|
* 删除项目中的构建产物
|
|
|
* 包括ghost目录下所有名为build的文件夹和tsconfig.tsbuildinfo文件
|
|
|
*/
|
|
|
function deleteBuildArtifacts() {
|
|
|
console.log('Deleting all build artifacts...');
|
|
|
try {
|
|
|
// 查找ghost目录下所有名为build的目录并递归删除
|
|
|
execSync('find ./ghost -type d -name "build" -exec rm -rf \'{}\' +', {
|
|
|
stdio: 'inherit' // 子进程的输入输出继承自当前进程(显示执行过程)
|
|
|
});
|
|
|
// 查找ghost目录下所有tsconfig.tsbuildinfo文件并删除
|
|
|
execSync('find ./ghost -type f -name "tsconfig.tsbuildinfo" -delete', {
|
|
|
stdio: 'inherit'
|
|
|
});
|
|
|
} catch (error) {
|
|
|
console.error('Failed to delete build artifacts:', error);
|
|
|
process.exit(1); // 执行失败时退出进程,状态码1表示错误
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除项目中所有node_modules目录
|
|
|
* 用于彻底清理已安装的依赖包
|
|
|
*/
|
|
|
function deleteNodeModules() {
|
|
|
console.log('Deleting all node_modules directories...');
|
|
|
try {
|
|
|
// 从当前目录开始查找所有node_modules目录并递归删除
|
|
|
// -prune确保不会进入已找到的node_modules目录内部继续查找
|
|
|
execSync('find . -name "node_modules" -type d -prune -exec rm -rf \'{}\' +', {
|
|
|
stdio: 'inherit'
|
|
|
});
|
|
|
} catch (error) {
|
|
|
console.error('Failed to delete node_modules directories:', error);
|
|
|
process.exit(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 重置NX构建工具的缓存
|
|
|
* NX是用于 monorepo 项目的构建系统,缓存可能影响构建结果
|
|
|
*/
|
|
|
function resetNxCache() {
|
|
|
console.log('Resetting NX cache...');
|
|
|
try {
|
|
|
// 删除NX的缓存目录
|
|
|
execSync('rm -rf .nxcache .nx');
|
|
|
} catch (error) {
|
|
|
console.error('Failed to reset NX cache:', error);
|
|
|
process.exit(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清理Yarn包管理器的缓存
|
|
|
* 移除缓存的依赖包副本,避免旧版本缓存影响安装
|
|
|
*/
|
|
|
function cleanYarnCache() {
|
|
|
console.log('Cleaning yarn cache...');
|
|
|
try {
|
|
|
// 删除Yarn缓存目录下的所有内容
|
|
|
execSync('rm -rf .yarncache/* .yarncachecopy/*');
|
|
|
} catch (error) {
|
|
|
console.error('Failed to clean yarn cache:', error);
|
|
|
process.exit(1);
|
|
|
}
|
|
|
} |