dir-analysis-tool 3.5.4 → 3.5.5

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
@@ -534,7 +534,7 @@ Then ask in your own words, for example "why is this repo so big?" or "fail CI i
534
534
  2. Files over 256 KiB are compared by a SHA-256 sample of their first and last 64 KiB.
535
535
  3. Remaining candidates are hashed in full: small files in a single read, large files streamed through reusable buffers. Each size group is finished as soon as its last file is hashed, so intermediate hash maps are retained only for groups in flight; candidates and results still use memory proportional to their count.
536
536
  - **Disk usage.** Sizes are apparent sizes (file lengths). `On disk` is the space allocated in blocks, which is what `du` reports.
537
- - **Determinism.** Results are sorted by size and then by path, so the same tree always produces the same output, whatever order the filesystem returned it in.
537
+ - **Determinism.** Results are sorted by size and then by path, so the same tree always produces the same output, whatever order the filesystem returned it in. Equal sizes are ordered by comparing paths where they first differ, without building them, so deep trees full of same-size files rank them as fast as shallow ones.
538
538
 
539
539
  **Benchmarks** (Apple M4, warm cache, best of three; measurements from the optimization work, not performance guarantees). On the Linux kernel source tree, 96,127 files and 1.53 GiB:
540
540
 
@@ -545,7 +545,7 @@ Then ask in your own words, for example "why is this repo so big?" or "fail CI i
545
545
  | Scan and find duplicates | 2.64 s | **1.20 s** | |
546
546
  | Peak heap while finding duplicates | 118 MB | **45 MB** | |
547
547
 
548
- On a synthetic tree of 1,000,000 files, v3.1 scans in 8.9 s where `du -s` takes 13 s. Compared with v2.0.7 on a 140,237-file tree, v3 scans about 3× faster and finds duplicates about 4× faster, with identical results. Run `pnpm bench` (or `pnpm bench <directory>`) to measure on your machine.
548
+ On a synthetic tree of 1,000,000 files, v3.1 scans in 8.9 s where `du -s` takes 13 s. On a 500-level chain of folders holding 2,000 files of one size, 3.5.5 scans in 44 ms where 3.5.4 took 90 ms (Linux in Docker on the same M4, median of nine), the same as with the largest-files list turned off. Compared with v2.0.7 on a 140,237-file tree, v3 scans about 3× faster and finds duplicates about 4× faster, with identical results. Run `pnpm bench` (or `pnpm bench <directory>`) to measure on your machine.
549
549
 
550
550
  The benchmark script reports scan and duplicate timings plus process peak RSS (not JavaScript heap). Results vary with storage, cache state, tree shape, and Node.js version. `du` measures allocated disk space and does not produce the same reports.
551
551
 
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { $ as errorCode, C as MAX_EXPLAIN_LIMIT, D as outsideRootError, E as isOutside, F as listDirectories, G as formatBytes, J as VERSION, K as parseSize, L as Excluder, M as analyze, O as compare, P as assertDirectory, Q as DatError, R as joinRelative, T as explainSkips, U as formatBudgetActual, W as parseBudget, X as parsePointInTime, Z as AbortError, a as renderComparisonCsv, c as renderAnalysis, d as escapeControlInLines, et as errorMessage, f as formatCount, h as supportsColor, i as renderAnalysisCsv, k as compactAnalysis, l as renderComparison, m as createStyle, n as renderSkipsExplanation, o as renderAnalysisMarkdown, p as stringifyJson, r as renderAnalysisHtml, s as renderComparisonMarkdown, t as renderPathExplanation, u as escapeControl, v as loadConfig, w as explainPath } from "./shared-c7R0EX5i.js";
2
+ import { $ as errorCode, C as MAX_EXPLAIN_LIMIT, D as outsideRootError, E as isOutside, F as listDirectories, G as formatBytes, J as VERSION, K as parseSize, L as Excluder, M as analyze, O as compare, P as assertDirectory, Q as DatError, R as joinRelative, T as explainSkips, U as formatBudgetActual, W as parseBudget, X as parsePointInTime, Z as AbortError, a as renderComparisonCsv, c as renderAnalysis, d as escapeControlInLines, et as errorMessage, f as formatCount, h as supportsColor, i as renderAnalysisCsv, k as compactAnalysis, l as renderComparison, m as createStyle, n as renderSkipsExplanation, o as renderAnalysisMarkdown, p as stringifyJson, r as renderAnalysisHtml, s as renderComparisonMarkdown, t as renderPathExplanation, u as escapeControl, v as loadConfig, w as explainPath } from "./shared-15TspuKg.js";
3
3
  import { constants, existsSync, watch } from "node:fs";
4
4
  import { access, mkdir, realpath, stat, writeFile } from "node:fs/promises";
