# 检查前端项目和node项目中未被使用的依赖包

import fs from 'fs'
import path from 'path'
const projectDir = path.resolve("."); // 当前项目目录
const excludeDirs = ["node_modules", ".git"]; // 应该排除的目录

// 读取并解析package.json
function readPackageJson() {
    const packageJsonPath = path.join(projectDir, "package.json");
    if (!fs.existsSync(packageJsonPath)) {
        console.error("package.json not found.");
        process.exit(1);
    }
    return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
}

// 递归遍历目录获取所有文件路径
function getAllFiles(dirPath, arrayOfFiles) {
    const files = fs.readdirSync(dirPath);

    arrayOfFiles = arrayOfFiles || [];

    files.forEach(function (file) {
        if (fs.statSync(dirPath + "/" + file).isDirectory()) {
            if (!excludeDirs.includes(file)) {
                arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
            }
        } else {
            arrayOfFiles.push(path.join(dirPath, "/", file));
        }
    });

    return arrayOfFiles;
}

// 检查依赖是否在文件中被引用,包括动态引用
function isDependencyUsed(files, dependency) {
    const regexStaticImport = new RegExp(
        `require\\(['"\`]${dependency}['"\`]|from ['"\`]${dependency}['"\`]`,
        "i"
    );
    const regexDynamicImport = new RegExp(
        `import\\(['"\`]${dependency}['"\`]\\)`,
        "i"
    );
    return files.some((file) => {
        const fileContent = fs.readFileSync(file, "utf8");
        return (
            regexStaticImport.test(fileContent) ||
            regexDynamicImport.test(fileContent)
        );
    });
}

function findUnusedDependencies() {
    const { dependencies } = readPackageJson();
    const allFiles = getAllFiles(projectDir);
    const unusedDependencies = [];

    Object.keys(dependencies).forEach((dependency) => {
        if (!isDependencyUsed(allFiles, dependency)) {
            unusedDependencies.push(dependency);
        }
    });

    return unusedDependencies;
}

const unusedDependencies = findUnusedDependencies();
if (unusedDependencies.length > 0) {
    console.log("未使用的依赖:", unusedDependencies.join(", "));
} else {
    console.log("所有依赖都已使用。");
}
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73