sweepwise 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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +114 -2
- package/README.zh-CN.md +100 -0
- package/bin/sweepwise.js +14 -0
- package/package.json +46 -4
- package/src/clean.js +217 -0
- package/src/cli.js +175 -0
- package/src/detectors/bigfiles.js +118 -0
- package/src/detectors/browsers.js +182 -0
- package/src/detectors/caches.js +151 -0
- package/src/detectors/docker.js +71 -0
- package/src/detectors/ignored.js +93 -0
- package/src/detectors/leftovers.js +163 -0
- package/src/detectors/nodeModules.js +94 -0
- package/src/detectors/worktrees.js +176 -0
- package/src/detectors/xcode.js +136 -0
- package/src/i18n.js +32 -0
- package/src/model.js +49 -0
- package/src/report.js +109 -0
- package/src/repos.js +107 -0
- package/src/scan.js +122 -0
- package/src/util.js +238 -0
package/src/util.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { tr, plural } from './i18n.js';
|
|
7
|
+
|
|
8
|
+
const execFileP = promisify(execFile);
|
|
9
|
+
|
|
10
|
+
export const HOME = os.homedir();
|
|
11
|
+
export const home = (...parts) => path.join(HOME, ...parts);
|
|
12
|
+
|
|
13
|
+
/** 运行外部命令,从不抛异常。 */
|
|
14
|
+
export async function run(cmd, args = [], opts = {}) {
|
|
15
|
+
try {
|
|
16
|
+
const { stdout, stderr } = await execFileP(cmd, args, {
|
|
17
|
+
maxBuffer: 512 * 1024 * 1024,
|
|
18
|
+
timeout: opts.timeout ?? 180_000,
|
|
19
|
+
cwd: opts.cwd,
|
|
20
|
+
env: opts.env ? { ...process.env, ...opts.env } : process.env,
|
|
21
|
+
});
|
|
22
|
+
return { ok: true, code: 0, stdout, stderr };
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return {
|
|
25
|
+
ok: false,
|
|
26
|
+
code: typeof e.code === 'number' ? e.code : 1,
|
|
27
|
+
stdout: e.stdout ?? '',
|
|
28
|
+
stderr: e.stderr ?? String(e.message),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function git(cwd, args, opts = {}) {
|
|
34
|
+
return run('git', ['--no-optional-locks', '-C', cwd, ...args], opts);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const whichCache = new Map();
|
|
38
|
+
export async function which(cmd) {
|
|
39
|
+
if (whichCache.has(cmd)) return whichCache.get(cmd);
|
|
40
|
+
let found = null;
|
|
41
|
+
for (const dir of (process.env.PATH || '').split(':')) {
|
|
42
|
+
if (!dir) continue;
|
|
43
|
+
const p = path.join(dir, cmd);
|
|
44
|
+
try {
|
|
45
|
+
await fs.access(p, fs.constants.X_OK);
|
|
46
|
+
found = p;
|
|
47
|
+
break;
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
whichCache.set(cmd, found);
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function exists(p) {
|
|
55
|
+
try {
|
|
56
|
+
await fs.lstat(p);
|
|
57
|
+
return true;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function statSafe(p) {
|
|
64
|
+
try {
|
|
65
|
+
return await fs.stat(p);
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function readdirSafe(p) {
|
|
72
|
+
try {
|
|
73
|
+
return await fs.readdir(p);
|
|
74
|
+
} catch {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 列出目录的直接子项(完整路径)。 */
|
|
80
|
+
export async function children(dir) {
|
|
81
|
+
return (await readdirSafe(dir)).filter((n) => n !== '.DS_Store').map((n) => path.join(dir, n));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 用 du 计算占用的磁盘空间(字节)。 */
|
|
85
|
+
export async function duBytes(p) {
|
|
86
|
+
const r = await run('du', ['-sk', p], { timeout: 600_000 });
|
|
87
|
+
const first = r.stdout.trim().split('\n').pop() || '';
|
|
88
|
+
const kb = parseInt(first.split('\t')[0], 10);
|
|
89
|
+
return Number.isFinite(kb) ? kb * 1024 : 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function duMany(paths, concurrency = 6) {
|
|
93
|
+
const sizes = await pMap(paths, (p) => duBytes(p), concurrency);
|
|
94
|
+
return sizes.reduce((a, b) => a + b, 0);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function pMap(items, fn, concurrency = 6) {
|
|
98
|
+
const results = new Array(items.length);
|
|
99
|
+
let next = 0;
|
|
100
|
+
async function worker() {
|
|
101
|
+
while (next < items.length) {
|
|
102
|
+
const i = next++;
|
|
103
|
+
results[i] = await fn(items[i], i);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, worker));
|
|
107
|
+
return results;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function isRunning(pattern) {
|
|
111
|
+
const r = await run('pgrep', ['-f', pattern], { timeout: 5000 });
|
|
112
|
+
return r.ok && r.stdout.trim().length > 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 文件是否被某个进程打开着,返回 PID 列表。 */
|
|
116
|
+
export async function openBy(file) {
|
|
117
|
+
const r = await run('lsof', ['-t', '--', file], { timeout: 15000 });
|
|
118
|
+
return r.stdout.trim().split('\n').filter(Boolean);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function freeSpace() {
|
|
122
|
+
const r = await run('df', ['-k', HOME]);
|
|
123
|
+
const line = r.stdout.trim().split('\n').pop() || '';
|
|
124
|
+
const cols = line.split(/\s+/);
|
|
125
|
+
return { total: Number(cols[1]) * 1024, used: Number(cols[2]) * 1024, free: Number(cols[3]) * 1024 };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ---------- 格式化 ----------
|
|
129
|
+
|
|
130
|
+
export function formatBytes(n) {
|
|
131
|
+
if (!n || n < 0) return '0 B';
|
|
132
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
133
|
+
let i = 0;
|
|
134
|
+
let v = n;
|
|
135
|
+
while (v >= 1000 && i < units.length - 1) {
|
|
136
|
+
v /= 1000;
|
|
137
|
+
i++;
|
|
138
|
+
}
|
|
139
|
+
return `${v >= 100 || i === 0 ? v.toFixed(0) : v.toFixed(1)} ${units[i]}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function parseSize(s) {
|
|
143
|
+
const m = String(s).trim().match(/^([\d.]+)\s*([kmgt]?)b?$/i);
|
|
144
|
+
if (!m) throw new Error(tr(`无法识别的大小:${s}`, `Can't parse size: ${s}`));
|
|
145
|
+
const mult = { '': 1, k: 1e3, m: 1e6, g: 1e9, t: 1e12 }[m[2].toLowerCase()];
|
|
146
|
+
return Math.round(parseFloat(m[1]) * mult);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function daysSince(ms) {
|
|
150
|
+
if (!ms) return null;
|
|
151
|
+
return Math.max(0, Math.floor((Date.now() - ms) / 86_400_000));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function ago(ms) {
|
|
155
|
+
const d = daysSince(ms);
|
|
156
|
+
if (d === null) return tr('时间未知', 'at an unknown time');
|
|
157
|
+
if (d === 0) {
|
|
158
|
+
const h = Math.floor((Date.now() - ms) / 3_600_000);
|
|
159
|
+
return h <= 0 ? tr('刚刚', 'just now') : tr(`${h} 小时前`, `${plural(h, 'hour')} ago`);
|
|
160
|
+
}
|
|
161
|
+
if (d < 60) return tr(`${d} 天前`, `${plural(d, 'day')} ago`);
|
|
162
|
+
if (d < 730) {
|
|
163
|
+
const m = Math.floor(d / 30);
|
|
164
|
+
return tr(`${m} 个月前`, `${plural(m, 'month')} ago`);
|
|
165
|
+
}
|
|
166
|
+
const y = Math.floor(d / 365);
|
|
167
|
+
return tr(`${y} 年前`, `${plural(y, 'year')} ago`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function tildify(p) {
|
|
171
|
+
return p.startsWith(HOME + '/') ? '~' + p.slice(HOME.length) : p;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function shellQuote(s) {
|
|
175
|
+
return /^[\w@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, `'\\''`)}'`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function isInside(child, parent) {
|
|
179
|
+
return child !== parent && child.startsWith(parent.endsWith('/') ? parent : parent + '/');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---------- 终端颜色与进度 ----------
|
|
183
|
+
|
|
184
|
+
let colorOn = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
185
|
+
export function setColor(on) {
|
|
186
|
+
colorOn = on;
|
|
187
|
+
}
|
|
188
|
+
const wrap = (code) => (s) => (colorOn ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
189
|
+
export const c = {
|
|
190
|
+
bold: wrap('1'),
|
|
191
|
+
dim: wrap('2'),
|
|
192
|
+
red: wrap('31'),
|
|
193
|
+
green: wrap('32'),
|
|
194
|
+
yellow: wrap('33'),
|
|
195
|
+
blue: wrap('34'),
|
|
196
|
+
cyan: wrap('36'),
|
|
197
|
+
gray: wrap('90'),
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
let progressShown = false;
|
|
201
|
+
export function progress(msg) {
|
|
202
|
+
if (!process.stderr.isTTY) return;
|
|
203
|
+
const cols = process.stderr.columns || 80;
|
|
204
|
+
process.stderr.write('\r\x1b[2K' + c.gray(truncateWidth(msg, cols - 1)));
|
|
205
|
+
progressShown = true;
|
|
206
|
+
}
|
|
207
|
+
export function progressDone() {
|
|
208
|
+
if (progressShown && process.stderr.isTTY) process.stderr.write('\r\x1b[2K');
|
|
209
|
+
progressShown = false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** 中文按 2 列宽计算。 */
|
|
213
|
+
export function strWidth(s) {
|
|
214
|
+
let w = 0;
|
|
215
|
+
for (const ch of String(s).replace(/\x1b\[[\d;]*m/g, '')) {
|
|
216
|
+
const cp = ch.codePointAt(0);
|
|
217
|
+
w += cp > 0x2e80 && !(cp >= 0xff61 && cp <= 0xff9f) ? 2 : 1;
|
|
218
|
+
}
|
|
219
|
+
return w;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function truncateWidth(s, max) {
|
|
223
|
+
if (strWidth(s) <= max) return s;
|
|
224
|
+
let out = '';
|
|
225
|
+
let w = 0;
|
|
226
|
+
for (const ch of s) {
|
|
227
|
+
const cw = strWidth(ch);
|
|
228
|
+
if (w + cw > max - 1) break;
|
|
229
|
+
out += ch;
|
|
230
|
+
w += cw;
|
|
231
|
+
}
|
|
232
|
+
return out + '…';
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function padStart(s, width) {
|
|
236
|
+
const w = strWidth(s);
|
|
237
|
+
return w >= width ? s : ' '.repeat(width - w) + s;
|
|
238
|
+
}
|