pruneplan 0.1.2 → 0.1.4

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/README.md CHANGED
@@ -36,6 +36,8 @@ Use `--json` to print a machine-readable report, including warnings and Knip ver
36
36
  npx pruneplan --json
37
37
  ```
38
38
 
39
+ The JSON report is written to stdout without progress text and starts with `schemaVersion: 1`. Consumers can use this field to check the report format before reading `knip`, `candidates`, and `warnings`. Any errors are written to stderr.
40
+
39
41
  ## Review and safety
40
42
 
41
43
  - The default command is preview-only and does not modify files.
@@ -0,0 +1,21 @@
1
+ import spawn from 'cross-spawn';
2
+
3
+ export function run(command, args, cwd, { capture = false } = {}) {
4
+ const result = spawn.sync(command, args, {
5
+ cwd, encoding: 'utf8',
6
+ stdio: capture ? ['ignore', 'pipe', 'pipe'] : 'inherit',
7
+ shell: false,
8
+ windowsHide: true,
9
+ });
10
+ if (result.error) throw new Error(`${command} could not be started: ${result.error.message}`);
11
+ return result;
12
+ }
13
+
14
+ export function runWindowsShim(command, args, cwd, { capture = false } = {}) {
15
+ return run(command, args, cwd, { capture });
16
+ }
17
+
18
+ export function runPackageManager(manager, args, cwd, options) {
19
+ if (process.platform === 'win32') return runWindowsShim(`${manager}.cmd`, args, cwd, options);
20
+ return run(manager, args, cwd, options);
21
+ }
package/bin/pruneplan.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { spawnSync } from 'node:child_process';
4
3
  import { createInterface } from 'node:readline/promises';
5
4
  import { stdin, stdout } from 'node:process';
6
5
  import { access, lstat, readFile, rm } from 'node:fs/promises';
7
6
  import path from 'node:path';
8
7
  import { fileURLToPath } from 'node:url';
8
+ import { run, runPackageManager, runWindowsShim } from './process-runner.js';
9
9
 
10
10
  const CHECK_SCRIPTS = ['typecheck', 'lint', 'test', 'build'];
11
11
  const AUTO_CATEGORIES = new Set(['files', 'exports', 'types', 'enumMembers', 'namespaceMembers', 'dependencies', 'devDependencies']);
@@ -46,18 +46,6 @@ async function detectPackageManager(directory, packageJson) {
46
46
  throw new Error('Could not detect the package manager. Add a lockfile or packageManager field to package.json.');
47
47
  }
48
48
 
49
- function run(command, args, cwd, { capture = false } = {}) {
50
- const result = spawnSync(command, args, {
51
- cwd, encoding: 'utf8',
52
- stdio: capture ? ['ignore', 'pipe', 'pipe'] : 'inherit',
53
- // Windows package-manager and global Knip commands are commonly exposed as .cmd shims.
54
- shell: process.platform === 'win32',
55
- windowsHide: true,
56
- });
57
- if (result.error) throw new Error(`${command} could not be started: ${result.error.message}`);
58
- return result;
59
- }
60
-
61
49
  async function findKnip(directory) {
62
50
  let current = directory;
63
51
  while (true) {
@@ -69,7 +57,9 @@ async function findKnip(directory) {
69
57
  }
70
58
  const bundled = fileURLToPath(new URL('../node_modules/knip/bin/knip.js', import.meta.url));
71
59
  try { await access(bundled); return { command: process.execPath, prefixArgs: [bundled], source: 'bundled' }; } catch {}
72
- return { command: 'knip', prefixArgs: [], source: 'PATH' };
60
+ return process.platform === 'win32'
61
+ ? { command: 'knip.cmd', prefixArgs: [], source: 'PATH', windowsShim: true }
62
+ : { command: 'knip', prefixArgs: [], source: 'PATH' };
73
63
  }
74
64
 
75
65
  function extractIssues(output) {
@@ -204,14 +194,17 @@ async function runChecks(directory, packageJson) {
204
194
  const manager = await detectPackageManager(directory, packageJson);
205
195
  stdout.write(`\nRunning checks with ${manager}: ${available.join(', ')}\n`);
206
196
  for (const name of available) {
207
- const result = run(manager, ['run', name], directory);
197
+ const result = runPackageManager(manager, ['run', name], directory);
208
198
  if (result.status !== 0) { stdout.write(`\nValidation failed at \`${name}\`. Changes remain in the working tree; inspect with git diff.\n`); return false; }
209
199
  }
210
200
  return true;
211
201
  }
212
202
 
213
203
  async function scanProject(directory, knip) {
214
- const result = run(knip.command, [...knip.prefixArgs, '--reporter', 'json', '--no-exit-code'], directory, { capture: true });
204
+ const args = [...knip.prefixArgs, '--reporter', 'json', '--no-exit-code'];
205
+ const result = knip.windowsShim
206
+ ? runWindowsShim(knip.command, args, directory, { capture: true })
207
+ : run(knip.command, args, directory, { capture: true });
215
208
  if (result.status !== 0) throw new Error(result.stderr.trim() || 'Knip scan failed. Check its configuration and try again.');
216
209
  return extractIssues(result.stdout);
217
210
  }
@@ -235,11 +228,11 @@ async function main() {
235
228
  const knip = await findKnip(directory);
236
229
  knip.version = await getKnipVersion(directory);
237
230
 
238
- stdout.write(`Scanning ${path.relative(process.cwd(), directory) || '.'} with Knip…\n`);
231
+ if (!options.json) stdout.write(`Scanning ${path.relative(process.cwd(), directory) || '.'} with Knip…\n`);
239
232
  const report = await scanProject(directory, knip);
240
233
  const plan = collectPlan(report);
241
234
  if (options.json) {
242
- stdout.write(`${JSON.stringify({ knip: { version: knip.version, source: knip.source }, ...plan }, null, 2)}\n`);
235
+ stdout.write(`${JSON.stringify({ schemaVersion: 1, knip: { version: knip.version, source: knip.source }, ...plan }, null, 2)}\n`);
243
236
  return;
244
237
  }
245
238
  displayPlan(plan, manager, knip);
@@ -278,7 +271,7 @@ async function main() {
278
271
  const packages = [...new Set(selected.filter((item) => item.kind === 'dependency').map((item) => item.name))];
279
272
  if (packages.length) {
280
273
  stdout.write(`\nRemoving selected packages with ${manager}: ${packages.join(', ')}\n`);
281
- const result = run(manager, managerRemoveArgs(manager, packages), directory);
274
+ const result = runPackageManager(manager, managerRemoveArgs(manager, packages), directory);
282
275
  if (result.status !== 0) throw new Error('Package manager removal failed. Review the current working tree before continuing.');
283
276
  }
284
277
  const checksPassed = await runChecks(directory, packageJson);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pruneplan",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Review and safely apply unused dependency and dead code cleanup plans.",
5
5
  "type": "module",
6
6
  "author": "Lakshmi Udbhav Regadamilli",
@@ -29,12 +29,16 @@
29
29
  "cli"
30
30
  ],
31
31
  "license": "MIT",
32
+ "scripts": {
33
+ "test": "node --test"
34
+ },
32
35
  "files": [
33
36
  "bin/",
34
37
  "README.md",
35
38
  "LICENSE"
36
39
  ],
37
40
  "dependencies": {
41
+ "cross-spawn": "^7.0.6",
38
42
  "knip": "^6.47.0"
39
43
  }
40
44
  }