5
5
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { Z as AbortError, V as BUDGET_METRICS, q as CATEGORIES, g as CONFIG_FILE_NAMES, N as DEFAULT_CONCURRENCY, I as DEFAULT_EXCLUDES, x as DEFAULT_EXPLAIN_LIMIT, A as DEFAULT_LARGE_FILE_THRESHOLD, j as DEFAULT_STALE_AGE, Q as DatError, S as EXPLAIN_VERSION, C as MAX_EXPLAIN_LIMIT, J as VERSION, M as analyze, y as buildJsonSchema, z as classify, k as compactAnalysis, O as compare, m as createStyle, H as evaluateBudgets, w as explainPath, T as explainSkips, B as extensionOf, _ as findConfigFile, G as formatBytes, F as listDirectories, v as loadConfig, W as parseBudget, Y as parseDuration, X as parsePointInTime, K as parseSize, c as renderAnalysis, i as renderAnalysisCsv, r as renderAnalysisHtml, o as renderAnalysisMarkdown, l as renderComparison, a as renderComparisonCsv, s as renderComparisonMarkdown, t as renderPathExplanation, n as renderSkipsExplanation, b as validateConfig } from "./shared-c7R0EX5i.js";
1
+ export { Z as AbortError, V as BUDGET_METRICS, q as CATEGORIES, g as CONFIG_FILE_NAMES, N as DEFAULT_CONCURRENCY, I as DEFAULT_EXCLUDES, x as DEFAULT_EXPLAIN_LIMIT, A as DEFAULT_LARGE_FILE_THRESHOLD, j as DEFAULT_STALE_AGE, Q as DatError, S as EXPLAIN_VERSION, C as MAX_EXPLAIN_LIMIT, J as VERSION, M as analyze, y as buildJsonSchema, z as classify, k as compactAnalysis, O as compare, m as createStyle, H as evaluateBudgets, w as explainPath, T as explainSkips, B as extensionOf, _ as findConfigFile, G as formatBytes, F as listDirectories, v as loadConfig, W as parseBudget, Y as parseDuration, X as parsePointInTime, K as parseSize, c as renderAnalysis, i as renderAnalysisCsv, r as renderAnalysisHtml, o as renderAnalysisMarkdown, l as renderComparison, a as renderComparisonCsv, s as renderComparisonMarkdown, t as renderPathExplanation, n as renderSkipsExplanation, b as validateConfig } from "./shared-15TspuKg.js";
@@ -867,12 +867,19 @@ var Scanner = class {
867
867
  const onAbort = () => this.#limiter.abort(new AbortError());
868
868
  signal?.addEventListener("abort", onAbort, { once: true });
869
869
  try {
870
- this.#limiter.run(() => this.#scanDir(index, root, chain));
870
+ this.#limiter.run(() => this.#scanDir(index, root, chain, ""));
871
871
  await this.#limiter.idle();
872
872
  } finally {
873
873
  signal?.removeEventListener("abort", onAbort);
874
874
  }
875
875
  }
876
+ /**
877
+ * Orders two entries as {@link comparePaths} orders their relative paths,
878
+ * without building them. See {@link compareRelativePaths}.
879
+ */
880
+ compareRelative(dirA, nameA, dirB, nameB) {
881
+ return compareRelativePaths(this.dirs, dirA, nameA, dirB, nameB);
882
+ }
876
883
  /** Root-relative path of a directory (or of `name` inside it), with `/` separators. */
