devclean-cache 0.0.0-stage → 0.5.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.
@@ -0,0 +1,71 @@
1
+ const UNITS = ["B", "KB", "MB", "GB", "TB", "PB"];
2
+ /** Human-readable byte size, e.g. 1536 -> "1.5 KB". */
3
+ export function formatBytes(bytes, digits = 1) {
4
+ if (!Number.isFinite(bytes) || bytes <= 0)
5
+ return "0 B";
6
+ let value = bytes;
7
+ let unit = 0;
8
+ while (value >= 1024 && unit < UNITS.length - 1) {
9
+ value /= 1024;
10
+ unit += 1;
11
+ }
12
+ const decimals = unit === 0 || value >= 100 ? 0 : digits;
13
+ return `${value.toFixed(decimals)} ${UNITS[unit]}`;
14
+ }
15
+ export function formatCount(count) {
16
+ if (!Number.isFinite(count) || count <= 0)
17
+ return "0";
18
+ if (count < 1000)
19
+ return String(count);
20
+ if (count < 1_000_000)
21
+ return `${(count / 1000).toFixed(1)}k`;
22
+ return `${(count / 1_000_000).toFixed(1)}M`;
23
+ }
24
+ /** Shorten a long path from the middle: C:\\Users\\me\\AppData\\...\\_cacache */
25
+ export function truncateMiddle(text, max) {
26
+ if (text.length <= max)
27
+ return text;
28
+ if (max <= 3)
29
+ return text.slice(0, max);
30
+ const keep = max - 3;
31
+ const head = Math.ceil(keep * 0.45);
32
+ const tail = keep - head;
33
+ return `${text.slice(0, head)}...${text.slice(text.length - tail)}`;
34
+ }
35
+ /**
36
+ * Shorten a long path, preferring the tail: C:\Users\me\AppData\...\pnpm\store
37
+ * becomes ...\AppData\Local\pnpm\store so the distinguishing end stays visible.
38
+ */
39
+ export function truncatePath(text, max) {
40
+ if (text.length <= max)
41
+ return text;
42
+ if (max <= 4)
43
+ return text.slice(text.length - max);
44
+ const sep = text.includes("\\") ? "\\" : "/";
45
+ const parts = text.split(/[\\/]/).filter((part) => part.length > 0);
46
+ if (parts.length === 0)
47
+ return text.slice(text.length - max);
48
+ let tail = parts[parts.length - 1];
49
+ for (let i = parts.length - 2; i >= 0; i -= 1) {
50
+ const next = parts[i] + sep + tail;
51
+ if (next.length + 2 > max)
52
+ break;
53
+ tail = next;
54
+ }
55
+ if (tail.length + 2 > max)
56
+ return "\u2026" + tail.slice(tail.length - (max - 1));
57
+ return "\u2026" + sep + tail;
58
+ }
59
+ export function percent(part, whole) {
60
+ if (!whole)
61
+ return 0;
62
+ return Math.min(100, Math.max(0, (part / whole) * 100));
63
+ }
64
+ /** A 20-cell unicode bar. */
65
+ export function bar(share, width = 16) {
66
+ const filled = Math.round((share / 100) * width);
67
+ return "█".repeat(filled) + "░".repeat(Math.max(0, width - filled));
68
+ }
69
+ export function plural(n, word) {
70
+ return `${n} ${word}${n === 1 ? "" : "s"}`;
71
+ }
@@ -0,0 +1,37 @@
1
+ export interface SizeResult {
2
+ bytes: number;
3
+ files: number;
4
+ dirs: number;
5
+ errors: number;
6
+ }
7
+ export declare function pathExists(target: string): Promise<boolean>;
8
+ export declare function isDirectory(target: string): Promise<boolean>;
9
+ /**
10
+ * Walk a directory tree without following symlinks and sum file sizes.
11
+ * Uses a breadth-first queue so very deep trees cannot blow the call stack.
12
+ */
13
+ export declare function dirSize(root: string, batch?: number): Promise<SizeResult>;
14
+ /** Delete a path recursively. Returns an error message instead of throwing. */
15
+ export declare function removePath(target: string): Promise<string | null>;
16
+ export declare function ensureDir(target: string): Promise<void>;
17
+ /** Directory names considered reclaimable inside a project tree. */
18
+ export declare const PROJECT_ARTIFACTS: Set<string>;
19
+ /** Files that mark a directory as a project root. */
20
+ export declare const PROJECT_MARKERS: Set<string>;
21
+ /** Directories that are never worth descending into. */
22
+ export declare const SYSTEM_DIRS: Set<string>;
23
+ export interface ProjectHit {
24
+ path: string;
25
+ name: string;
26
+ root: string;
27
+ bytes: number;
28
+ files: number;
29
+ }
30
+ /**
31
+ * Find package/build directories inside real projects under `root`.
32
+ * A directory counts as a project only when it contains a known project
33
+ * marker (package.json, .git, Cargo.toml, ...), which keeps caches that
34
+ * belong to the OS or to Python's standard library out of the report.
35
+ * Does not descend into an artifact once it matches.
36
+ */
37
+ export declare function findProjectArtifacts(root: string, maxDepth?: number, onProgress?: (visited: number) => void): Promise<ProjectHit[]>;
@@ -0,0 +1,192 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ const EMPTY = { bytes: 0, files: 0, dirs: 0, errors: 0 };
4
+ export async function pathExists(target) {
5
+ try {
6
+ await fs.lstat(target);
7
+ return true;
8
+ }
9
+ catch {
10
+ return false;
11
+ }
12
+ }
13
+ export async function isDirectory(target) {
14
+ try {
15
+ const st = await fs.stat(target);
16
+ return st.isDirectory();
17
+ }
18
+ catch {
19
+ return false;
20
+ }
21
+ }
22
+ async function statBatch(files, result) {
23
+ const CONCURRENCY = 64;
24
+ for (let i = 0; i < files.length; i += CONCURRENCY) {
25
+ const slice = files.slice(i, i + CONCURRENCY);
26
+ const sizes = await Promise.all(slice.map(async (file) => {
27
+ try {
28
+ const st = await fs.stat(file);
29
+ return st.isFile() ? st.size : 0;
30
+ }
31
+ catch {
32
+ result.errors += 1;
33
+ return 0;
34
+ }
35
+ }));
36
+ for (const size of sizes) {
37
+ result.bytes += size;
38
+ result.files += 1;
39
+ }
40
+ }
41
+ }
42
+ /**
43
+ * Walk a directory tree without following symlinks and sum file sizes.
44
+ * Uses a breadth-first queue so very deep trees cannot blow the call stack.
45
+ */
46
+ export async function dirSize(root, batch = 24) {
47
+ const result = { ...EMPTY };
48
+ let pending = [root];
49
+ while (pending.length > 0) {
50
+ const chunk = pending.splice(0, batch);
51
+ const listings = await Promise.all(chunk.map(async (dir) => {
52
+ try {
53
+ return await fs.readdir(dir, { withFileTypes: true });
54
+ }
55
+ catch {
56
+ result.errors += 1;
57
+ return [];
58
+ }
59
+ }));
60
+ for (let i = 0; i < chunk.length; i += 1) {
61
+ result.dirs += 1;
62
+ const dir = chunk[i];
63
+ const files = [];
64
+ for (const entry of listings[i]) {
65
+ const full = path.join(dir, entry.name);
66
+ if (entry.isSymbolicLink())
67
+ continue;
68
+ if (entry.isDirectory()) {
69
+ pending.push(full);
70
+ }
71
+ else if (entry.isFile()) {
72
+ files.push(full);
73
+ }
74
+ }
75
+ if (files.length > 0)
76
+ await statBatch(files, result);
77
+ }
78
+ }
79
+ return result;
80
+ }
81
+ /** Delete a path recursively. Returns an error message instead of throwing. */
82
+ export async function removePath(target) {
83
+ try {
84
+ await fs.rm(target, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
85
+ return null;
86
+ }
87
+ catch (error) {
88
+ return error instanceof Error ? error.message : String(error);
89
+ }
90
+ }
91
+ export async function ensureDir(target) {
92
+ await fs.mkdir(target, { recursive: true });
93
+ }
94
+ /** Directory names considered reclaimable inside a project tree. */
95
+ export const PROJECT_ARTIFACTS = new Set([
96
+ "node_modules",
97
+ ".venv",
98
+ "venv",
99
+ "__pycache__",
100
+ "target",
101
+ ".next",
102
+ ".nuxt",
103
+ ".turbo",
104
+ ".parcel-cache",
105
+ ".pytest_cache",
106
+ ".mypy_cache",
107
+ ".ruff_cache",
108
+ ]);
109
+ /** Files that mark a directory as a project root. */
110
+ export const PROJECT_MARKERS = new Set([
111
+ "package.json",
112
+ "pyproject.toml",
113
+ "Cargo.toml",
114
+ "go.mod",
115
+ "pom.xml",
116
+ "build.gradle",
117
+ "build.gradle.kts",
118
+ "composer.json",
119
+ "Gemfile",
120
+ "requirements.txt",
121
+ ".git",
122
+ ]);
123
+ /** Directories that are never worth descending into. */
124
+ export const SYSTEM_DIRS = new Set([
125
+ "AppData",
126
+ "Application Data",
127
+ "Windows",
128
+ "Program Files",
129
+ "Program Files (x86)",
130
+ "ProgramData",
131
+ "$Recycle.Bin",
132
+ "System Volume Information",
133
+ "node_modules",
134
+ "site-packages",
135
+ "Library",
136
+ "Caches",
137
+ ]);
138
+ /**
139
+ * Find package/build directories inside real projects under `root`.
140
+ * A directory counts as a project only when it contains a known project
141
+ * marker (package.json, .git, Cargo.toml, ...), which keeps caches that
142
+ * belong to the OS or to Python's standard library out of the report.
143
+ * Does not descend into an artifact once it matches.
144
+ */
145
+ export async function findProjectArtifacts(root, maxDepth = 6, onProgress) {
146
+ const hits = [];
147
+ const queue = [
148
+ { dir: root, depth: 0, projectRoot: null },
149
+ ];
150
+ let visited = 0;
151
+ while (queue.length > 0) {
152
+ const chunk = queue.splice(0, 16);
153
+ const listings = await Promise.all(chunk.map(async ({ dir }) => {
154
+ try {
155
+ return await fs.readdir(dir, { withFileTypes: true });
156
+ }
157
+ catch {
158
+ return [];
159
+ }
160
+ }));
161
+ for (let i = 0; i < chunk.length; i += 1) {
162
+ const { dir, depth, projectRoot } = chunk[i];
163
+ visited += 1;
164
+ if (onProgress && visited % 50 === 0)
165
+ onProgress(visited);
166
+ if (depth >= maxDepth)
167
+ continue;
168
+ const entries = listings[i];
169
+ const hasMarker = entries.some((entry) => PROJECT_MARKERS.has(entry.name));
170
+ const currentRoot = projectRoot ?? (hasMarker ? dir : null);
171
+ const next = [];
172
+ for (const entry of entries) {
173
+ if (!entry.isDirectory())
174
+ continue;
175
+ if (entry.isSymbolicLink())
176
+ continue;
177
+ const full = path.join(dir, entry.name);
178
+ if (PROJECT_ARTIFACTS.has(entry.name)) {
179
+ if (currentRoot)
180
+ hits.push({ path: full, name: entry.name, root: currentRoot, bytes: 0, files: 0 });
181
+ }
182
+ else if (!entry.name.startsWith(".") &&
183
+ !SYSTEM_DIRS.has(entry.name) &&
184
+ entry.name !== "__pycache__") {
185
+ next.push({ dir: full, depth: depth + 1, projectRoot: currentRoot });
186
+ }
187
+ }
188
+ queue.push(...next);
189
+ }
190
+ }
191
+ return hits;
192
+ }
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
- {
1
+ {
2
2
  "name": "devclean-cache",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.5.0",
4
+ "description": "Find and reclaim the gigabytes your dev tools are hoarding. Install as devclean-cache, run as devclean.",
5
+ "type": "module",
6
+ "bin": {
7
+ "devclean": "dist/cli.js"
8
+ },
9
+ "exports": {
10
+ ".": "./dist/index.js"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "README.md",
15
+ "README.zh-CN.md",
16
+ "CHANGELOG.md",
17
+ "SECURITY.md",
18
+ "LICENSE"
19
+ ],
20
+ "engines": {
21
+ "node": ">=20"
22
+ },
23
+ "scripts": {
24
+ "build": "tsc -p tsconfig.json",
25
+ "dev": "node src/cli.ts",
26
+ "test": "node --test",
27
+ "prepublishOnly": "npm run build",
28
+ "prepare": "npm run build"
29
+ },
30
+ "keywords": [
31
+ "cli",
32
+ "disk-space",
33
+ "cleanup",
34
+ "cache",
35
+ "devclean",
36
+ "node_modules",
37
+ "developer-tools",
38
+ "windows",
39
+ "macos",
40
+ "linux"
41
+ ],
42
+ "license": "MIT",
43
+ "devDependencies": {
44
+ "@types/node": "^22.10.2",
45
+ "typescript": "^5.7.2"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/shisilang684/devclean.git"
50
+ },
51
+ "homepage": "https://github.com/shisilang684/devclean#readme",
52
+ "bugs": {
53
+ "url": "https://github.com/shisilang684/devclean/issues"
54
+ },
55
+ "author": "shisilang684"
56
+ }