办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { promises as fs } from 'node:fs';
import { join } from 'node:path';
 
const rootDir = process.cwd();
 
/**
 * 递归查找并删除目标目录
 * @param {string} currentDir - 当前遍历的目录路径
 */
async function cleanTargetsRecursively(currentDir, targets) {
  const items = await fs.readdir(currentDir);
 
  for (const item of items) {
    try {
      const itemPath = join(currentDir, item);
      if (targets.includes(item)) {
        // 匹配到目标目录或文件时直接删除
        await fs.rm(itemPath, { force: true, recursive: true });
        console.log(`Deleted: ${itemPath}`);
      }
      const stat = await fs.lstat(itemPath);
      if (stat.isDirectory()) {
        await cleanTargetsRecursively(itemPath, targets);
      }
    } catch {
      // console.error(
      //   `Error handling item ${item} in ${currentDir}: ${error.message}`,
      // );
    }
  }
}
 
(async function startCleanup() {
  // 要删除的目录及文件名称
  const targets = ['node_modules', 'dist', '.turbo', 'dist.zip'];
 
  const deleteLockFile = process.argv.includes('--del-lock');
  const cleanupTargets = [...targets];
  if (deleteLockFile) {
    cleanupTargets.push('pnpm-lock.yaml');
  }
 
  console.log(
    `Starting cleanup of targets: ${cleanupTargets.join(', ')} from root: ${rootDir}`,
  );
 
  try {
    await cleanTargetsRecursively(rootDir, cleanupTargets);
    console.log('Cleanup process completed.');
  } catch (error) {
    console.error(`Unexpected error during cleanup: ${error.message}`);
  }
})();