pruneplan 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +60 -2
- package/bin/pruneplan.js +254 -0
- package/package.json +36 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lakshmi Udbhav Regadamilli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pruneplan
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Find dead code and unused packages. Review the cleanup before applying it.**
|
|
4
|
+
|
|
5
|
+
`pruneplan` gives you one reviewable cleanup flow for unused dependencies, files, exports, and types. It uses [Knip](https://knip.dev/) to analyze the project, removes dependencies through the package manager already in use, and updates the lockfile as part of that removal.
|
|
6
|
+
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
From the root of a JavaScript or TypeScript project:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx pruneplan
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This only prints a plan. After reviewing it, apply the cleanup interactively:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npx pruneplan --apply
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
To scan a different project, pass its directory:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npx pruneplan ../my-app
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## What happens when you apply
|
|
28
|
+
|
|
29
|
+
1. `pruneplan` asks Knip to scan the project and displays the cleanup candidates.
|
|
30
|
+
2. After you confirm, it removes unused dependencies with npm, pnpm, Yarn, or Bun, updating the package manager's lockfile.
|
|
31
|
+
3. It asks Knip to remove reported unused files, exports, and types.
|
|
32
|
+
4. It runs any `typecheck`, `lint`, `test`, and `build` scripts defined in the project's `package.json`.
|
|
33
|
+
5. It leaves the changes in your working tree for review with `git diff`.
|
|
34
|
+
|
|
35
|
+
For CI or other non-interactive use, `--yes` explicitly skips the prompt:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npx pruneplan --yes
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Safety
|
|
42
|
+
|
|
43
|
+
- The default command only reports findings. It does not modify files.
|
|
44
|
+
- Applying requires `--apply` plus an interactive confirmation, or the explicit `--yes` flag.
|
|
45
|
+
- Package removals use your detected package manager so its lockfile is updated too.
|
|
46
|
+
- npm and pnpm package removals skip install scripts.
|
|
47
|
+
- Validation runs only scripts that already exist in your project. These are ordinary project commands and may have their own side effects.
|
|
48
|
+
- Static analysis can miss runtime-loaded packages and exports used through reflection, plugins, or external consumers. Review the plan and final diff before committing.
|
|
49
|
+
- If validation fails, the changes remain in your working tree so you can inspect and fix them; `pruneplan` does not discard project changes.
|
|
50
|
+
|
|
51
|
+
## Requirements
|
|
52
|
+
|
|
53
|
+
- Node.js 20.19+, or 22.12+
|
|
54
|
+
- A JavaScript or TypeScript project with a `package.json`
|
|
55
|
+
- npm, pnpm, Yarn, or Bun, identified from a lockfile or the `packageManager` field
|
|
56
|
+
|
|
57
|
+
Knip is installed as a dependency of `pruneplan`; you do not need to add it to the project being scanned.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
package/bin/pruneplan.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { createInterface } from 'node:readline/promises';
|
|
5
|
+
import { stdin, stdout } from 'node:process';
|
|
6
|
+
import { access, readFile } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
const KNIP_JSON_FLAG = '--reporter';
|
|
11
|
+
const APPLY_FIX_TYPES = ['files', 'exports', 'types'];
|
|
12
|
+
const CHECK_SCRIPTS = ['typecheck', 'lint', 'test', 'build'];
|
|
13
|
+
|
|
14
|
+
function parseArgs(argv) {
|
|
15
|
+
const options = { apply: false, yes: false, directory: process.cwd() };
|
|
16
|
+
let hasDirectory = false;
|
|
17
|
+
|
|
18
|
+
for (const arg of argv) {
|
|
19
|
+
if (arg === '--apply') options.apply = true;
|
|
20
|
+
else if (arg === '--yes' || arg === '-y') {
|
|
21
|
+
options.apply = true;
|
|
22
|
+
options.yes = true;
|
|
23
|
+
} else if (arg === '--help' || arg === '-h') options.help = true;
|
|
24
|
+
else if (arg.startsWith('-')) throw new Error(`Unknown option: ${arg}`);
|
|
25
|
+
else if (hasDirectory) throw new Error('Only one project path can be supplied.');
|
|
26
|
+
else {
|
|
27
|
+
options.directory = path.resolve(arg);
|
|
28
|
+
hasDirectory = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return options;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function printHelp() {
|
|
36
|
+
stdout.write(`pruneplan — review unused dependencies and dead code\n\n`);
|
|
37
|
+
stdout.write(`Usage:\n pruneplan [project-directory] [--apply] [--yes]\n\n`);
|
|
38
|
+
stdout.write(`Options:\n`);
|
|
39
|
+
stdout.write(` --apply Apply the displayed cleanup plan after confirmation\n`);
|
|
40
|
+
stdout.write(` --yes Apply without an interactive confirmation (for automation)\n`);
|
|
41
|
+
stdout.write(` -h, --help Show this help\n\n`);
|
|
42
|
+
stdout.write(`Knip is included with pruneplan; no project setup is required.\n`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function readProject(directory) {
|
|
46
|
+
const packagePath = path.join(directory, 'package.json');
|
|
47
|
+
const packageJson = JSON.parse(await readFile(packagePath, 'utf8'));
|
|
48
|
+
return { packagePath, packageJson };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function detectPackageManager(directory, packageJson) {
|
|
52
|
+
const lockfiles = [
|
|
53
|
+
['pnpm-lock.yaml', 'pnpm'],
|
|
54
|
+
['yarn.lock', 'yarn'],
|
|
55
|
+
['bun.lock', 'bun'],
|
|
56
|
+
['bun.lockb', 'bun'],
|
|
57
|
+
['package-lock.json', 'npm'],
|
|
58
|
+
['npm-shrinkwrap.json', 'npm'],
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
for (const [file, manager] of lockfiles) {
|
|
62
|
+
try {
|
|
63
|
+
await access(path.join(directory, file));
|
|
64
|
+
return manager;
|
|
65
|
+
} catch {}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const declared = packageJson.packageManager?.split('@')[0];
|
|
69
|
+
if (['npm', 'pnpm', 'yarn', 'bun'].includes(declared)) return declared;
|
|
70
|
+
throw new Error('Could not detect the package manager. Add a lockfile or packageManager field to package.json.');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function run(command, args, cwd, { capture = false } = {}) {
|
|
74
|
+
const result = spawnSync(command, args, {
|
|
75
|
+
cwd,
|
|
76
|
+
encoding: 'utf8',
|
|
77
|
+
stdio: capture ? ['ignore', 'pipe', 'pipe'] : 'inherit',
|
|
78
|
+
shell: process.platform === 'win32',
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (result.error) throw new Error(`${command} could not be started: ${result.error.message}`);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function findKnip(directory) {
|
|
86
|
+
let current = directory;
|
|
87
|
+
while (true) {
|
|
88
|
+
const localBin = path.join(current, 'node_modules', '.bin', process.platform === 'win32' ? 'knip.cmd' : 'knip');
|
|
89
|
+
try {
|
|
90
|
+
await access(localBin);
|
|
91
|
+
return { command: localBin, prefixArgs: [] };
|
|
92
|
+
} catch {}
|
|
93
|
+
|
|
94
|
+
const parent = path.dirname(current);
|
|
95
|
+
if (parent === current) break;
|
|
96
|
+
current = parent;
|
|
97
|
+
}
|
|
98
|
+
const bundledKnip = fileURLToPath(new URL('../node_modules/knip/bin/knip.js', import.meta.url));
|
|
99
|
+
try {
|
|
100
|
+
await access(bundledKnip);
|
|
101
|
+
return { command: process.execPath, prefixArgs: [bundledKnip] };
|
|
102
|
+
} catch {}
|
|
103
|
+
|
|
104
|
+
return { command: 'knip', prefixArgs: [] };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function extractIssues(output) {
|
|
108
|
+
const start = output.indexOf('{');
|
|
109
|
+
const end = output.lastIndexOf('}');
|
|
110
|
+
if (start < 0 || end < start) throw new Error('Knip did not return JSON. Run `knip --reporter json` to inspect its output.');
|
|
111
|
+
const parsed = JSON.parse(output.slice(start, end + 1));
|
|
112
|
+
if (!Array.isArray(parsed.issues)) throw new Error('Knip JSON output did not include an issues array.');
|
|
113
|
+
return parsed.issues;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function collectPlan(issues) {
|
|
117
|
+
const files = [];
|
|
118
|
+
const exports = [];
|
|
119
|
+
const dependencies = new Map();
|
|
120
|
+
|
|
121
|
+
for (const issue of issues) {
|
|
122
|
+
for (const item of issue.files ?? []) files.push({ file: issue.file, name: item.name });
|
|
123
|
+
for (const category of ['exports', 'types', 'enumMembers', 'namespaceMembers']) {
|
|
124
|
+
for (const item of issue[category] ?? []) exports.push({ file: issue.file, category, name: item.name });
|
|
125
|
+
}
|
|
126
|
+
for (const category of ['dependencies', 'devDependencies']) {
|
|
127
|
+
for (const item of issue[category] ?? []) {
|
|
128
|
+
dependencies.set(item.name, { name: item.name, category, file: issue.file });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { files, exports, dependencies: [...dependencies.values()] };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function displayPlan(plan, manager) {
|
|
137
|
+
const total = plan.files.length + plan.exports.length + plan.dependencies.length;
|
|
138
|
+
stdout.write(`\nPruneplan found ${total} cleanup candidate${total === 1 ? '' : 's'}.\n`);
|
|
139
|
+
if (total === 0) {
|
|
140
|
+
stdout.write('Your project looks tidy.\n');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (plan.dependencies.length) {
|
|
145
|
+
stdout.write(`\nUnused packages (${plan.dependencies.length}) — will be removed with ${manager}:\n`);
|
|
146
|
+
for (const item of plan.dependencies) stdout.write(` • ${item.name} (${item.category})\n`);
|
|
147
|
+
}
|
|
148
|
+
if (plan.files.length) {
|
|
149
|
+
stdout.write(`\nUnused files (${plan.files.length}) — Knip can delete these:\n`);
|
|
150
|
+
for (const item of plan.files) stdout.write(` • ${item.file}\n`);
|
|
151
|
+
}
|
|
152
|
+
if (plan.exports.length) {
|
|
153
|
+
stdout.write(`\nUnused exports and types (${plan.exports.length}) — Knip can remove these:\n`);
|
|
154
|
+
for (const item of plan.exports) stdout.write(` • ${item.name} in ${item.file}\n`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function confirmApply() {
|
|
159
|
+
if (!stdin.isTTY || !stdout.isTTY) {
|
|
160
|
+
throw new Error('Applying in a non-interactive terminal requires the explicit `--yes` flag.');
|
|
161
|
+
}
|
|
162
|
+
const prompt = createInterface({ input: stdin, output: stdout });
|
|
163
|
+
try {
|
|
164
|
+
const answer = await prompt.question('\nApply this cleanup plan? [y/N] ');
|
|
165
|
+
return answer.trim().toLowerCase() === 'y';
|
|
166
|
+
} finally {
|
|
167
|
+
prompt.close();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function managerRemoveArgs(manager, packages) {
|
|
172
|
+
switch (manager) {
|
|
173
|
+
case 'npm': return ['uninstall', ...packages, '--ignore-scripts'];
|
|
174
|
+
case 'pnpm': return ['remove', ...packages, '--ignore-scripts'];
|
|
175
|
+
case 'yarn': return ['remove', ...packages];
|
|
176
|
+
case 'bun': return ['remove', ...packages];
|
|
177
|
+
default: throw new Error(`Unsupported package manager: ${manager}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function runChecks(directory, packageJson) {
|
|
182
|
+
const scripts = packageJson.scripts ?? {};
|
|
183
|
+
const available = CHECK_SCRIPTS.filter((name) => typeof scripts[name] === 'string');
|
|
184
|
+
if (!available.length) {
|
|
185
|
+
stdout.write('\nNo typecheck, lint, test, or build scripts found; skipped validation.\n');
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const manager = await detectPackageManager(directory, packageJson);
|
|
190
|
+
stdout.write(`\nRunning project checks with ${manager}: ${available.join(', ')}\n`);
|
|
191
|
+
for (const name of available) {
|
|
192
|
+
const args = manager === 'npm' ? ['run', name] : ['run', name];
|
|
193
|
+
const result = run(manager, args, directory);
|
|
194
|
+
if (result.status !== 0) {
|
|
195
|
+
stdout.write(`\nValidation failed at \`${name}\`. Review the changes with git diff.\n`);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function main() {
|
|
203
|
+
const options = parseArgs(process.argv.slice(2));
|
|
204
|
+
if (options.help) return printHelp();
|
|
205
|
+
|
|
206
|
+
const directory = path.resolve(options.directory);
|
|
207
|
+
const { packageJson } = await readProject(directory);
|
|
208
|
+
const manager = await detectPackageManager(directory, packageJson);
|
|
209
|
+
const knip = await findKnip(directory);
|
|
210
|
+
|
|
211
|
+
stdout.write(`Scanning ${path.relative(process.cwd(), directory) || '.'} with Knip…\n`);
|
|
212
|
+
const scan = run(knip.command, [...knip.prefixArgs, KNIP_JSON_FLAG, 'json', '--no-exit-code'], directory, { capture: true });
|
|
213
|
+
if (scan.status !== 0) {
|
|
214
|
+
const details = scan.stderr.trim();
|
|
215
|
+
throw new Error(details || 'Knip failed. Check its configuration and try again.');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const plan = collectPlan(extractIssues(scan.stdout));
|
|
219
|
+
displayPlan(plan, manager);
|
|
220
|
+
const total = plan.files.length + plan.exports.length + plan.dependencies.length;
|
|
221
|
+
if (!total || !options.apply) {
|
|
222
|
+
if (total && !options.apply) stdout.write('\nPreview only. Run with --apply to make these changes.\n');
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!options.yes && !(await confirmApply())) {
|
|
227
|
+
stdout.write('No files changed.\n');
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (plan.dependencies.length) {
|
|
232
|
+
const names = [...new Set(plan.dependencies.map(({ name }) => name))];
|
|
233
|
+
stdout.write(`\nRemoving ${names.length} unused package${names.length === 1 ? '' : 's'}…\n`);
|
|
234
|
+
const result = run(manager, managerRemoveArgs(manager, names), directory);
|
|
235
|
+
if (result.status !== 0) throw new Error('Package manager removal failed. Review the current working tree before continuing.');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (plan.files.length || plan.exports.length) {
|
|
239
|
+
stdout.write('\nRemoving dead files and exports with Knip…\n');
|
|
240
|
+
const args = ['--fix', '--no-exit-code', '--fix-type', APPLY_FIX_TYPES.join(',')];
|
|
241
|
+
if (plan.files.length) args.push('--allow-remove-files');
|
|
242
|
+
const result = run(knip.command, [...knip.prefixArgs, ...args], directory);
|
|
243
|
+
if (result.status !== 0) throw new Error('Knip could not apply all code fixes. Review the current working tree before continuing.');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const checksPassed = await runChecks(directory, packageJson);
|
|
247
|
+
stdout.write('\nCleanup applied. Review the result with `git diff` before committing.\n');
|
|
248
|
+
if (!checksPassed) process.exitCode = 1;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
main().catch((error) => {
|
|
252
|
+
console.error(`pruneplan: ${error.message}`);
|
|
253
|
+
process.exitCode = 1;
|
|
254
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pruneplan",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Review and safely apply unused dependency and dead code cleanup plans.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Lakshmi Udbhav Regadamilli",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Udbhav-regadamilli/pruneplan.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Udbhav-regadamilli/pruneplan#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Udbhav-regadamilli/pruneplan/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"bin": "./bin/pruneplan.js",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"unused-dependencies",
|
|
24
|
+
"dead-code",
|
|
25
|
+
"cleanup",
|
|
26
|
+
"knip",
|
|
27
|
+
"cli"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"files": [
|
|
31
|
+
"bin/",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"knip": "^6.47.0"
|
|
37
|
+
}
|
|
38
|
+
}
|