dustclaw 0.1.2 → 0.4.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/README.md +162 -43
- package/data/config.json +23 -0
- package/data/targets.json +326 -0
- package/dist/commands/clean.d.ts +6 -0
- package/dist/commands/clean.js +75 -0
- package/dist/commands/overview.d.ts +1 -1
- package/dist/commands/overview.js +6 -4
- package/dist/commands/scan.d.ts +3 -3
- package/dist/commands/scan.js +4 -6
- package/dist/commands/wasteland.d.ts +1 -1
- package/dist/commands/wasteland.js +4 -1
- package/dist/index.js +55 -10
- package/dist/utils/config.d.ts +26 -0
- package/dist/utils/config.js +81 -0
- package/dist/utils/disk.d.ts +1 -0
- package/dist/utils/disk.js +6 -0
- package/dist/utils/format.js +10 -3
- package/dist/utils/scan.js +16 -8
- package/dist/utils/state.d.ts +9 -0
- package/dist/utils/state.js +32 -0
- package/dist/utils/wasteland.d.ts +1 -0
- package/dist/utils/wasteland.js +27 -122
- package/package.json +12 -11
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { rmSync } from "node:fs";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { boxTable, colorSize, formatSize } from "../utils/format.js";
|
|
5
|
+
import { loadLastScan, scanAgePath, staleThresholdMs } from "../utils/state.js";
|
|
6
|
+
function ask(question) {
|
|
7
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
rl.question(question, (answer) => {
|
|
10
|
+
rl.close();
|
|
11
|
+
resolve(answer.trim().toLowerCase());
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function deleteEntry(entry) {
|
|
16
|
+
rmSync(entry.path, { recursive: true, force: true });
|
|
17
|
+
}
|
|
18
|
+
export async function clean(opts) {
|
|
19
|
+
const state = loadLastScan();
|
|
20
|
+
if (!state) {
|
|
21
|
+
console.log(chalk.yellow("\n No scan found. Run dustclaw wasteland first.\n"));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const ageMs = scanAgePath(state);
|
|
25
|
+
if (ageMs > staleThresholdMs()) {
|
|
26
|
+
const hours = Math.floor(ageMs / (60 * 60 * 1000));
|
|
27
|
+
console.log(chalk.yellow(`\n Scan is ${hours}h old. Run dustclaw wasteland again for fresh results.\n`));
|
|
28
|
+
}
|
|
29
|
+
const filter = opts.only?.toLowerCase();
|
|
30
|
+
const safe = state.entries.filter((e) => e.safe && (!filter || e.name.toLowerCase().includes(filter)));
|
|
31
|
+
const unsafe = state.entries.filter((e) => !e.safe && (!filter || e.name.toLowerCase().includes(filter)));
|
|
32
|
+
if (filter && safe.length === 0 && unsafe.length === 0) {
|
|
33
|
+
console.log(chalk.yellow(`\n No entries matching "${opts.only}".\n`));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (safe.length === 0 && unsafe.length === 0) {
|
|
37
|
+
console.log(chalk.green("\n Nothing to clean.\n"));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (safe.length > 0) {
|
|
41
|
+
const safeTotal = safe.reduce((sum, e) => sum + e.size, 0);
|
|
42
|
+
console.log(chalk.bold("\n Safe to clean:\n"));
|
|
43
|
+
console.log(boxTable(["Name", "Size", "Path"], safe.map((e) => [e.name, colorSize(e.size), e.path])));
|
|
44
|
+
console.log(chalk.bold(`\n Total: ${colorSize(safeTotal)}\n`));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
console.log(chalk.yellow("\n No safely cleanable items found.\n"));
|
|
48
|
+
}
|
|
49
|
+
if (unsafe.length > 0) {
|
|
50
|
+
console.log(chalk.red.bold(" Manual only (cannot be auto-cleaned):\n"));
|
|
51
|
+
console.log(boxTable(["Name", "Size", "Path"], unsafe.map((e) => [e.name, colorSize(e.size), e.path])));
|
|
52
|
+
console.log(chalk.gray("\n These contain user data and must be cleaned manually.\n"));
|
|
53
|
+
console.log(chalk.gray(" To include them, set safe: true in ~/.dustclaw.json overrides.\n"));
|
|
54
|
+
}
|
|
55
|
+
if (safe.length === 0)
|
|
56
|
+
return;
|
|
57
|
+
if (!opts.force) {
|
|
58
|
+
console.log(chalk.yellow(" Dry run. Add --force to actually delete safe items.\n"));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
console.log(chalk.red.bold(" WARNING: Deletion is permanent. There is no undo.\n"));
|
|
62
|
+
let freed = 0;
|
|
63
|
+
for (const entry of safe) {
|
|
64
|
+
const answer = await ask(` Delete ${chalk.bold(entry.name)} (${formatSize(entry.size)})? [y/N] `);
|
|
65
|
+
if (answer === "y") {
|
|
66
|
+
deleteEntry(entry);
|
|
67
|
+
freed += entry.size;
|
|
68
|
+
console.log(chalk.green(` Deleted: ${entry.path}`));
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
console.log(chalk.gray(` Skipped: ${entry.path}`));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
console.log(chalk.bold(`\n Freed: ${colorSize(freed)}\n`));
|
|
75
|
+
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import chalk from "chalk";
|
|
3
|
-
import {
|
|
3
|
+
import { resolveConfig } from "../utils/config.js";
|
|
4
|
+
import { getDiskInfo, usedRatio } from "../utils/disk.js";
|
|
4
5
|
import { boxTable, colorSize, formatPercent, formatSize } from "../utils/format.js";
|
|
5
6
|
import { scanDir } from "../utils/scan.js";
|
|
6
7
|
export function overview(opts) {
|
|
7
|
-
const
|
|
8
|
+
const { overviewDepth } = resolveConfig();
|
|
9
|
+
const count = opts.top;
|
|
8
10
|
const targetPath = resolve(opts.path);
|
|
9
11
|
const disks = getDiskInfo();
|
|
10
|
-
const entries = scanDir(targetPath, { depth:
|
|
12
|
+
const entries = scanDir(targetPath, { depth: overviewDepth });
|
|
11
13
|
const topEntries = entries.sort((a, b) => b.size - a.size).slice(0, count);
|
|
12
14
|
if (opts.json) {
|
|
13
15
|
console.log(JSON.stringify({
|
|
@@ -31,7 +33,7 @@ export function overview(opts) {
|
|
|
31
33
|
formatSize(d.total),
|
|
32
34
|
colorSize(d.used),
|
|
33
35
|
colorSize(d.available),
|
|
34
|
-
formatPercent(d
|
|
36
|
+
formatPercent(usedRatio(d)),
|
|
35
37
|
]);
|
|
36
38
|
console.log(boxTable(["Mount", "Total", "Used", "Available", "Use%"], diskRows));
|
|
37
39
|
console.log(chalk.bold(`\n Top ${count} in ${targetPath}\n`));
|
package/dist/commands/scan.d.ts
CHANGED
package/dist/commands/scan.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { boxTable, colorSize } from "../utils/format.js";
|
|
4
|
-
import {
|
|
4
|
+
import { scanDir } from "../utils/scan.js";
|
|
5
5
|
export function scan(path, opts) {
|
|
6
6
|
const targetPath = resolve(path ?? ".");
|
|
7
|
-
const count =
|
|
7
|
+
const count = opts.top;
|
|
8
8
|
const scanOpts = {
|
|
9
|
-
depth:
|
|
9
|
+
depth: opts.depth,
|
|
10
10
|
filesOnly: opts.filesOnly,
|
|
11
11
|
dirsOnly: opts.dirsOnly,
|
|
12
|
+
olderThan: opts.olderThan,
|
|
12
13
|
};
|
|
13
|
-
if (opts.olderThan) {
|
|
14
|
-
scanOpts.olderThan = parseAge(opts.olderThan);
|
|
15
|
-
}
|
|
16
14
|
const entries = scanDir(targetPath, scanOpts);
|
|
17
15
|
const sorted = entries.sort((a, b) => b.size - a.size).slice(0, count);
|
|
18
16
|
if (opts.json) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { boxTable, colorSize, formatSize } from "../utils/format.js";
|
|
4
|
+
import { saveLastScan } from "../utils/state.js";
|
|
4
5
|
import { findNodeModules, findWaste } from "../utils/wasteland.js";
|
|
5
|
-
export function wasteland(opts) {
|
|
6
|
+
export async function wasteland(opts) {
|
|
6
7
|
const waste = findWaste();
|
|
7
8
|
let nmEntries = [];
|
|
8
9
|
if (opts.nodeModules) {
|
|
@@ -27,6 +28,7 @@ export function wasteland(opts) {
|
|
|
27
28
|
console.log(chalk.green("\n No wasteland found. Your disk is clean!\n"));
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
31
|
+
saveLastScan(allEntries);
|
|
30
32
|
console.log(chalk.bold("\n Wasteland Report\n"));
|
|
31
33
|
if (waste.length > 0) {
|
|
32
34
|
console.log(chalk.bold(" Known space wasters:\n"));
|
|
@@ -40,4 +42,5 @@ export function wasteland(opts) {
|
|
|
40
42
|
console.log(boxTable(["Size", "Path"], nmRows));
|
|
41
43
|
}
|
|
42
44
|
console.log(chalk.bold(`\n Total reclaimable: ${colorSize(totalSize)}\n`));
|
|
45
|
+
console.log(chalk.gray(" Scan saved. Run dustclaw clean to remove safe items.\n"));
|
|
43
46
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,65 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { Command, InvalidArgumentError } from "commander";
|
|
5
|
+
import { clean } from "./commands/clean.js";
|
|
3
6
|
import { overview } from "./commands/overview.js";
|
|
4
7
|
import { scan } from "./commands/scan.js";
|
|
5
8
|
import { wasteland } from "./commands/wasteland.js";
|
|
9
|
+
import { resolveConfig } from "./utils/config.js";
|
|
10
|
+
import { parseAge } from "./utils/scan.js";
|
|
11
|
+
function positiveIntOption(value) {
|
|
12
|
+
const n = Number(value);
|
|
13
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
14
|
+
throw new InvalidArgumentError("Must be a whole number of 1 or more.");
|
|
15
|
+
}
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
function ageOption(value) {
|
|
19
|
+
try {
|
|
20
|
+
return parseAge(value);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
throw new InvalidArgumentError(err.message);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const { version } = JSON.parse(readFileSync(fileURLToPath(new URL("../package.json", import.meta.url)), "utf-8"));
|
|
27
|
+
const { overviewTopCount, scanTopCount, scanDepth } = resolveConfig();
|
|
6
28
|
const program = new Command();
|
|
7
29
|
program
|
|
8
30
|
.name("dustclaw")
|
|
9
31
|
.description("Disk space monitoring, analysis, and cleanup CLI")
|
|
10
|
-
.version(
|
|
32
|
+
.version(version)
|
|
33
|
+
.showHelpAfterError(`
|
|
34
|
+
Examples:
|
|
35
|
+
dustclaw ../.. overview of a path
|
|
36
|
+
dustclaw scan ~/Projects -n 20 top 20 largest items in a path
|
|
37
|
+
dustclaw wasteland find caches and build junk
|
|
38
|
+
dustclaw clean dry run cleanup
|
|
39
|
+
|
|
40
|
+
Run dustclaw <command> --help for all options.`)
|
|
41
|
+
.addHelpText("after", `
|
|
42
|
+
Examples:
|
|
43
|
+
dustclaw overview of the current directory
|
|
44
|
+
dustclaw ../.. overview of a path
|
|
45
|
+
dustclaw scan ~/Projects -n 20 top 20 largest items in a path
|
|
46
|
+
dustclaw scan --older-than 6m items not modified in 6 months
|
|
47
|
+
dustclaw wasteland find caches and build junk
|
|
48
|
+
dustclaw clean dry run cleanup
|
|
49
|
+
dustclaw clean --force delete safe items, prompts per item`);
|
|
11
50
|
program
|
|
12
|
-
.command("overview", { isDefault: true })
|
|
13
|
-
.description(
|
|
14
|
-
.option("-n, --top <count>", "Number of top items to show",
|
|
15
|
-
.option("-p, --path <path>", "Path to analyze", ".")
|
|
51
|
+
.command("overview [path]", { isDefault: true })
|
|
52
|
+
.description(`Quick overview: disk usage, free space, top ${overviewTopCount} biggest items`)
|
|
53
|
+
.option("-n, --top <count>", "Number of top items to show", positiveIntOption, overviewTopCount)
|
|
54
|
+
.option("-p, --path <path>", "Path to analyze (same as the positional path)", ".")
|
|
16
55
|
.option("--json", "Output as JSON")
|
|
17
|
-
.action(overview);
|
|
56
|
+
.action((path, opts) => overview({ ...opts, path: path ?? opts.path }));
|
|
18
57
|
program
|
|
19
58
|
.command("scan [path]")
|
|
20
59
|
.description("Deep scan: ranked list of largest files and folders")
|
|
21
|
-
.option("-n, --top <count>", "Number of top items to show",
|
|
22
|
-
.option("-d, --depth <depth>", "Max directory depth",
|
|
23
|
-
.option("--older-than <age>", "Filter by age (e.g. 30d, 6m, 1y)")
|
|
60
|
+
.option("-n, --top <count>", "Number of top items to show", positiveIntOption, scanTopCount)
|
|
61
|
+
.option("-d, --depth <depth>", "Max directory depth", positiveIntOption, scanDepth)
|
|
62
|
+
.option("--older-than <age>", "Filter by age (e.g. 30d, 6m, 1y)", ageOption)
|
|
24
63
|
.option("--files-only", "Show only files, not directories")
|
|
25
64
|
.option("--dirs-only", "Show only directories, not files")
|
|
26
65
|
.option("--json", "Output as JSON")
|
|
@@ -31,4 +70,10 @@ program
|
|
|
31
70
|
.option("--node-modules <path>", "Also search for node_modules under this path")
|
|
32
71
|
.option("--json", "Output as JSON")
|
|
33
72
|
.action(wasteland);
|
|
73
|
+
program
|
|
74
|
+
.command("clean")
|
|
75
|
+
.description("Clean safe space wasters (dry run by default, use --force to delete)")
|
|
76
|
+
.option("--only <name>", "Filter to entries matching this name (case-insensitive)")
|
|
77
|
+
.option("--force", "Actually delete (irreversible, prompts per item)")
|
|
78
|
+
.action(clean);
|
|
34
79
|
program.parse();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface TargetDef {
|
|
2
|
+
name: string;
|
|
3
|
+
path: string;
|
|
4
|
+
description: string;
|
|
5
|
+
platform: "mac" | "linux" | "all";
|
|
6
|
+
safe: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface DustclawConfig {
|
|
9
|
+
minCacheSizeMb: number;
|
|
10
|
+
minCacheSize: number;
|
|
11
|
+
staleThresholdHours: number;
|
|
12
|
+
staleThresholdMs: number;
|
|
13
|
+
nodeModulesMaxDepth: number;
|
|
14
|
+
encoding: BufferEncoding;
|
|
15
|
+
scanDepth: number;
|
|
16
|
+
overviewDepth: number;
|
|
17
|
+
overviewTopCount: number;
|
|
18
|
+
scanTopCount: number;
|
|
19
|
+
blockSize: number;
|
|
20
|
+
cachesPath: string;
|
|
21
|
+
cacheSkipList: string[];
|
|
22
|
+
opaqueDirectories: string[];
|
|
23
|
+
}
|
|
24
|
+
export declare function resolveConfig(): DustclawConfig;
|
|
25
|
+
export declare function resolveCacheNames(): Record<string, string>;
|
|
26
|
+
export declare function resolveTargets(): TargetDef[];
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
const USER_CONFIG_PATH = join(homedir(), ".dustclaw.json");
|
|
6
|
+
// Bundled defaults live in <package root>/data. This file runs from either
|
|
7
|
+
// src/utils (dev, tests) or dist/utils (build), both two levels below the root.
|
|
8
|
+
const DATA_DIR = fileURLToPath(new URL("../../data/", import.meta.url));
|
|
9
|
+
function loadDataFile(name) {
|
|
10
|
+
return JSON.parse(readFileSync(join(DATA_DIR, name), "utf-8"));
|
|
11
|
+
}
|
|
12
|
+
function loadDefaultConfig() {
|
|
13
|
+
return loadDataFile("config.json");
|
|
14
|
+
}
|
|
15
|
+
function loadUserConfig() {
|
|
16
|
+
if (!existsSync(USER_CONFIG_PATH))
|
|
17
|
+
return {};
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(USER_CONFIG_PATH, "utf-8"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Config is read once per process. The scanners call resolveConfig() for every
|
|
26
|
+
// directory they visit, so re-reading the JSON files each time is very slow.
|
|
27
|
+
let cachedConfig;
|
|
28
|
+
let cachedTargetsFile;
|
|
29
|
+
export function resolveConfig() {
|
|
30
|
+
cachedConfig ??= buildConfig();
|
|
31
|
+
return cachedConfig;
|
|
32
|
+
}
|
|
33
|
+
function buildConfig() {
|
|
34
|
+
const defaults = loadDefaultConfig();
|
|
35
|
+
const user = loadUserConfig();
|
|
36
|
+
const minCacheSizeMb = user.minCacheSizeMb ?? defaults.minCacheSizeMb;
|
|
37
|
+
const staleThresholdHours = user.staleThresholdHours ?? defaults.staleThresholdHours;
|
|
38
|
+
return {
|
|
39
|
+
minCacheSizeMb,
|
|
40
|
+
minCacheSize: minCacheSizeMb * 1024 * 1024,
|
|
41
|
+
staleThresholdHours,
|
|
42
|
+
staleThresholdMs: staleThresholdHours * 60 * 60 * 1000,
|
|
43
|
+
nodeModulesMaxDepth: user.nodeModulesMaxDepth ?? defaults.nodeModulesMaxDepth,
|
|
44
|
+
encoding: user.encoding ?? defaults.encoding,
|
|
45
|
+
scanDepth: user.scanDepth ?? defaults.scanDepth,
|
|
46
|
+
overviewDepth: user.overviewDepth ?? defaults.overviewDepth,
|
|
47
|
+
overviewTopCount: user.overviewTopCount ?? defaults.overviewTopCount,
|
|
48
|
+
scanTopCount: user.scanTopCount ?? defaults.scanTopCount,
|
|
49
|
+
blockSize: user.blockSize ?? defaults.blockSize,
|
|
50
|
+
cachesPath: user.cachesPath ?? defaults.cachesPath,
|
|
51
|
+
cacheSkipList: user.cacheSkipList ?? defaults.cacheSkipList,
|
|
52
|
+
opaqueDirectories: user.opaqueDirectories ?? defaults.opaqueDirectories,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function loadTargetsFile() {
|
|
56
|
+
cachedTargetsFile ??= loadDataFile("targets.json");
|
|
57
|
+
return cachedTargetsFile;
|
|
58
|
+
}
|
|
59
|
+
export function resolveCacheNames() {
|
|
60
|
+
return loadTargetsFile().cacheNames;
|
|
61
|
+
}
|
|
62
|
+
export function resolveTargets() {
|
|
63
|
+
const platform = process.platform === "darwin" ? "mac" : "linux";
|
|
64
|
+
const base = loadTargetsFile().targets.filter((t) => t.platform === platform || t.platform === "all");
|
|
65
|
+
const user = loadUserConfig();
|
|
66
|
+
const overrides = user.overrides ?? {};
|
|
67
|
+
const merged = base.map((t) => {
|
|
68
|
+
const expanded = { ...t, path: t.path.replace(/^~/, homedir()) };
|
|
69
|
+
return overrides[t.name] !== undefined
|
|
70
|
+
? { ...expanded, safe: overrides[t.name].safe }
|
|
71
|
+
: expanded;
|
|
72
|
+
});
|
|
73
|
+
const custom = (user.targets ?? []).map((t) => ({
|
|
74
|
+
name: t.name,
|
|
75
|
+
path: t.path.replace(/^~/, homedir()),
|
|
76
|
+
description: t.description ?? "",
|
|
77
|
+
platform: "all",
|
|
78
|
+
safe: t.safe,
|
|
79
|
+
}));
|
|
80
|
+
return [...merged, ...custom];
|
|
81
|
+
}
|
package/dist/utils/disk.d.ts
CHANGED
package/dist/utils/disk.js
CHANGED
|
@@ -27,3 +27,9 @@ export function getDiskInfo() {
|
|
|
27
27
|
}
|
|
28
28
|
return disks;
|
|
29
29
|
}
|
|
30
|
+
// Same as df's Capacity column. Dividing by total is wrong on APFS, where every
|
|
31
|
+
// volume reports the whole shared container as its total.
|
|
32
|
+
export function usedRatio(disk) {
|
|
33
|
+
const usable = disk.used + disk.available;
|
|
34
|
+
return usable === 0 ? 0 : disk.used / usable;
|
|
35
|
+
}
|
package/dist/utils/format.js
CHANGED
|
@@ -27,12 +27,19 @@ export function formatPercent(ratio) {
|
|
|
27
27
|
return chalk.yellow(text);
|
|
28
28
|
return chalk.green(text);
|
|
29
29
|
}
|
|
30
|
+
function visLen(str) {
|
|
31
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: intentional ANSI escape strip
|
|
32
|
+
return str.replace(/\x1B\[[0-9;]*m/g, "").length;
|
|
33
|
+
}
|
|
34
|
+
function padEndVis(str, width) {
|
|
35
|
+
return str + " ".repeat(Math.max(0, width - visLen(str)));
|
|
36
|
+
}
|
|
30
37
|
export function boxTable(headers, rows) {
|
|
31
|
-
const colWidths = headers.map((h, i) => Math.max(h
|
|
32
|
-
const sep =
|
|
38
|
+
const colWidths = headers.map((h, i) => Math.max(visLen(h), ...rows.map((r) => visLen(r[i] ?? ""))));
|
|
39
|
+
const sep = `├${colWidths.map((w) => "─".repeat(w + 2)).join("┼")}┤`;
|
|
33
40
|
const top = `┌${colWidths.map((w) => "─".repeat(w + 2)).join("┬")}┐`;
|
|
34
41
|
const bot = `└${colWidths.map((w) => "─".repeat(w + 2)).join("┴")}┘`;
|
|
35
|
-
const fmtRow = (cells) => `│${cells.map((c, i) => ` ${c
|
|
42
|
+
const fmtRow = (cells) => `│${cells.map((c, i) => ` ${padEndVis(c, colWidths[i])} `).join("│")}│`;
|
|
36
43
|
const lines = [top, fmtRow(headers), sep, ...rows.map(fmtRow), bot];
|
|
37
44
|
return lines.join("\n");
|
|
38
45
|
}
|
package/dist/utils/scan.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { lstatSync, readdirSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { resolveConfig } from "./config.js";
|
|
3
4
|
export function scanDir(root, opts = {}, currentDepth = 0) {
|
|
4
|
-
const
|
|
5
|
+
const { scanDepth, blockSize, opaqueDirectories } = resolveConfig();
|
|
6
|
+
const maxDepth = opts.depth ?? scanDepth;
|
|
7
|
+
const opaqueSet = new Set(opaqueDirectories);
|
|
5
8
|
const entries = [];
|
|
6
9
|
let items;
|
|
7
10
|
try {
|
|
@@ -16,13 +19,13 @@ export function scanDir(root, opts = {}, currentDepth = 0) {
|
|
|
16
19
|
const fullPath = join(root, name);
|
|
17
20
|
let stat;
|
|
18
21
|
try {
|
|
19
|
-
stat =
|
|
22
|
+
stat = lstatSync(fullPath);
|
|
20
23
|
}
|
|
21
24
|
catch {
|
|
22
25
|
continue;
|
|
23
26
|
}
|
|
24
27
|
if (stat.isDirectory()) {
|
|
25
|
-
if (name
|
|
28
|
+
if (opaqueSet.has(name)) {
|
|
26
29
|
const size = dirSize(fullPath);
|
|
27
30
|
if (!opts.filesOnly) {
|
|
28
31
|
if (!opts.olderThan || stat.mtime < opts.olderThan) {
|
|
@@ -49,7 +52,7 @@ export function scanDir(root, opts = {}, currentDepth = 0) {
|
|
|
49
52
|
if (!opts.olderThan || stat.mtime < opts.olderThan) {
|
|
50
53
|
entries.push({
|
|
51
54
|
path: fullPath,
|
|
52
|
-
size: stat.
|
|
55
|
+
size: stat.blocks * blockSize,
|
|
53
56
|
isDirectory: false,
|
|
54
57
|
mtime: stat.mtime,
|
|
55
58
|
});
|
|
@@ -60,6 +63,11 @@ export function scanDir(root, opts = {}, currentDepth = 0) {
|
|
|
60
63
|
return entries;
|
|
61
64
|
}
|
|
62
65
|
export function dirSize(dirPath) {
|
|
66
|
+
return sumDir(dirPath, resolveConfig().blockSize);
|
|
67
|
+
}
|
|
68
|
+
// lstat, not stat: symlinks are not followed, matching du. Following them
|
|
69
|
+
// double-counts linked data and loops forever on a link to a parent folder.
|
|
70
|
+
function sumDir(dirPath, blockSize) {
|
|
63
71
|
let total = 0;
|
|
64
72
|
let items;
|
|
65
73
|
try {
|
|
@@ -71,12 +79,12 @@ export function dirSize(dirPath) {
|
|
|
71
79
|
for (const name of items) {
|
|
72
80
|
const fullPath = join(dirPath, name);
|
|
73
81
|
try {
|
|
74
|
-
const stat =
|
|
82
|
+
const stat = lstatSync(fullPath);
|
|
75
83
|
if (stat.isFile()) {
|
|
76
|
-
total += stat.
|
|
84
|
+
total += stat.blocks * blockSize;
|
|
77
85
|
}
|
|
78
86
|
else if (stat.isDirectory()) {
|
|
79
|
-
total +=
|
|
87
|
+
total += sumDir(fullPath, blockSize);
|
|
80
88
|
}
|
|
81
89
|
}
|
|
82
90
|
catch { }
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WasteEntry } from "./wasteland.js";
|
|
2
|
+
export interface ScanState {
|
|
3
|
+
scannedAt: string;
|
|
4
|
+
entries: WasteEntry[];
|
|
5
|
+
}
|
|
6
|
+
export declare function saveLastScan(entries: WasteEntry[]): void;
|
|
7
|
+
export declare function loadLastScan(): ScanState | null;
|
|
8
|
+
export declare function scanAgePath(state: ScanState): number;
|
|
9
|
+
export declare function staleThresholdMs(): number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { resolveConfig } from "./config.js";
|
|
5
|
+
const STATE_DIR = join(homedir(), ".dustclaw");
|
|
6
|
+
const SCAN_FILE = join(STATE_DIR, "last-scan.json");
|
|
7
|
+
export function saveLastScan(entries) {
|
|
8
|
+
const { encoding } = resolveConfig();
|
|
9
|
+
mkdirSync(STATE_DIR, { recursive: true });
|
|
10
|
+
const state = {
|
|
11
|
+
scannedAt: new Date().toISOString(),
|
|
12
|
+
entries,
|
|
13
|
+
};
|
|
14
|
+
writeFileSync(SCAN_FILE, JSON.stringify(state, null, 2), encoding);
|
|
15
|
+
}
|
|
16
|
+
export function loadLastScan() {
|
|
17
|
+
if (!existsSync(SCAN_FILE))
|
|
18
|
+
return null;
|
|
19
|
+
const { encoding } = resolveConfig();
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(readFileSync(SCAN_FILE, encoding));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function scanAgePath(state) {
|
|
28
|
+
return Date.now() - new Date(state.scannedAt).getTime();
|
|
29
|
+
}
|
|
30
|
+
export function staleThresholdMs() {
|
|
31
|
+
return resolveConfig().staleThresholdMs;
|
|
32
|
+
}
|