@iyowei/sweep-node-modules-cli 0.2.0 → 0.2.1
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 +2 -4
- package/README.zh-CN.md +2 -4
- package/dist/cli.js +335 -256
- package/dist/manifest.json +3 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
**English** | [中文](README.zh-CN.md)
|
|
10
10
|
|
|
11
|
-
A workspace-level cleaner for `node_modules`: scan several root directories in one pass, list every `node_modules` directory across your projects along with its size, and bulk-delete them after you confirm, reclaiming disk space.
|
|
11
|
+
A workspace-level cleaner for `node_modules`: scan several root directories in one pass, list every `node_modules` directory across your projects along with its size, and bulk-delete them after you confirm, reclaiming disk space. The CLI is a thin shell over the API package it is built on, [`@iyowei/sweep-node-modules`](https://www.npmjs.com/package/@iyowei/sweep-node-modules). For programmatic use from your own scripts and tools, reach for that package (its usage is documented there).
|
|
12
12
|
|
|
13
13
|
> Where this sits: single-project cleaners handle "go into one project and clean its own artifacts"; this tool handles "stand at the workspace level and clean many projects in one pass". The two layers coexist; see [ADR 0001](../../docs/adrs/0001-workspace-level-cleaner.md).
|
|
14
14
|
|
|
@@ -80,11 +80,9 @@ $bin = "$env:USERPROFILE\tools\sweep-node-modules\packages\sweep-node-modules-cl
|
|
|
80
80
|
|
|
81
81
|
> Or skip the command line: add the `bin` directory to your user `Path` under System Properties → Environment Variables.
|
|
82
82
|
|
|
83
|
-
- **Requires**: bun or node, either works (the launcher is a shell / cmd script run by the OS; it does not depend on node)
|
|
83
|
+
- **Requires**: bun or node, either works (the launcher is a shell / cmd script run by the OS; it does not depend on node); **node-only environments**: run one build first (`bun install && bun run build`) — node does not read the repo's path mappings, so an uncovered checkout cannot resolve the API package's artifacts
|
|
84
84
|
- The most direct entry point; afterwards just run `sweep-nm` (on Windows via `packages\sweep-node-modules-cli\bin\sweep-nm.cmd`)
|
|
85
85
|
|
|
86
|
-
> The tool also ships an API package, [`@iyowei/sweep-node-modules`](https://www.npmjs.com/package/@iyowei/sweep-node-modules), for use in your own programs or scripts (usage is documented in that package).
|
|
87
|
-
|
|
88
86
|
## Usage
|
|
89
87
|
|
|
90
88
|
```shell
|
package/README.zh-CN.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
[English](README.md) | **中文**
|
|
10
10
|
|
|
11
|
-
工作区级 `node_modules` 清理工具: 一次扫描多个根目录, 跨项目列出各处 `node_modules` 与体积, 确认后批量删除,
|
|
11
|
+
工作区级 `node_modules` 清理工具: 一次扫描多个根目录, 跨项目列出各处 `node_modules` 与体积, 确认后批量删除, 回收磁盘空间。本 CLI 是 API 包 [`@iyowei/sweep-node-modules`](https://www.npmjs.com/package/@iyowei/sweep-node-modules) 之上的一层薄壳。要在你自己的脚本与工具里做程序化调用, 请用该包 (用法见该包文档)。
|
|
12
12
|
|
|
13
13
|
> 分层说明: 单项目清理工具管「进入某个项目, 清它自己的产物」; 本工具管「站在工作区层面, 一次清理很多个项目」。两者分层共存, 见 [ADR 0001](../../docs/adrs/0001-workspace-level-cleaner.md)。
|
|
14
14
|
|
|
@@ -80,11 +80,9 @@ $bin = "$env:USERPROFILE\tools\sweep-node-modules\packages\sweep-node-modules-cl
|
|
|
80
80
|
|
|
81
81
|
> 也可以不经命令行: 在「系统属性 → 环境变量」里把该 `bin` 目录加到用户变量 `Path` 中。
|
|
82
82
|
|
|
83
|
-
- **需要**: bun 或 node 任一 (启动器是 shell / cmd 脚本, 由系统执行, 不依赖 node)
|
|
83
|
+
- **需要**: bun 或 node 任一 (启动器是 shell / cmd 脚本, 由系统执行, 不依赖 node); **仅 node 的环境**: 需先完成一次构建 (构建链以 bun 为工具: `bun install && bun run build`)——node 不解析仓库内的路径映射, 未经构建的检出态会找不到 API 包产物
|
|
84
84
|
- 特点: 入口最直接; 之后直接敲 `sweep-nm` (Windows 经 `packages\sweep-node-modules-cli\bin\sweep-nm.cmd`)
|
|
85
85
|
|
|
86
|
-
> 本工具同时提供可编程 API 包 [`@iyowei/sweep-node-modules`](https://www.npmjs.com/package/@iyowei/sweep-node-modules), 供在程序或脚本中调用 (用法见该包文档)。
|
|
87
|
-
|
|
88
86
|
## 使用
|
|
89
87
|
|
|
90
88
|
```shell
|
package/dist/cli.js
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
// src/cli.ts
|
|
2
|
-
import { lstat as lstat3, mkdir } from "node:fs/promises";
|
|
2
|
+
import { lstat as lstat3, mkdir, writeFile } from "node:fs/promises";
|
|
3
3
|
import { homedir as homedir6 } from "node:os";
|
|
4
4
|
import { basename, dirname } from "node:path";
|
|
5
|
-
|
|
6
|
-
// ../sweep-node-modules/src/classify.ts
|
|
7
|
-
import { homedir as homedir3 } from "node:os";
|
|
8
|
-
|
|
9
5
|
// ../sweep-node-modules/src/config.ts
|
|
10
6
|
import { readFile } from "node:fs/promises";
|
|
11
7
|
import { homedir } from "node:os";
|
|
12
8
|
import { posix, win32 } from "node:path";
|
|
9
|
+
|
|
10
|
+
// ../sweep-node-modules/src/errors.ts
|
|
11
|
+
var SWEEP_ERROR_CODES = new Set([
|
|
12
|
+
"CONFIG_READ_FAILED",
|
|
13
|
+
"CONFIG_CORRUPT_JSON",
|
|
14
|
+
"CONFIG_CORRUPT_SHAPE",
|
|
15
|
+
"CONFIG_ABSENT",
|
|
16
|
+
"INVALID_ARGUMENT",
|
|
17
|
+
"CANCELLED"
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
class SweepError extends Error {
|
|
21
|
+
name = "SweepError";
|
|
22
|
+
code;
|
|
23
|
+
details;
|
|
24
|
+
constructor(code, message, details) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.details = details;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ../sweep-node-modules/src/config.ts
|
|
13
32
|
var DEFAULT_EXCLUDE = [
|
|
14
33
|
".bun",
|
|
15
34
|
".npm",
|
|
@@ -66,20 +85,29 @@ function describeActual(value) {
|
|
|
66
85
|
function fieldError(container, field, optional) {
|
|
67
86
|
const value = container[field];
|
|
68
87
|
if (value === undefined) {
|
|
69
|
-
return optional ? null : `${field} 应为字符串数组 (实际: 缺失)
|
|
88
|
+
return optional ? null : { field, message: `${field} 应为字符串数组 (实际: 缺失)` };
|
|
70
89
|
}
|
|
71
90
|
if (!Array.isArray(value)) {
|
|
72
|
-
return
|
|
91
|
+
return {
|
|
92
|
+
field,
|
|
93
|
+
message: `${field} 应为字符串数组 (实际: ${describeActual(value)})`
|
|
94
|
+
};
|
|
73
95
|
}
|
|
74
96
|
const badIndex = value.findIndex((item) => typeof item !== "string");
|
|
75
97
|
if (badIndex !== -1) {
|
|
76
|
-
return
|
|
98
|
+
return {
|
|
99
|
+
field,
|
|
100
|
+
message: `${field} 第 ${badIndex + 1} 项应为字符串 (实际: ${describeActual(value[badIndex])})`
|
|
101
|
+
};
|
|
77
102
|
}
|
|
78
103
|
return null;
|
|
79
104
|
}
|
|
80
105
|
function shapeError(value) {
|
|
81
106
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
82
|
-
return
|
|
107
|
+
return {
|
|
108
|
+
field: "顶层",
|
|
109
|
+
message: `顶层应为对象 (实际: ${describeActual(value)})`
|
|
110
|
+
};
|
|
83
111
|
}
|
|
84
112
|
const candidate = value;
|
|
85
113
|
const rootsError = fieldError(candidate, "roots", false);
|
|
@@ -97,17 +125,17 @@ async function loadConfig(path) {
|
|
|
97
125
|
} catch (error) {
|
|
98
126
|
if (error.code === "ENOENT")
|
|
99
127
|
return { state: "absent" };
|
|
100
|
-
throw new
|
|
128
|
+
throw new SweepError("CONFIG_READ_FAILED", `配置读取失败 (${path}): ${error.message}`, { path, errno: error.code });
|
|
101
129
|
}
|
|
102
130
|
let data;
|
|
103
131
|
try {
|
|
104
132
|
data = JSON.parse(text);
|
|
105
133
|
} catch (error) {
|
|
106
|
-
throw new
|
|
134
|
+
throw new SweepError("CONFIG_CORRUPT_JSON", `配置损坏 (${path}): JSON 解析失败: ${error.message}`, { path });
|
|
107
135
|
}
|
|
108
|
-
const
|
|
109
|
-
if (
|
|
110
|
-
throw new
|
|
136
|
+
const problem = shapeError(data);
|
|
137
|
+
if (problem !== null) {
|
|
138
|
+
throw new SweepError("CONFIG_CORRUPT_SHAPE", `配置损坏 (${path}): ${problem.message}`, { path, field: problem.field });
|
|
111
139
|
}
|
|
112
140
|
const raw = data;
|
|
113
141
|
return {
|
|
@@ -122,41 +150,35 @@ async function loadConfig(path) {
|
|
|
122
150
|
async function loadResolvedConfig(resolved) {
|
|
123
151
|
const result = await loadConfig(resolved.path);
|
|
124
152
|
if (result.state === "absent" && resolved.source !== "platform-default") {
|
|
125
|
-
throw new
|
|
153
|
+
throw new SweepError("CONFIG_ABSENT", `配置不存在 (${resolved.path}), 请检查路径`, { path: resolved.path, source: resolved.source });
|
|
126
154
|
}
|
|
127
155
|
return result;
|
|
128
156
|
}
|
|
129
157
|
function mergeNames(configNames, cliNames) {
|
|
130
158
|
return [...new Set([...configNames, ...cliNames])].filter((name) => name !== "node_modules" && name !== ".git");
|
|
131
159
|
}
|
|
132
|
-
|
|
160
|
+
// ../sweep-node-modules/src/display.ts
|
|
161
|
+
function formatBytes(bytes) {
|
|
162
|
+
if (bytes < 1024)
|
|
163
|
+
return `${Math.round(bytes)} B`;
|
|
164
|
+
const units = ["KB", "MB", "GB", "TB"];
|
|
165
|
+
let value = bytes;
|
|
166
|
+
let index = -1;
|
|
167
|
+
do {
|
|
168
|
+
value /= 1024;
|
|
169
|
+
index += 1;
|
|
170
|
+
} while (value >= 1024 && index < units.length - 1);
|
|
171
|
+
return `${Math.round(value * 10) / 10} ${units[index]}`;
|
|
172
|
+
}
|
|
173
|
+
var sanitizeLine = (text) => text.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g, "").replace(/\s+/g, " ").trim();
|
|
174
|
+
var sanitizeOutputLine = (text) => {
|
|
175
|
+
const indent = /^[ \t]*/.exec(text)?.[0] ?? "";
|
|
176
|
+
return `${indent}${sanitizeLine(text)}`;
|
|
177
|
+
};
|
|
133
178
|
// ../sweep-node-modules/src/guard.ts
|
|
134
179
|
import { lstat, realpath, stat } from "node:fs/promises";
|
|
135
180
|
import { homedir as homedir2 } from "node:os";
|
|
136
181
|
import { posix as posix2, win32 as win322 } from "node:path";
|
|
137
|
-
|
|
138
|
-
// ../sweep-node-modules/src/errors.ts
|
|
139
|
-
var SWEEP_ERROR_CODES = new Set([
|
|
140
|
-
"CONFIG_READ_FAILED",
|
|
141
|
-
"CONFIG_CORRUPT_JSON",
|
|
142
|
-
"CONFIG_CORRUPT_SHAPE",
|
|
143
|
-
"CONFIG_ABSENT",
|
|
144
|
-
"INVALID_ARGUMENT",
|
|
145
|
-
"CANCELLED"
|
|
146
|
-
]);
|
|
147
|
-
|
|
148
|
-
class SweepError extends Error {
|
|
149
|
-
name = "SweepError";
|
|
150
|
-
code;
|
|
151
|
-
details;
|
|
152
|
-
constructor(code, message, details) {
|
|
153
|
-
super(message);
|
|
154
|
-
this.code = code;
|
|
155
|
-
this.details = details;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// ../sweep-node-modules/src/guard.ts
|
|
160
182
|
var POSIX_STYLE = {
|
|
161
183
|
name: "posix",
|
|
162
184
|
ops: posix2,
|
|
@@ -394,8 +416,8 @@ async function findCrossDeviceTargets(targets, options) {
|
|
|
394
416
|
function crossDeviceIndex(entries) {
|
|
395
417
|
return new Map(entries.map((entry) => [entry.target, entry.kind]));
|
|
396
418
|
}
|
|
397
|
-
|
|
398
419
|
// ../sweep-node-modules/src/classify.ts
|
|
420
|
+
import { homedir as homedir3 } from "node:os";
|
|
399
421
|
var fold2 = (text, style) => style.caseInsensitive ? text.toLowerCase() : text;
|
|
400
422
|
var INSTALL_TREE_SEGMENTS = new Set([...DEFAULT_EXCLUDE, "extensions", "_npx"].map((name) => name.toLowerCase()));
|
|
401
423
|
function classifyTarget(target, options = {}) {
|
|
@@ -427,6 +449,114 @@ function classifyTarget(target, options = {}) {
|
|
|
427
449
|
}
|
|
428
450
|
return { kind: "project" };
|
|
429
451
|
}
|
|
452
|
+
|
|
453
|
+
// ../sweep-node-modules/src/skip.ts
|
|
454
|
+
var SUSPECT_HINT = "已跳过 (加 --force 一并清理)";
|
|
455
|
+
var CROSS_DEVICE_NOTES = {
|
|
456
|
+
"on-path": "跨设备: 根与目标之间有挂载点",
|
|
457
|
+
"target-itself": "跨设备: 目标本体即挂载点"
|
|
458
|
+
};
|
|
459
|
+
var CROSS_DEVICE_HINTS = {
|
|
460
|
+
"on-path": "已跳过 (把该挂载点声明为独立根即可清理)",
|
|
461
|
+
"target-itself": "已跳过 (挂载点无法删除, 需先卸载该卷)"
|
|
462
|
+
};
|
|
463
|
+
var CROSS_DEVICE_TRAILER_ACTIONS = {
|
|
464
|
+
"on-path": "把根与目标之间的挂载点声明为独立根即可清理",
|
|
465
|
+
"target-itself": "目标本体即挂载点, 需先卸载该卷"
|
|
466
|
+
};
|
|
467
|
+
function crossDeviceNote(kind) {
|
|
468
|
+
return CROSS_DEVICE_NOTES[kind];
|
|
469
|
+
}
|
|
470
|
+
function skipReasonOf(entry, crossDevice, policy) {
|
|
471
|
+
const deviceKind = crossDevice.get(entry.target);
|
|
472
|
+
if (deviceKind !== undefined) {
|
|
473
|
+
return deviceKind === "on-path" ? "cross-device:on-path" : "cross-device:target-itself";
|
|
474
|
+
}
|
|
475
|
+
if (entry.bytes === undefined)
|
|
476
|
+
return "unmeasured";
|
|
477
|
+
if (entry.suspect && !policy.releaseSuspects)
|
|
478
|
+
return "suspect-install-tree";
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
function skipsBatch(entry, crossDevice, policy) {
|
|
482
|
+
return skipReasonOf(entry, crossDevice, policy) !== null;
|
|
483
|
+
}
|
|
484
|
+
function deletionBatch(entries, crossDevice, policy) {
|
|
485
|
+
return entries.filter((entry) => !skipsBatch(entry, crossDevice, policy)).map((entry) => entry.target);
|
|
486
|
+
}
|
|
487
|
+
function noteOf(entry, reason) {
|
|
488
|
+
if (reason === "suspect-install-tree")
|
|
489
|
+
return SUSPECT_HINT;
|
|
490
|
+
if (reason === "unmeasured") {
|
|
491
|
+
return `体积统计失败: ${entry.unmeasuredReason ?? "原因未知"}`;
|
|
492
|
+
}
|
|
493
|
+
return CROSS_DEVICE_HINTS[reason === "cross-device:on-path" ? "on-path" : "target-itself"];
|
|
494
|
+
}
|
|
495
|
+
function collectSkips(entries, crossDevice, policy) {
|
|
496
|
+
const suspectSkipped = policy.releaseSuspects ? [] : entries.filter((entry) => entry.suspect);
|
|
497
|
+
const deviceSkipped = entries.filter((entry) => crossDevice.has(entry.target));
|
|
498
|
+
const trailer = [
|
|
499
|
+
...suspectSkipped.length > 0 ? [`疑似安装树 ${suspectSkipped.length} 处默认跳过 (加 --force 一并清理)`] : [],
|
|
500
|
+
...["on-path", "target-itself"].flatMap((kind) => {
|
|
501
|
+
const count = deviceSkipped.filter((entry) => crossDevice.get(entry.target) === kind).length;
|
|
502
|
+
return count === 0 ? [] : [
|
|
503
|
+
`跨设备目标 ${count} 处默认跳过 (${CROSS_DEVICE_TRAILER_ACTIONS[kind]})`
|
|
504
|
+
];
|
|
505
|
+
})
|
|
506
|
+
];
|
|
507
|
+
const hints = new Map;
|
|
508
|
+
for (const entry of suspectSkipped) {
|
|
509
|
+
if (entry.bytes !== undefined)
|
|
510
|
+
hints.set(entry.target, SUSPECT_HINT);
|
|
511
|
+
}
|
|
512
|
+
for (const entry of deviceSkipped) {
|
|
513
|
+
const kind = crossDevice.get(entry.target);
|
|
514
|
+
if (entry.bytes !== undefined && kind !== undefined) {
|
|
515
|
+
hints.set(entry.target, CROSS_DEVICE_HINTS[kind]);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const book = [];
|
|
519
|
+
for (const entry of entries) {
|
|
520
|
+
const reason = skipReasonOf(entry, crossDevice, policy);
|
|
521
|
+
if (reason === null)
|
|
522
|
+
continue;
|
|
523
|
+
book.push({ target: entry.target, reason, note: noteOf(entry, reason) });
|
|
524
|
+
}
|
|
525
|
+
return { entries: book, trailer, hints };
|
|
526
|
+
}
|
|
527
|
+
// ../sweep-node-modules/src/summary.ts
|
|
528
|
+
var SUCCESS_KINDS = new Set([
|
|
529
|
+
"removed",
|
|
530
|
+
"missing",
|
|
531
|
+
"stale"
|
|
532
|
+
]);
|
|
533
|
+
function isSuccessOutcome(outcome) {
|
|
534
|
+
return SUCCESS_KINDS.has(outcome.kind);
|
|
535
|
+
}
|
|
536
|
+
function summarizeReport(report) {
|
|
537
|
+
const counts = {
|
|
538
|
+
removed: 0,
|
|
539
|
+
missing: 0,
|
|
540
|
+
stale: 0,
|
|
541
|
+
failed: 0,
|
|
542
|
+
rejected: 0,
|
|
543
|
+
"not-attempted": 0,
|
|
544
|
+
"not-expected": 0,
|
|
545
|
+
skipped: 0
|
|
546
|
+
};
|
|
547
|
+
for (const entry of report.entries)
|
|
548
|
+
counts[entry.outcome.kind] += 1;
|
|
549
|
+
return {
|
|
550
|
+
counts,
|
|
551
|
+
succeeded: counts.removed + counts.missing + counts.stale,
|
|
552
|
+
unprocessed: counts.skipped + counts.rejected + counts["not-attempted"] + counts["not-expected"],
|
|
553
|
+
failed: counts.failed,
|
|
554
|
+
aborted: report.removal?.aborted !== undefined
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
// ../sweep-node-modules/src/sweep.ts
|
|
558
|
+
import { homedir as homedir4 } from "node:os";
|
|
559
|
+
|
|
430
560
|
// ../sweep-node-modules/src/delete.ts
|
|
431
561
|
import { lstat as lstat2, realpath as realpath2, rm } from "node:fs/promises";
|
|
432
562
|
import { isAbsolute, join, relative, sep } from "node:path";
|
|
@@ -504,105 +634,185 @@ async function reviewComponents(chain) {
|
|
|
504
634
|
}
|
|
505
635
|
return null;
|
|
506
636
|
}
|
|
637
|
+
var REMOVAL_CONCURRENCY = 4;
|
|
507
638
|
async function removeTargets(targets, options) {
|
|
508
|
-
const result = { removed: [], missing: [], failed: [] };
|
|
509
639
|
const roots = options?.roots ?? [];
|
|
510
640
|
const signal = options?.signal;
|
|
511
641
|
const emit = options?.onProgress;
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
642
|
+
const slots = Array.from({
|
|
643
|
+
length: targets.length
|
|
644
|
+
});
|
|
645
|
+
let aborted;
|
|
646
|
+
let abortedIndex = Number.POSITIVE_INFINITY;
|
|
647
|
+
const recordAbort = (index, info) => {
|
|
648
|
+
if (index >= abortedIndex)
|
|
649
|
+
return;
|
|
650
|
+
aborted = info;
|
|
651
|
+
abortedIndex = index;
|
|
652
|
+
};
|
|
653
|
+
let cancelled = false;
|
|
654
|
+
const failures = [];
|
|
655
|
+
let next = 0;
|
|
656
|
+
let active = 0;
|
|
657
|
+
let resolveDrained = null;
|
|
658
|
+
const drained = new Promise((resolve) => {
|
|
659
|
+
resolveDrained = resolve;
|
|
660
|
+
});
|
|
661
|
+
const halted = () => cancelled || aborted !== undefined;
|
|
662
|
+
function pump() {
|
|
663
|
+
while (active < REMOVAL_CONCURRENCY && next < targets.length && !halted()) {
|
|
664
|
+
const index = next;
|
|
665
|
+
next += 1;
|
|
666
|
+
active += 1;
|
|
667
|
+
removeOne(index);
|
|
529
668
|
}
|
|
530
|
-
if (
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
message: `安全复核失败 (根被替换为符号链接): ${root.configured}`,
|
|
535
|
-
path: root.configured
|
|
536
|
-
};
|
|
537
|
-
emit?.({ kind: "aborted", aborted: result.aborted });
|
|
538
|
-
break;
|
|
669
|
+
if (active === 0 && resolveDrained !== null) {
|
|
670
|
+
const resolve = resolveDrained;
|
|
671
|
+
resolveDrained = null;
|
|
672
|
+
resolve();
|
|
539
673
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
674
|
+
}
|
|
675
|
+
async function removeOne(index) {
|
|
676
|
+
try {
|
|
677
|
+
const target = targets[index];
|
|
678
|
+
if (target === undefined)
|
|
679
|
+
return;
|
|
680
|
+
if (signal?.aborted) {
|
|
681
|
+
cancelled = true;
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
const root = roots.find((candidate) => isUnder(candidate.real, target));
|
|
685
|
+
if (root === undefined) {
|
|
686
|
+
const info = {
|
|
544
687
|
target,
|
|
545
|
-
code: "
|
|
546
|
-
message:
|
|
547
|
-
|
|
688
|
+
code: "REVIEW_NOT_UNDER_ANY_ROOT",
|
|
689
|
+
message: `安全复核失败 (目标不在任何 roots 之下): ${target}`,
|
|
690
|
+
path: target
|
|
548
691
|
};
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
692
|
+
recordAbort(index, info);
|
|
693
|
+
slots[index] = { kind: "aborted" };
|
|
694
|
+
emit?.({ kind: "aborted", aborted: info });
|
|
695
|
+
return;
|
|
552
696
|
}
|
|
553
|
-
if (
|
|
554
|
-
|
|
697
|
+
if (await isSymlinkHead(root.configured)) {
|
|
698
|
+
const info = {
|
|
555
699
|
target,
|
|
556
|
-
code: "
|
|
557
|
-
message: `安全复核失败 (
|
|
558
|
-
path:
|
|
700
|
+
code: "REVIEW_HEAD_SYMLINK",
|
|
701
|
+
message: `安全复核失败 (根被替换为符号链接): ${root.configured}`,
|
|
702
|
+
path: root.configured
|
|
559
703
|
};
|
|
560
|
-
|
|
561
|
-
|
|
704
|
+
recordAbort(index, info);
|
|
705
|
+
slots[index] = { kind: "aborted" };
|
|
706
|
+
emit?.({ kind: "aborted", aborted: info });
|
|
707
|
+
return;
|
|
562
708
|
}
|
|
563
|
-
const
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
709
|
+
const finding = await reviewComponents(componentChain(root.real, target));
|
|
710
|
+
if (finding !== null) {
|
|
711
|
+
if (finding.kind === "vanished") {
|
|
712
|
+
const vanished = {
|
|
713
|
+
target,
|
|
714
|
+
code: "REVIEW_COMPONENT_VANISHED",
|
|
715
|
+
message: describeVanishedReview(target, finding.path),
|
|
716
|
+
partialRisk: false
|
|
717
|
+
};
|
|
718
|
+
slots[index] = { kind: "failed", failure: vanished };
|
|
719
|
+
emit?.({ kind: "failed", failure: vanished });
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
if (finding.kind === "unsafe") {
|
|
723
|
+
const info = {
|
|
724
|
+
target,
|
|
725
|
+
code: "REVIEW_COMPONENT_REPLACED",
|
|
726
|
+
message: `安全复核失败 (路径组件被替换): ${finding.path}`,
|
|
727
|
+
path: finding.path
|
|
728
|
+
};
|
|
729
|
+
recordAbort(index, info);
|
|
730
|
+
slots[index] = { kind: "aborted" };
|
|
731
|
+
emit?.({ kind: "aborted", aborted: info });
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const unverified = {
|
|
735
|
+
target,
|
|
736
|
+
code: "REVIEW_UNVERIFIED",
|
|
737
|
+
message: describeReviewError(target, finding.path, finding.error),
|
|
738
|
+
partialRisk: false
|
|
739
|
+
};
|
|
740
|
+
slots[index] = { kind: "failed", failure: unverified };
|
|
741
|
+
emit?.({ kind: "failed", failure: unverified });
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
try {
|
|
745
|
+
await rm(target, { recursive: true, force: false });
|
|
746
|
+
slots[index] = { kind: "removed", target };
|
|
747
|
+
emit?.({ kind: "removed", target });
|
|
748
|
+
} catch (error) {
|
|
749
|
+
if (error?.code === "ENOENT") {
|
|
750
|
+
const state = await checkGone(target);
|
|
751
|
+
if (state === "gone") {
|
|
752
|
+
slots[index] = { kind: "missing", target };
|
|
753
|
+
emit?.({ kind: "missing", target });
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const survivor = {
|
|
757
|
+
target,
|
|
758
|
+
code: "REMOVE_ENOENT_SURVIVOR",
|
|
759
|
+
message: describeSurvivor(target, state),
|
|
760
|
+
partialRisk: true
|
|
761
|
+
};
|
|
762
|
+
slots[index] = { kind: "failed", failure: survivor };
|
|
763
|
+
emit?.({ kind: "failed", failure: survivor });
|
|
764
|
+
return;
|
|
584
765
|
}
|
|
585
|
-
const
|
|
766
|
+
const failure = {
|
|
586
767
|
target,
|
|
587
|
-
code: "
|
|
588
|
-
|
|
768
|
+
code: "REMOVE_FAILED",
|
|
769
|
+
errno: error?.code,
|
|
770
|
+
message: describeRemovalError(target, error),
|
|
589
771
|
partialRisk: true
|
|
590
772
|
};
|
|
591
|
-
|
|
592
|
-
emit?.({ kind: "failed", failure
|
|
593
|
-
continue;
|
|
773
|
+
slots[index] = { kind: "failed", failure };
|
|
774
|
+
emit?.({ kind: "failed", failure });
|
|
594
775
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
776
|
+
} catch (error) {
|
|
777
|
+
if (failures.length === 0)
|
|
778
|
+
failures.push(error);
|
|
779
|
+
} finally {
|
|
780
|
+
if (signal?.aborted)
|
|
781
|
+
cancelled = true;
|
|
782
|
+
active -= 1;
|
|
783
|
+
pump();
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
pump();
|
|
787
|
+
await drained;
|
|
788
|
+
if (failures.length > 0)
|
|
789
|
+
throw failures[0];
|
|
790
|
+
const result = { removed: [], missing: [], failed: [] };
|
|
791
|
+
for (const slot of slots) {
|
|
792
|
+
if (slot === undefined)
|
|
793
|
+
continue;
|
|
794
|
+
switch (slot.kind) {
|
|
795
|
+
case "removed":
|
|
796
|
+
result.removed.push(slot.target);
|
|
797
|
+
break;
|
|
798
|
+
case "missing":
|
|
799
|
+
result.missing.push(slot.target);
|
|
800
|
+
break;
|
|
801
|
+
case "failed":
|
|
802
|
+
result.failed.push(slot.failure);
|
|
803
|
+
break;
|
|
804
|
+
case "aborted":
|
|
805
|
+
break;
|
|
604
806
|
}
|
|
605
807
|
}
|
|
808
|
+
if (aborted !== undefined)
|
|
809
|
+
result.aborted = aborted;
|
|
810
|
+
if (cancelled) {
|
|
811
|
+
throw new SweepError("CANCELLED", "删除在条目间检查点被取消", {
|
|
812
|
+
phase: "remove",
|
|
813
|
+
partial: result
|
|
814
|
+
});
|
|
815
|
+
}
|
|
606
816
|
return result;
|
|
607
817
|
}
|
|
608
818
|
async function toTrustRoots(roots) {
|
|
@@ -624,19 +834,7 @@ function splitStaleRejections(rejected, staleTargets) {
|
|
|
624
834
|
}
|
|
625
835
|
return { rejected: rest, stale };
|
|
626
836
|
}
|
|
627
|
-
|
|
628
|
-
import { readFile as readFile2, writeFile } from "node:fs/promises";
|
|
629
|
-
var isBun = typeof Bun !== "undefined";
|
|
630
|
-
var versions = process.versions;
|
|
631
|
-
var runtimeName = isBun ? "bun" : "node";
|
|
632
|
-
var runtimeLabel = `${runtimeName} ${versions[runtimeName] ?? "unknown"}`;
|
|
633
|
-
async function writeTextFile(path, text) {
|
|
634
|
-
if (isBun) {
|
|
635
|
-
await Bun.write(path, text);
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
await writeFile(path, text, "utf8");
|
|
639
|
-
}
|
|
837
|
+
|
|
640
838
|
// ../sweep-node-modules/src/scan-parallel.ts
|
|
641
839
|
import { readdir, realpath as realpath3, stat as stat2 } from "node:fs/promises";
|
|
642
840
|
import { join as join2 } from "node:path";
|
|
@@ -1213,112 +1411,8 @@ function createJsSizer() {
|
|
|
1213
1411
|
function createSizer() {
|
|
1214
1412
|
return findDu() !== null ? createDuSizer() : createJsSizer();
|
|
1215
1413
|
}
|
|
1216
|
-
|
|
1217
|
-
var SUSPECT_HINT = "已跳过 (加 --force 一并清理)";
|
|
1218
|
-
var CROSS_DEVICE_NOTES = {
|
|
1219
|
-
"on-path": "跨设备: 根与目标之间有挂载点",
|
|
1220
|
-
"target-itself": "跨设备: 目标本体即挂载点"
|
|
1221
|
-
};
|
|
1222
|
-
var CROSS_DEVICE_HINTS = {
|
|
1223
|
-
"on-path": "已跳过 (把该挂载点声明为独立根即可清理)",
|
|
1224
|
-
"target-itself": "已跳过 (挂载点无法删除, 需先卸载该卷)"
|
|
1225
|
-
};
|
|
1226
|
-
var CROSS_DEVICE_TRAILER_ACTIONS = {
|
|
1227
|
-
"on-path": "把根与目标之间的挂载点声明为独立根即可清理",
|
|
1228
|
-
"target-itself": "目标本体即挂载点, 需先卸载该卷"
|
|
1229
|
-
};
|
|
1230
|
-
function crossDeviceNote(kind) {
|
|
1231
|
-
return CROSS_DEVICE_NOTES[kind];
|
|
1232
|
-
}
|
|
1233
|
-
function skipReasonOf(entry, crossDevice, policy) {
|
|
1234
|
-
const deviceKind = crossDevice.get(entry.target);
|
|
1235
|
-
if (deviceKind !== undefined) {
|
|
1236
|
-
return deviceKind === "on-path" ? "cross-device:on-path" : "cross-device:target-itself";
|
|
1237
|
-
}
|
|
1238
|
-
if (entry.bytes === undefined)
|
|
1239
|
-
return "unmeasured";
|
|
1240
|
-
if (entry.suspect && !policy.releaseSuspects)
|
|
1241
|
-
return "suspect-install-tree";
|
|
1242
|
-
return null;
|
|
1243
|
-
}
|
|
1244
|
-
function skipsBatch(entry, crossDevice, policy) {
|
|
1245
|
-
return skipReasonOf(entry, crossDevice, policy) !== null;
|
|
1246
|
-
}
|
|
1247
|
-
function deletionBatch(entries, crossDevice, policy) {
|
|
1248
|
-
return entries.filter((entry) => !skipsBatch(entry, crossDevice, policy)).map((entry) => entry.target);
|
|
1249
|
-
}
|
|
1250
|
-
function noteOf(entry, reason) {
|
|
1251
|
-
if (reason === "suspect-install-tree")
|
|
1252
|
-
return SUSPECT_HINT;
|
|
1253
|
-
if (reason === "unmeasured") {
|
|
1254
|
-
return `体积统计失败: ${entry.unmeasuredReason ?? "原因未知"}`;
|
|
1255
|
-
}
|
|
1256
|
-
return CROSS_DEVICE_HINTS[reason === "cross-device:on-path" ? "on-path" : "target-itself"];
|
|
1257
|
-
}
|
|
1258
|
-
function collectSkips(entries, crossDevice, policy) {
|
|
1259
|
-
const suspectSkipped = policy.releaseSuspects ? [] : entries.filter((entry) => entry.suspect);
|
|
1260
|
-
const deviceSkipped = entries.filter((entry) => crossDevice.has(entry.target));
|
|
1261
|
-
const trailer = [
|
|
1262
|
-
...suspectSkipped.length > 0 ? [`疑似安装树 ${suspectSkipped.length} 处默认跳过 (加 --force 一并清理)`] : [],
|
|
1263
|
-
...["on-path", "target-itself"].flatMap((kind) => {
|
|
1264
|
-
const count = deviceSkipped.filter((entry) => crossDevice.get(entry.target) === kind).length;
|
|
1265
|
-
return count === 0 ? [] : [
|
|
1266
|
-
`跨设备目标 ${count} 处默认跳过 (${CROSS_DEVICE_TRAILER_ACTIONS[kind]})`
|
|
1267
|
-
];
|
|
1268
|
-
})
|
|
1269
|
-
];
|
|
1270
|
-
const hints = new Map;
|
|
1271
|
-
for (const entry of suspectSkipped) {
|
|
1272
|
-
if (entry.bytes !== undefined)
|
|
1273
|
-
hints.set(entry.target, SUSPECT_HINT);
|
|
1274
|
-
}
|
|
1275
|
-
for (const entry of deviceSkipped) {
|
|
1276
|
-
const kind = crossDevice.get(entry.target);
|
|
1277
|
-
if (entry.bytes !== undefined && kind !== undefined) {
|
|
1278
|
-
hints.set(entry.target, CROSS_DEVICE_HINTS[kind]);
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
const book = [];
|
|
1282
|
-
for (const entry of entries) {
|
|
1283
|
-
const reason = skipReasonOf(entry, crossDevice, policy);
|
|
1284
|
-
if (reason === null)
|
|
1285
|
-
continue;
|
|
1286
|
-
book.push({ target: entry.target, reason, note: noteOf(entry, reason) });
|
|
1287
|
-
}
|
|
1288
|
-
return { entries: book, trailer, hints };
|
|
1289
|
-
}
|
|
1290
|
-
// ../sweep-node-modules/src/summary.ts
|
|
1291
|
-
var SUCCESS_KINDS = new Set([
|
|
1292
|
-
"removed",
|
|
1293
|
-
"missing",
|
|
1294
|
-
"stale"
|
|
1295
|
-
]);
|
|
1296
|
-
function isSuccessOutcome(outcome) {
|
|
1297
|
-
return SUCCESS_KINDS.has(outcome.kind);
|
|
1298
|
-
}
|
|
1299
|
-
function summarizeReport(report) {
|
|
1300
|
-
const counts = {
|
|
1301
|
-
removed: 0,
|
|
1302
|
-
missing: 0,
|
|
1303
|
-
stale: 0,
|
|
1304
|
-
failed: 0,
|
|
1305
|
-
rejected: 0,
|
|
1306
|
-
"not-attempted": 0,
|
|
1307
|
-
"not-expected": 0,
|
|
1308
|
-
skipped: 0
|
|
1309
|
-
};
|
|
1310
|
-
for (const entry of report.entries)
|
|
1311
|
-
counts[entry.outcome.kind] += 1;
|
|
1312
|
-
return {
|
|
1313
|
-
counts,
|
|
1314
|
-
succeeded: counts.removed + counts.missing + counts.stale,
|
|
1315
|
-
unprocessed: counts.skipped + counts.rejected + counts["not-attempted"] + counts["not-expected"],
|
|
1316
|
-
failed: counts.failed,
|
|
1317
|
-
aborted: report.removal?.aborted !== undefined
|
|
1318
|
-
};
|
|
1319
|
-
}
|
|
1414
|
+
|
|
1320
1415
|
// ../sweep-node-modules/src/sweep.ts
|
|
1321
|
-
import { homedir as homedir4 } from "node:os";
|
|
1322
1416
|
var invalidArgument = (field, message) => new SweepError("INVALID_ARGUMENT", `${message} (字段: ${field})`, { field });
|
|
1323
1417
|
function assertNameArray(value, field, required) {
|
|
1324
1418
|
if (value === undefined) {
|
|
@@ -1610,18 +1704,6 @@ var bannerLine = (suffix, color) => paint(`${BAR_BLOCK} ${BANNER} ${suffix}`, "
|
|
|
1610
1704
|
var neutralLine = (text, color) => ` ${paint(`${NEUTRAL_BLOCK} ${text}`, "2", color)}`;
|
|
1611
1705
|
var TIER_BIG = 512 * 1024 ** 2;
|
|
1612
1706
|
var TIER_MID = 100 * 1024 ** 2;
|
|
1613
|
-
function formatBytes(bytes) {
|
|
1614
|
-
if (bytes < 1024)
|
|
1615
|
-
return `${Math.round(bytes)} B`;
|
|
1616
|
-
const units = ["KB", "MB", "GB", "TB"];
|
|
1617
|
-
let value = bytes;
|
|
1618
|
-
let index = -1;
|
|
1619
|
-
do {
|
|
1620
|
-
value /= 1024;
|
|
1621
|
-
index += 1;
|
|
1622
|
-
} while (value >= 1024 && index < units.length - 1);
|
|
1623
|
-
return `${Math.round(value * 10) / 10} ${units[index]}`;
|
|
1624
|
-
}
|
|
1625
1707
|
function render(options) {
|
|
1626
1708
|
const { mode, roots, entries, color, home, releasedBytes, pathStyle } = options;
|
|
1627
1709
|
const style = pathStyle ?? PLATFORM_STYLE;
|
|
@@ -1707,11 +1789,6 @@ function displayPath(target, style, home, keepSuffix = false) {
|
|
|
1707
1789
|
const dir = keepSuffix || !fold3(target, style).endsWith(fold3(suffix, style)) ? target : target.slice(0, -suffix.length);
|
|
1708
1790
|
return shortenPath(dir, style, home);
|
|
1709
1791
|
}
|
|
1710
|
-
var sanitizeLine = (text) => text.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g, "").replace(/\s+/g, " ").trim();
|
|
1711
|
-
var sanitizeOutputLine = (text) => {
|
|
1712
|
-
const indent = /^[ \t]*/.exec(text)?.[0] ?? "";
|
|
1713
|
-
return `${indent}${sanitizeLine(text)}`;
|
|
1714
|
-
};
|
|
1715
1792
|
var padEnd = (text, width) => text + " ".repeat(Math.max(0, width - displayWidth(text)));
|
|
1716
1793
|
var padStart = (text, width) => " ".repeat(Math.max(0, width - displayWidth(text))) + text;
|
|
1717
1794
|
function displayWidth(text) {
|
|
@@ -2022,6 +2099,8 @@ function parseArgs(argv) {
|
|
|
2022
2099
|
options.command = only;
|
|
2023
2100
|
return { ok: true, options };
|
|
2024
2101
|
}
|
|
2102
|
+
var RUNTIME_NAME = typeof Bun === "undefined" ? "node" : "bun";
|
|
2103
|
+
var runtimeLabel = `${RUNTIME_NAME} ${process.versions[RUNTIME_NAME] ?? "unknown"}`;
|
|
2025
2104
|
function colorEnabled() {
|
|
2026
2105
|
return process.stdout.isTTY === true && !process.env.NO_COLOR;
|
|
2027
2106
|
}
|
|
@@ -2058,7 +2137,7 @@ function runWizard(configPath, color) {
|
|
|
2058
2137
|
firstSymlinkOnTarget,
|
|
2059
2138
|
async writeFile(path, text) {
|
|
2060
2139
|
await mkdir(dirname(path), { recursive: true });
|
|
2061
|
-
await
|
|
2140
|
+
await writeFile(path, text, "utf8");
|
|
2062
2141
|
},
|
|
2063
2142
|
io: createReadlineIO(color)
|
|
2064
2143
|
});
|
package/dist/manifest.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
|
-
"commit": "
|
|
3
|
+
"commit": "649c1f4128c950d192f7225458f54cd8071a4e2d",
|
|
4
4
|
"dirty": false,
|
|
5
|
-
"cliSha256": "
|
|
6
|
-
"builtAt": "2026-09-
|
|
5
|
+
"cliSha256": "2582a554e1cd9c141f86485dbb1d361a9243d9b71b2dc816b29fb1b2e3cfbc82",
|
|
6
|
+
"builtAt": "2026-09-29T15:02:19.360Z"
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iyowei/sweep-node-modules-cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "CLI to find and delete node_modules across projects: scan multiple workspace roots (monorepo friendly), bulk-remove them to free up disk space, preview first and delete after confirmation. 工作区级 node_modules 批量清理工具。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"registry": "https://registry.npmjs.org/"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@iyowei/sweep-node-modules": "
|
|
60
|
+
"@iyowei/sweep-node-modules": "0.5.0"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "bun build src/cli.ts --target=node --outfile=dist/cli.js && bun scripts/write-dist-manifest.ts",
|