877
884
  relativePath(dirIndex, name) {
878
885
  const parts = name === void 0 ? [] : [name];
@@ -901,7 +908,12 @@ var Scanner = class {
901
908
  this.#visitor.directory?.(index, node);
902
909
  return index;
903
910
  }
904
- async #scanDir(index, absPath, inherited) {
911
+ /**
912
+ * @param relDir Root-relative path of the directory when the excluder
913
+ * matches paths (passed down from the parent, so it is never rebuilt
914
+ * from the root), and `''` otherwise.
915
+ */
916
+ async #scanDir(index, absPath, inherited, relDir) {
905
917
  const { excluder } = this.#options;
906
918
  let entries;
907
919
  try {
@@ -914,7 +926,6 @@ var Scanner = class {
914
926
  const node = this.dirs[index];
915
927
  node.entries = entries.length;
916
928
  if (entries.length === 0) return;
917
- const relDir = excluder.needsPaths ? this.relativePath(index) : "";
918
929
  let chain = inherited;
919
930
  if (excluder.gitignore && entries.some((e) => e.name === ".gitignore" && !e.isDirectory())) {
920
931
  const scope = await excluder.loadScope(joinNative(absPath, ".gitignore"), relDir);
@@ -922,6 +933,7 @@ var Scanner = class {
922
933
  }
923
934
  const base = absPath.endsWith(sep) ? absPath : absPath + sep;
924
935
  const checkExclusion = excluder.active;
936
+ const needsPaths = excluder.needsPaths;
925
937
  for (const entry of entries) {
926
938
  const name = entry.name;
927
939
  const isDirectory = entry.isDirectory();
@@ -929,9 +941,9 @@ var Scanner = class {
929
941
  const isSymlink = !isDirectory && !isFile && entry.isSymbolicLink();
930
942
  const isUnknown = !isDirectory && !isFile && !isSymlink && isUnknownType(entry);
931
943
  if (!isDirectory && !isFile && !isSymlink && !isUnknown) continue;
944
+ const rel = needsPaths ? relDir ? `${relDir}/${name}` : name : "";
932
945
  if (checkExclusion) {
933
- const rel = relDir ? `${relDir}/${name}` : name;
934
- if (excluder.isExcluded(rel, name, isDirectory, chain)) {
946
+ if (excluder.isExcluded(rel || name, name, isDirectory, chain)) {
935
947
  this.counters.excluded++;
936
948
  this.#options.onSkip?.({
937
949
  kind: "excluded",
@@ -945,12 +957,12 @@ var Scanner = class {
945
957
  }
946
958
  const fullPath = base + name;
947
959
  if (this.#options.directoriesOnly && !isDirectory) continue;
948
- if (isDirectory && !this.#options.followSymlinks) this.#enterDir(index, name, fullPath, chain);
960
+ if (isDirectory && !this.#options.followSymlinks) this.#enterDir(index, name, fullPath, chain, rel);
949
961
  else if (isFile) this.#limiter.run(() => this.#statFile(index, name, fullPath));
950
- else this.#limiter.run(() => this.#resolveEntry(index, name, fullPath, chain, isSymlink, isDirectory));
962
+ else this.#limiter.run(() => this.#resolveEntry(index, name, fullPath, chain, rel, isSymlink, isDirectory));
951
963
  }
952
964
  }
953
- #enterDir(parent, name, fullPath, chain) {
965
+ #enterDir(parent, name, fullPath, chain, relPath) {
954
966
  const depth = this.dirs[parent].depth + 1;
955
967
  if (depth > this.#options.maxDepth) {
956
968
  this.#options.onSkip?.({
@@ -962,7 +974,7 @@ var Scanner = class {
962
974
  return;
963
975
  }
964
976
  const child = this.#addDir(name, parent, depth);
965
- this.#limiter.run(() => this.#scanDir(child, fullPath, chain));
977
+ this.#limiter.run(() => this.#scanDir(child, fullPath, chain, relPath));
966
978
  }
967
979
  async #statFile(dirIndex, name, fullPath) {
968
980
  let stats;
@@ -979,7 +991,7 @@ var Scanner = class {
979
991
  * Handles symlinks, entries whose type the filesystem did not report, and
980
992
  * (when following symlinks) directories, which then need their identity.
981
993
  */
982
- async #resolveEntry(dirIndex, name, fullPath, chain, isSymlink, checkedAsDirectory) {
994
+ async #resolveEntry(dirIndex, name, fullPath, chain, relPath, isSymlink, checkedAsDirectory) {
983
995
  let stats;
984
996
  try {
985
997
  stats = await lstat(fullPath);
@@ -1015,8 +1027,7 @@ var Scanner = class {
1015
1027
  if (stats.isDirectory()) {
1016
1028
  const { excluder } = this.#options;
1017
1029
  if (excluder.active && !checkedAsDirectory) {
1018
- const rel = this.relativePath(dirIndex, name);
1019
- if (excluder.isExcluded(rel, name, true, chain)) {
1030
+ if (excluder.isExcluded(relPath || name, name, true, chain)) {
1020
1031
  this.counters.excluded++;
1021
1032
  this.#options.onSkip?.({
1022
1033
  kind: "excluded",
@@ -1031,7 +1042,7 @@ var Scanner = class {
1031
1042
  const key = await inodeKey(stats, fullPath);
1032
1043
  if (this.#seenDirs.has(key)) return;
1033
1044
  this.#seenDirs.add(key);
1034
- this.#enterDir(dirIndex, name, fullPath, chain);
1045
+ this.#enterDir(dirIndex, name, fullPath, chain, relPath);
1035
1046
  } else if (stats.isFile()) {
1036
1047
  const pending = this.#acceptFile(dirIndex, name, fullPath, stats);
1037
1048
  if (pending) await pending;
@@ -1115,6 +1126,59 @@ function rollUp(dirs) {
1115
1126
  }
1116
1127
  }
1117
1128
  }
1129
+ /** Code unit of the `/` that joins path segments. */
1130
+ const SLASH = 47;
1131
+ /**
1132
+ * Compares the root-relative paths of two entries, each a directory index
1133
+ * plus an optional name inside it. The result has the same sign as
1134
+ * `comparePaths(relativePath(dirA, nameA), relativePath(dirB, nameB))`.
1135
+ *
1136
+ * Building both paths costs O(depth) time and garbage on every call, which
1137
+ * made top-N tie-breaks on deep trees slow (#31). This walks parent indices
1138
+ * to the closest common directory instead, and compares only the first
1139
+ * segment where the paths part. That is not a segment-by-segment comparison:
1140
+ * when one segment is a prefix of the other, the shorter path continues with
1141
+ * `/` (or ends), and `/` sorts after characters such as `-` and `.`, exactly
1142
+ * as it does in the joined string.
1143
+ */
1144
+ function compareRelativePaths(dirs, dirA, nameA, dirB, nameB) {
1145
+ if (dirA === dirB) {
1146
+ if (nameA === void 0 || nameB === void 0) return nameA === nameB ? 0 : nameA === void 0 ? -1 : 1;
1147
+ return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
1148
+ }
1149
+ let a = dirA;
1150
+ let b = dirB;
1151
+ let belowA = -1;
1152
+ let belowB = -1;
1153
+ while (dirs[a].depth > dirs[b].depth) {
1154
+ belowA = a;
1155
+ a = dirs[a].parent;
1156
+ }
1157
+ while (dirs[b].depth > dirs[a].depth) {
1158
+ belowB = b;
1159
+ b = dirs[b].parent;
1160
+ }
1161
+ while (a !== b) {
1162
+ belowA = a;
1163
+ a = dirs[a].parent;
1164
+ belowB = b;
1165
+ b = dirs[b].parent;
1166
+ }
1167
+ const segmentA = belowA === -1 ? nameA : dirs[belowA].name;
1168
+ const segmentB = belowB === -1 ? nameB : dirs[belowB].name;
1169
+ if (segmentA === void 0) return segmentB === void 0 ? 0 : -1;
1170
+ if (segmentB === void 0) return 1;
1171
+ const length = Math.min(segmentA.length, segmentB.length);
1172
+ for (let i = 0; i < length; i++) {
1173
+ const diff = segmentA.charCodeAt(i) - segmentB.charCodeAt(i);
1174
+ if (diff !== 0) return diff;
1175
+ }
1176
+ return nextCode(segmentA, length, belowA !== -1 && (belowA !== dirA || nameA !== void 0)) - nextCode(segmentB, length, belowB !== -1 && (belowB !== dirB || nameB !== void 0));
1177
+ }
1178
+ function nextCode(segment, at, continues) {
1179
+ if (at < segment.length) return segment.charCodeAt(at);
1180
+ return continues ? SLASH : -1;
1181
+ }
1118
1182
  /**
1119
1183
  * `dev:ino` identity of a file or directory. `Stats.ino` is a Number, which is
1120
1184
  * exact only up to 2^53; Windows NTFS file IDs keep a sequence number in their
@@ -2493,7 +2557,7 @@ var Collector = class {
2493
2557
  return result;
2494
2558
  }
2495
2559
  #largestDirectories(scanner) {
2496
- const top = new TopK(this.#features.top, (a, b) => comparePaths(scanner.relativePath(a), scanner.relativePath(b)));
2560
+ const top = new TopK(this.#features.top, (a, b) => scanner.compareRelative(a, void 0, b, void 0));
2497
2561
  const dirs = scanner.dirs;
2498
2562
  for (let i = 1; i < dirs.length; i++) {
2499
2563
  const node = dirs[i];
@@ -2558,9 +2622,9 @@ var Collector = class {
2558
2622
  }
2559
2623
  return result;
2560
2624
  }
2625
+ /** Ties in the largest-files heap: path order, without building the paths (#31). */
2561
2626
  #compareRefs(a, b) {
2562
- const scanner = this.#scanner;
2563
- return comparePaths(scanner.relativePath(a.dir, a.name), scanner.relativePath(b.dir, b.name));
2627
+ return this.#scanner.compareRelative(a.dir, a.name, b.dir, b.name);
2564
2628
  }
2565
2629
  };
2566
2630
  function createProgressReporter(report, interval, snapshot) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dir-analysis-tool",
3
- "version": "3.5.4",
3
+ "version": "3.5.5",
4
4
  "description": "Fast, zero-dependency directory analysis for your terminal and CI: largest folders and files, type and extension breakdowns, staged duplicate detection, size budgets, directory diffs, and self-contained HTML, Markdown, JSON, and CSV reports.",
5
5
  "type": "module",
6
6
  "exports": {