-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
237 lines (211 loc) · 7.59 KB
/
index.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env node
import { exec } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { parse } from "@babel/parser";
import traverse from "@babel/traverse";
import chalk from "chalk";
import { Command } from "commander";
import { glob } from "glob";
import inquirer from "inquirer";
import { fileURLToPath } from "node:url";
import { detectPackageManager, getPackages, isDependencyUsedTransitively, readIgnoreConfig } from "./utils/index.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJsonPath = path.resolve(__dirname, "../package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
const { version } = packageJson;
const program = new Command();
const analyzeUnusedPackages = (verbose: boolean) => {
const log = (message: string) => {
if (verbose) {
console.log(chalk.blue(`[Verbose] ${message}`));
}
};
const packageJsonPath = path.resolve(process.cwd(), "package.json");
if (!fs.existsSync(packageJsonPath)) {
console.error(chalk.red("No package.json found in the current directory."));
process.exit(1);
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
const allDependencies = {
...packageJson.dependencies,
// TODO: more analysis on these types
...packageJson.devDependencies,
...packageJson.peerDependencies,
...packageJson.optionalDependencies,
};
const ignoredDependencies = new Set(readIgnoreConfig());
const usedDependencies = new Set();
const dependencyFileCount = new Map<string, number>();
const files: string[] = glob.sync("**/*.{js,jsx,ts,tsx}", {
ignore: ["node_modules/**", "dist/**", "build/**"],
});
for (const file of files) {
const content = fs.readFileSync(file, "utf-8");
try {
const ast = parse(content, {
sourceType: "module",
plugins: ["jsx", "typescript"],
});
traverse.default(ast, {
ImportDeclaration(path: any) {
const source = path.node.source.value;
for (const dep of Object.keys(allDependencies)) {
if (ignoredDependencies.has(dep)) continue;
if (source === dep || source.startsWith(`${dep}/`)) {
usedDependencies.add(dep);
dependencyFileCount.set(dep, (dependencyFileCount.get(dep) || 0) + 1);
}
}
},
CallExpression(path: any) {
// Handle require() calls
if (
path.node.callee.name === "require" &&
path.node.arguments.length > 0 &&
path.node.arguments[0].type === "StringLiteral"
) {
const source = path.node.arguments[0].value;
for (const dep of Object.keys(allDependencies)) {
if (ignoredDependencies.has(dep)) continue;
if (source === dep || source.startsWith(`${dep}/`)) {
usedDependencies.add(dep);
dependencyFileCount.set(dep, (dependencyFileCount.get(dep) || 0) + 1);
}
}
}
},
});
} catch (error) {
if (error instanceof Error) {
console.error(chalk.red(`Failed to parse ${file}: ${error.message}`));
}
}
}
if (verbose) {
console.log(chalk.blue("Dependency usage count:"));
for (const [dep, count] of dependencyFileCount.entries()) {
console.log(`${dep}: ${count} file(s)`);
}
}
// Identify unused dependencies
const unusedDependencies = Object.keys(packageJson.dependencies).filter((dep) => {
if (usedDependencies.has(dep)) return false;
if (ignoredDependencies.has(dep)) return false;
// if (coreDependencies.has(dep)) {
// console.log(chalk.yellow(`[Warning] ${dep} is core to your environment and may not appear in imports.`));
// return false;
// }
if (isDependencyUsedTransitively(dep, allDependencies, log)) {
return false;
}
return true;
});
if (unusedDependencies.length === 0) {
console.log(chalk.green("No unused dependencies found!"));
} else {
console.log(chalk.yellow("The following dependencies appear to be unused:"));
for (const dep of unusedDependencies) {
console.log(`- ${chalk.red(dep)}`);
}
return unusedDependencies;
}
return [];
};
const uninstallPackagesBatch = (packages: string[], packageManager: string) => {
const managerRemoveCmd =
packageManager === "yarn" ? "yarn remove" : packageManager === "pnpm" ? "pnpm remove" : "npm uninstall";
const batchSize = 5;
const chunks: string[][] = [];
for (let i = 0; i < packages.length; i += batchSize) {
chunks.push(packages.slice(i, i + batchSize));
}
const uninstallChunk = (chunk: string[]) => {
return new Promise((resolve, reject) => {
const command = `${managerRemoveCmd} ${chunk.join(" ")}`;
console.log(chalk.blue(`Running: ${command}`));
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`Error: ${error.message}`));
reject(error);
} else if (stderr) {
console.error(chalk.yellow(`Warning: ${stderr}`));
resolve(stderr);
} else {
console.log(chalk.green(`Successfully uninstalled: ${chunk.join(", ")}`));
resolve(stdout);
}
});
});
};
const runBatches = async () => {
for (const chunk of chunks) {
try {
await uninstallChunk(chunk);
} catch (err) {
console.error(chalk.red(`Failed to uninstall batch: ${chunk.join(", ")}`));
}
}
};
runBatches().then(() => {
console.log(chalk.green("All batches processed."));
});
};
program.name("pkg-uninstaller").description("A CLI tool to uninstall Node.js packages interactively").version(version);
program
.command("analyze")
.description("Analyze and identify unused dependencies in the project")
.option("-v, --verbose", "Enable verbose logging")
.action(async (options) => {
const unusedDependencies = analyzeUnusedPackages(options.verbose);
if (unusedDependencies.length > 0) {
const { removeUnused } = await inquirer.prompt([
{
type: "confirm",
name: "removeUnused",
message: "Would you like to uninstall unused dependencies?",
default: false,
},
]);
if (removeUnused) {
const packageManager = detectPackageManager();
uninstallPackagesBatch(unusedDependencies, packageManager);
}
}
});
program
.command("uninstall")
.description("Uninstall selected packages from package.json")
.action(async () => {
try {
const packages = getPackages();
if (packages.length === 0) {
console.log(chalk.yellow("No packages found to uninstall."));
return;
}
const { selectedPackages } = await inquirer.prompt([
{
type: "checkbox",
name: "selectedPackages",
message: "Select packages to uninstall:",
choices: packages,
},
]);
if (selectedPackages.length === 0) {
console.log(chalk.yellow("No packages selected for uninstallation."));
return;
}
const packageManager = detectPackageManager();
console.log(chalk.blue(`Detected package manager: ${packageManager}`));
uninstallPackagesBatch(selectedPackages, packageManager);
} catch (error) {
if (error instanceof Error && "isTtyError" in error) {
console.error(chalk.red("Prompt couldn't be rendered in the current environment."));
} else {
console.error(chalk.red("Prompt was closed forcefully."));
}
process.exit(1);
}
});
program.parse(process.argv);