dir-analysis-tool 3.0.0 → 3.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/README.md +45 -41
- package/dist/cli.js +700 -27
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -1
- package/dist/{shared-Cmryy0SH.js → shared-htkguqJG.js} +519 -190
- package/package.json +8 -12
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import {
|
|
2
|
+
import { createHash, hash } from "node:crypto";
|
|
3
|
+
import { existsSync, readFile } from "node:fs";
|
|
4
|
+
import { lstat, open, readFile as readFile$1, readdir, stat } from "node:fs/promises";
|
|
4
5
|
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
6
|
-
import ignore from "ignore";
|
|
7
6
|
//#region src/errors.ts
|
|
8
7
|
/**
|
|
9
8
|
* An expected, user-facing failure (bad input, missing directory, invalid
|
|
@@ -178,6 +177,19 @@ function formatBytes(bytes, units = "iec") {
|
|
|
178
177
|
function trimZeros(text) {
|
|
179
178
|
return text.includes(".") ? text.replace(/\.?0+$/, "") : text;
|
|
180
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* The unit system a byte count was most likely written in, so a value can be
|
|
182
|
+
* shown back the way the user typed it: 1,000,000 bytes ("1MB") is SI and
|
|
183
|
+
* 1,048,576 ("1MiB") is IEC. Returns `fallback` when it is ambiguous.
|
|
184
|
+
*/
|
|
185
|
+
function unitsOf(bytes, fallback) {
|
|
186
|
+
if (!(bytes > 0)) return fallback;
|
|
187
|
+
const si = bytes % 1e3 === 0;
|
|
188
|
+
const iec = bytes % 1024 === 0;
|
|
189
|
+
if (si && !iec) return "si";
|
|
190
|
+
if (iec && !si) return "iec";
|
|
191
|
+
return fallback;
|
|
192
|
+
}
|
|
181
193
|
const SIZE_PATTERN = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*([a-z]*)\s*$/i;
|
|
182
194
|
const SIZE_MULTIPLIERS = {
|
|
183
195
|
"": 1,
|
|
@@ -345,6 +357,14 @@ function compare$1(actual, operator, limit) {
|
|
|
345
357
|
case "!=": return actual !== limit;
|
|
346
358
|
}
|
|
347
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Formats a budget's measured value for display. Sizes use the unit system of
|
|
362
|
+
* the rule's own limit, so "size<=500MB" reads "actual 312 MB", not "297 MiB".
|
|
363
|
+
*/
|
|
364
|
+
function formatBudgetActual(budget, units) {
|
|
365
|
+
if (BUDGET_METRICS[budget.metric].kind === "count") return budget.actual.toLocaleString("en-US");
|
|
366
|
+
return formatBytes(budget.actual, unitsOf(budget.limit, units));
|
|
367
|
+
}
|
|
348
368
|
//#endregion
|
|
349
369
|
//#region src/core/classify.ts
|
|
350
370
|
const EXTENSIONS = {
|
|
@@ -702,79 +722,13 @@ function emptyTypeStats() {
|
|
|
702
722
|
};
|
|
703
723
|
}
|
|
704
724
|
//#endregion
|
|
705
|
-
//#region src/core/limiter.ts
|
|
706
|
-
/**
|
|
707
|
-
* A tiny concurrency limiter for filesystem work.
|
|
708
|
-
*
|
|
709
|
-
* Tasks run at most `concurrency` at a time. Pending tasks are taken
|
|
710
|
-
* last-in-first-out, which makes a directory walk proceed depth-first and
|
|
711
|
-
* keeps the pending queue small even on very wide trees.
|
|
712
|
-
*/
|
|
713
|
-
var Limiter = class {
|
|
714
|
-
#concurrency;
|
|
715
|
-
#queue = [];
|
|
716
|
-
#active = 0;
|
|
717
|
-
#failure = void 0;
|
|
718
|
-
#failed = false;
|
|
719
|
-
#idle = null;
|
|
720
|
-
constructor(concurrency) {
|
|
721
|
-
if (!Number.isInteger(concurrency) || concurrency < 1) throw new RangeError(`concurrency must be a positive integer (got ${concurrency})`);
|
|
722
|
-
this.#concurrency = concurrency;
|
|
723
|
-
}
|
|
724
|
-
/** Schedules a task. Tasks are expected to handle their own errors; a thrown error aborts the run. */
|
|
725
|
-
run(task) {
|
|
726
|
-
if (this.#failed) return;
|
|
727
|
-
this.#queue.push(task);
|
|
728
|
-
this.#pump();
|
|
729
|
-
}
|
|
730
|
-
/** Resolves once every scheduled task, including tasks scheduled by tasks, has finished. */
|
|
731
|
-
idle() {
|
|
732
|
-
if (this.#failed) return Promise.reject(this.#failure);
|
|
733
|
-
if (this.#active === 0 && this.#queue.length === 0) return Promise.resolve();
|
|
734
|
-
return new Promise((resolve, reject) => {
|
|
735
|
-
this.#idle = {
|
|
736
|
-
resolve,
|
|
737
|
-
reject
|
|
738
|
-
};
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
/** Stops scheduling new work and rejects `idle()` with `reason`. */
|
|
742
|
-
abort(reason) {
|
|
743
|
-
if (this.#failed) return;
|
|
744
|
-
this.#failed = true;
|
|
745
|
-
this.#failure = reason;
|
|
746
|
-
this.#queue.length = 0;
|
|
747
|
-
this.#idle?.reject(reason);
|
|
748
|
-
this.#idle = null;
|
|
749
|
-
}
|
|
750
|
-
#pump() {
|
|
751
|
-
while (this.#active < this.#concurrency && this.#queue.length > 0) {
|
|
752
|
-
const task = this.#queue.pop();
|
|
753
|
-
this.#active++;
|
|
754
|
-
task().then(this.#onDone, this.#onError);
|
|
755
|
-
}
|
|
756
|
-
if (this.#active === 0 && this.#queue.length === 0 && this.#idle) {
|
|
757
|
-
const idle = this.#idle;
|
|
758
|
-
this.#idle = null;
|
|
759
|
-
idle.resolve();
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
#onDone = () => {
|
|
763
|
-
this.#active--;
|
|
764
|
-
this.#pump();
|
|
765
|
-
};
|
|
766
|
-
#onError = (error) => {
|
|
767
|
-
this.#active--;
|
|
768
|
-
this.abort(error);
|
|
769
|
-
};
|
|
770
|
-
};
|
|
771
|
-
//#endregion
|
|
772
725
|
//#region src/core/duplicates.ts
|
|
773
|
-
/** Files up to this size are hashed in full straight away. */
|
|
726
|
+
/** Files up to this size are read in one call and hashed in full straight away. */
|
|
774
727
|
const SAMPLE_THRESHOLD = 262144;
|
|
775
728
|
/** Bytes read from each end of a larger file for its sample hash. */
|
|
776
729
|
const SAMPLE_BYTES = 65536;
|
|
777
|
-
const READ_BUFFER_BYTES =
|
|
730
|
+
const READ_BUFFER_BYTES = 524288;
|
|
731
|
+
const ALGORITHM = "sha256";
|
|
778
732
|
/**
|
|
779
733
|
* Finds files with identical contents among groups of same-sized files.
|
|
780
734
|
*
|
|
@@ -782,7 +736,10 @@ const READ_BUFFER_BYTES = 1048576;
|
|
|
782
736
|
* 1. Only sizes shared by at least two distinct inodes are considered.
|
|
783
737
|
* 2. Large files are first compared by a sample hash of their first and
|
|
784
738
|
* last 64 KiB, which separates almost all non-duplicates cheaply.
|
|
785
|
-
* 3. Survivors (and small files) get a full
|
|
739
|
+
* 3. Survivors (and small files) get a full SHA-256.
|
|
740
|
+
*
|
|
741
|
+
* Groups are hashed independently and finished as soon as their last member
|
|
742
|
+
* is done, so memory holds only the groups in flight plus the results.
|
|
786
743
|
*/
|
|
787
744
|
async function findDuplicates(sizeGroups, options) {
|
|
788
745
|
const small = [];
|
|
@@ -792,104 +749,150 @@ async function findDuplicates(sizeGroups, options) {
|
|
|
792
749
|
if (distinct.length < 2) continue;
|
|
793
750
|
(distinct[0].bytes <= SAMPLE_THRESHOLD ? small : large).push(distinct);
|
|
794
751
|
}
|
|
795
|
-
const buffers = new BufferPool(
|
|
752
|
+
const buffers = new BufferPool();
|
|
796
753
|
const sampled = await hashStage(large, "sample", options, buffers);
|
|
797
|
-
|
|
754
|
+
for (const group of sampled) small.push(group.members);
|
|
755
|
+
return hashStage(small, "full", options, buffers);
|
|
798
756
|
}
|
|
799
757
|
async function hashStage(groups, stage, options, buffers) {
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
const
|
|
803
|
-
|
|
804
|
-
const { signal } = options;
|
|
805
|
-
const onAbort = () => limiter.abort(new AbortError());
|
|
806
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
758
|
+
let total = 0;
|
|
759
|
+
for (const group of groups) total += group.length;
|
|
760
|
+
const results = [];
|
|
761
|
+
if (total === 0) return results;
|
|
762
|
+
const { signal, onProgress } = options;
|
|
807
763
|
let done = 0;
|
|
808
|
-
|
|
764
|
+
onProgress?.({
|
|
809
765
|
stage,
|
|
810
766
|
done,
|
|
811
767
|
total
|
|
812
768
|
});
|
|
813
|
-
|
|
814
|
-
const
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
769
|
+
const finish = (state) => {
|
|
770
|
+
for (const [digest, members] of state.byHash) if (members.length > 1) results.push({
|
|
771
|
+
hash: digest,
|
|
772
|
+
bytes: state.bytes,
|
|
773
|
+
members
|
|
774
|
+
});
|
|
775
|
+
};
|
|
776
|
+
function* tasks() {
|
|
777
|
+
for (const group of groups) {
|
|
778
|
+
const state = {
|
|
779
|
+
bytes: group[0].bytes,
|
|
780
|
+
pending: group.length,
|
|
781
|
+
byHash: /* @__PURE__ */ new Map()
|
|
782
|
+
};
|
|
783
|
+
for (const candidate of group) yield async () => {
|
|
784
|
+
try {
|
|
785
|
+
const path = options.pathOf(candidate);
|
|
786
|
+
const digest = stage === "sample" ? await sampleHash(path, candidate.bytes, buffers) : await hashFileOfSize(path, candidate.bytes, buffers);
|
|
787
|
+
const members = state.byHash.get(digest);
|
|
788
|
+
if (members) members.push(candidate);
|
|
789
|
+
else state.byHash.set(digest, [candidate]);
|
|
790
|
+
} catch (error) {
|
|
791
|
+
options.onError?.(candidate, error);
|
|
792
|
+
} finally {
|
|
793
|
+
done++;
|
|
794
|
+
if (--state.pending === 0) finish(state);
|
|
795
|
+
onProgress?.({
|
|
796
|
+
stage,
|
|
797
|
+
done,
|
|
798
|
+
total
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
};
|
|
839
802
|
}
|
|
840
|
-
}
|
|
803
|
+
}
|
|
804
|
+
await runLimited(tasks(), options.concurrency, signal);
|
|
805
|
+
return results;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Runs tasks from a lazy iterator with at most `concurrency` in flight, so a
|
|
809
|
+
* million-file stage never materializes a million closures up front.
|
|
810
|
+
*/
|
|
811
|
+
async function runLimited(tasks, concurrency, signal) {
|
|
812
|
+
if (signal?.aborted) throw new AbortError();
|
|
813
|
+
let aborted = false;
|
|
814
|
+
const onAbort = () => {
|
|
815
|
+
aborted = true;
|
|
816
|
+
};
|
|
817
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
818
|
+
const worker = async () => {
|
|
819
|
+
for (;;) {
|
|
820
|
+
if (aborted) return;
|
|
821
|
+
const next = tasks.next();
|
|
822
|
+
if (next.done) return;
|
|
823
|
+
await next.value();
|
|
824
|
+
}
|
|
825
|
+
};
|
|
841
826
|
try {
|
|
842
|
-
|
|
827
|
+
const workers = [];
|
|
828
|
+
for (let i = 0; i < concurrency; i++) workers.push(worker());
|
|
829
|
+
await Promise.all(workers);
|
|
843
830
|
} finally {
|
|
844
831
|
signal?.removeEventListener("abort", onAbort);
|
|
845
832
|
}
|
|
846
|
-
|
|
833
|
+
if (aborted) throw new AbortError();
|
|
847
834
|
}
|
|
848
835
|
/** Streams a file through SHA-256 and returns the hex digest. */
|
|
849
|
-
async function hashFile(path, buffer
|
|
850
|
-
const
|
|
836
|
+
async function hashFile(path, buffer) {
|
|
837
|
+
const hasher = createHash(ALGORITHM);
|
|
851
838
|
const handle = await open(path, "r");
|
|
852
839
|
try {
|
|
840
|
+
const chunk = buffer ?? Buffer.allocUnsafe(READ_BUFFER_BYTES);
|
|
853
841
|
let position = 0;
|
|
854
842
|
for (;;) {
|
|
855
|
-
const { bytesRead } = await handle.read(
|
|
843
|
+
const { bytesRead } = await handle.read(chunk, 0, chunk.length, position);
|
|
856
844
|
if (bytesRead === 0) break;
|
|
857
|
-
|
|
845
|
+
hasher.update(bytesRead === chunk.length ? chunk : chunk.subarray(0, bytesRead));
|
|
858
846
|
position += bytesRead;
|
|
859
847
|
}
|
|
860
848
|
} finally {
|
|
861
849
|
await handle.close();
|
|
862
850
|
}
|
|
863
|
-
return
|
|
851
|
+
return hasher.digest("hex");
|
|
864
852
|
}
|
|
865
|
-
|
|
866
|
-
|
|
853
|
+
/** Hashes a file whose size is known: one read for small files, streaming otherwise. */
|
|
854
|
+
async function hashFileOfSize(path, size, buffers) {
|
|
855
|
+
if (size <= SAMPLE_THRESHOLD) return hash(ALGORITHM, await readWhole(path), "hex");
|
|
856
|
+
const buffer = buffers?.acquire();
|
|
857
|
+
try {
|
|
858
|
+
return await hashFile(path, buffer);
|
|
859
|
+
} finally {
|
|
860
|
+
if (buffer) buffers.release(buffer);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
function readWhole(path) {
|
|
864
|
+
return new Promise((resolve, reject) => {
|
|
865
|
+
readFile(path, (error, data) => error ? reject(error) : resolve(data));
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
async function sampleHash(path, size, buffers) {
|
|
869
|
+
const hasher = createHash(ALGORITHM);
|
|
870
|
+
const buffer = buffers.acquire();
|
|
867
871
|
const handle = await open(path, "r");
|
|
868
872
|
try {
|
|
869
873
|
for (const position of [0, Math.max(0, size - SAMPLE_BYTES)]) {
|
|
870
874
|
const { bytesRead } = await handle.read(buffer, 0, SAMPLE_BYTES, position);
|
|
871
|
-
|
|
875
|
+
hasher.update(buffer.subarray(0, bytesRead));
|
|
872
876
|
}
|
|
873
877
|
} finally {
|
|
878
|
+
buffers.release(buffer);
|
|
874
879
|
await handle.close();
|
|
875
880
|
}
|
|
876
|
-
return
|
|
881
|
+
return hasher.digest("hex");
|
|
877
882
|
}
|
|
878
883
|
function distinctInodes(group) {
|
|
879
884
|
if (group.length < 2) return group;
|
|
880
885
|
const seen = /* @__PURE__ */ new Set();
|
|
881
886
|
return group.filter((candidate) => {
|
|
882
|
-
|
|
883
|
-
seen.
|
|
887
|
+
const key = `${candidate.dev}:${candidate.ino}`;
|
|
888
|
+
if (seen.has(key)) return false;
|
|
889
|
+
seen.add(key);
|
|
884
890
|
return true;
|
|
885
891
|
});
|
|
886
892
|
}
|
|
887
|
-
/** Reuses read buffers so
|
|
893
|
+
/** Reuses read buffers so streaming large files allocates a fixed amount of memory. */
|
|
888
894
|
var BufferPool = class {
|
|
889
895
|
#free = [];
|
|
890
|
-
constructor(size) {
|
|
891
|
-
for (let i = 0; i < size; i++) this.#free.push(Buffer.allocUnsafe(READ_BUFFER_BYTES));
|
|
892
|
-
}
|
|
893
896
|
acquire() {
|
|
894
897
|
return this.#free.pop() ?? Buffer.allocUnsafe(READ_BUFFER_BYTES);
|
|
895
898
|
}
|
|
@@ -898,6 +901,261 @@ var BufferPool = class {
|
|
|
898
901
|
}
|
|
899
902
|
};
|
|
900
903
|
//#endregion
|
|
904
|
+
//#region src/core/gitignore.ts
|
|
905
|
+
/**
|
|
906
|
+
* The compiled rules of one ignore file.
|
|
907
|
+
*
|
|
908
|
+
* `test()` returns `true` when the last matching rule ignores the path,
|
|
909
|
+
* `false` when it un-ignores it, and `undefined` when no rule matches, so
|
|
910
|
+
* several files can be chained with Git's precedence.
|
|
911
|
+
*/
|
|
912
|
+
var GitignoreMatcher = class GitignoreMatcher {
|
|
913
|
+
#runs;
|
|
914
|
+
#ignoreCase;
|
|
915
|
+
constructor(runs, ignoreCase) {
|
|
916
|
+
this.#runs = runs;
|
|
917
|
+
this.#ignoreCase = ignoreCase;
|
|
918
|
+
}
|
|
919
|
+
/** True when the file contains at least one rule. */
|
|
920
|
+
get hasRules() {
|
|
921
|
+
return this.#runs.length > 0;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* @param path Path relative to the ignore file's directory, `/`-separated,
|
|
925
|
+
* without leading or trailing slashes.
|
|
926
|
+
*/
|
|
927
|
+
test(path, isDirectory) {
|
|
928
|
+
const runs = this.#runs;
|
|
929
|
+
let name;
|
|
930
|
+
let folded;
|
|
931
|
+
for (let i = runs.length - 1; i >= 0; i--) {
|
|
932
|
+
const run = runs[i];
|
|
933
|
+
const fast = isDirectory && run.dirFast !== null ? true : run.anyFast !== null;
|
|
934
|
+
const byName = run.anyName !== null || isDirectory && run.dirName !== null;
|
|
935
|
+
if (fast || byName) {
|
|
936
|
+
name ??= path.slice(path.lastIndexOf("/") + 1);
|
|
937
|
+
if (fast) {
|
|
938
|
+
folded ??= this.#ignoreCase ? name.toLowerCase() : name;
|
|
939
|
+
if (run.anyFast?.has(folded) || isDirectory && run.dirFast?.has(folded)) return !run.negated;
|
|
940
|
+
}
|
|
941
|
+
if (run.anyName?.test(name) || isDirectory && run.dirName?.test(name)) return !run.negated;
|
|
942
|
+
}
|
|
943
|
+
if (run.anyPath?.test(path) || isDirectory && run.dirPath?.test(path)) return !run.negated;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/** Compiles the contents of an ignore file (or a list of patterns). */
|
|
947
|
+
static compile(source, options = {}) {
|
|
948
|
+
const lines = typeof source === "string" ? source.split(/\r?\n/) : source;
|
|
949
|
+
const ignoreCase = options.ignoreCase === true;
|
|
950
|
+
const flags = ignoreCase ? "i" : "";
|
|
951
|
+
const runs = [];
|
|
952
|
+
let negated = false;
|
|
953
|
+
let parts = new RunParts();
|
|
954
|
+
const regex = (sources) => sources.length > 0 ? new RegExp(`^(?:${sources.join("|")})$`, flags) : null;
|
|
955
|
+
const flush = () => {
|
|
956
|
+
const run = {
|
|
957
|
+
negated,
|
|
958
|
+
anyFast: parts.anyFast.isEmpty ? null : parts.anyFast,
|
|
959
|
+
dirFast: parts.dirFast.isEmpty ? null : parts.dirFast,
|
|
960
|
+
anyName: regex(parts.anyName),
|
|
961
|
+
dirName: regex(parts.dirName),
|
|
962
|
+
anyPath: regex(parts.anyPath),
|
|
963
|
+
dirPath: regex(parts.dirPath)
|
|
964
|
+
};
|
|
965
|
+
if (run.anyFast || run.dirFast || run.anyName || run.dirName || run.anyPath || run.dirPath) runs.push(run);
|
|
966
|
+
parts = new RunParts();
|
|
967
|
+
};
|
|
968
|
+
for (const line of lines) {
|
|
969
|
+
const rule = parseRule(line);
|
|
970
|
+
if (rule === null) continue;
|
|
971
|
+
if (rule.negated !== negated) {
|
|
972
|
+
flush();
|
|
973
|
+
negated = rule.negated;
|
|
974
|
+
}
|
|
975
|
+
if (rule.fast) {
|
|
976
|
+
const target = rule.dirOnly ? parts.dirFast : parts.anyFast;
|
|
977
|
+
const value = ignoreCase ? rule.fast.value.toLowerCase() : rule.fast.value;
|
|
978
|
+
if (rule.fast.kind === "literal") target.literals.add(value);
|
|
979
|
+
else target.addSuffix(value);
|
|
980
|
+
} else if (rule.nameOnly) (rule.dirOnly ? parts.dirName : parts.anyName).push(rule.source);
|
|
981
|
+
else (rule.dirOnly ? parts.dirPath : parts.anyPath).push(rule.source);
|
|
982
|
+
}
|
|
983
|
+
flush();
|
|
984
|
+
return new GitignoreMatcher(runs, ignoreCase);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
/** Exact names and `*suffix` rules of one run, matched with hash lookups. */
|
|
988
|
+
var FastNames = class {
|
|
989
|
+
literals = /* @__PURE__ */ new Set();
|
|
990
|
+
/** Suffixes keyed by their last character, so a name checks only a few. */
|
|
991
|
+
#suffixes = /* @__PURE__ */ new Map();
|
|
992
|
+
get isEmpty() {
|
|
993
|
+
return this.literals.size === 0 && this.#suffixes.size === 0;
|
|
994
|
+
}
|
|
995
|
+
addSuffix(suffix) {
|
|
996
|
+
const key = suffix[suffix.length - 1];
|
|
997
|
+
const list = this.#suffixes.get(key);
|
|
998
|
+
if (list) list.push(suffix);
|
|
999
|
+
else this.#suffixes.set(key, [suffix]);
|
|
1000
|
+
}
|
|
1001
|
+
has(name) {
|
|
1002
|
+
if (this.literals.has(name)) return true;
|
|
1003
|
+
if (name === "") return false;
|
|
1004
|
+
const list = this.#suffixes.get(name[name.length - 1]);
|
|
1005
|
+
if (list === void 0) return false;
|
|
1006
|
+
for (const suffix of list) if (name.endsWith(suffix)) return true;
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
var RunParts = class {
|
|
1011
|
+
anyFast = new FastNames();
|
|
1012
|
+
dirFast = new FastNames();
|
|
1013
|
+
anyName = [];
|
|
1014
|
+
dirName = [];
|
|
1015
|
+
anyPath = [];
|
|
1016
|
+
dirPath = [];
|
|
1017
|
+
};
|
|
1018
|
+
/** No wildcards, classes, or escapes: the pattern is a literal name (or suffix). */
|
|
1019
|
+
const LITERAL = /^[^*?[\\]+$/;
|
|
1020
|
+
/** Turns one gitignore line into a regex fragment; `null` for blanks and comments. */
|
|
1021
|
+
function parseRule(line) {
|
|
1022
|
+
let pattern = line.replace(/(?<!\\)\s+$/, "");
|
|
1023
|
+
if (pattern === "" || pattern.startsWith("#")) return null;
|
|
1024
|
+
let negated = false;
|
|
1025
|
+
if (pattern.startsWith("!")) {
|
|
1026
|
+
negated = true;
|
|
1027
|
+
pattern = pattern.slice(1);
|
|
1028
|
+
} else if (pattern.startsWith("\\!") || pattern.startsWith("\\#")) pattern = pattern.slice(1);
|
|
1029
|
+
if (pattern === "") return null;
|
|
1030
|
+
let dirOnly = false;
|
|
1031
|
+
if (pattern.endsWith("/") && !pattern.endsWith("\\/")) {
|
|
1032
|
+
dirOnly = true;
|
|
1033
|
+
pattern = pattern.replace(/\/+$/, "");
|
|
1034
|
+
if (pattern === "") return null;
|
|
1035
|
+
}
|
|
1036
|
+
let anchored = false;
|
|
1037
|
+
if (pattern.startsWith("/")) {
|
|
1038
|
+
anchored = true;
|
|
1039
|
+
pattern = pattern.replace(/^\/+/, "");
|
|
1040
|
+
} else if (pattern.includes("/")) anchored = true;
|
|
1041
|
+
if (pattern.startsWith("**/")) {
|
|
1042
|
+
anchored = false;
|
|
1043
|
+
pattern = pattern.replace(/^(\*\*\/)+/, "");
|
|
1044
|
+
}
|
|
1045
|
+
if (pattern === "") return null;
|
|
1046
|
+
const body = globToRegex(pattern);
|
|
1047
|
+
if (body === null) return null;
|
|
1048
|
+
const nameOnly = !anchored && !pattern.includes("/") && !pattern.includes("**");
|
|
1049
|
+
const source = anchored || nameOnly ? body : `(?:.*/)?${body}`;
|
|
1050
|
+
let fast = null;
|
|
1051
|
+
if (nameOnly) {
|
|
1052
|
+
if (LITERAL.test(pattern)) fast = {
|
|
1053
|
+
kind: "literal",
|
|
1054
|
+
value: pattern
|
|
1055
|
+
};
|
|
1056
|
+
else if (pattern.startsWith("*") && LITERAL.test(pattern.slice(1))) fast = {
|
|
1057
|
+
kind: "suffix",
|
|
1058
|
+
value: pattern.slice(1)
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
return {
|
|
1062
|
+
negated,
|
|
1063
|
+
dirOnly,
|
|
1064
|
+
nameOnly,
|
|
1065
|
+
fast,
|
|
1066
|
+
source
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Translates a gitignore glob (already stripped of anchors and trailing
|
|
1071
|
+
* slashes) into regex source. Returns `null` for an unusable pattern.
|
|
1072
|
+
*/
|
|
1073
|
+
function globToRegex(glob) {
|
|
1074
|
+
let out = "";
|
|
1075
|
+
const n = glob.length;
|
|
1076
|
+
for (let i = 0; i < n; i++) {
|
|
1077
|
+
const c = glob[i];
|
|
1078
|
+
if (c === "\\") {
|
|
1079
|
+
if (i + 1 < n) out += escapeRegex(glob[++i]);
|
|
1080
|
+
else out += "\\\\";
|
|
1081
|
+
} else if (c === "*") {
|
|
1082
|
+
if (glob[i + 1] === "*") {
|
|
1083
|
+
let j = i + 1;
|
|
1084
|
+
while (glob[j] === "*") j++;
|
|
1085
|
+
const atStart = i === 0 || glob[i - 1] === "/";
|
|
1086
|
+
if (atStart && j === n) out += ".*";
|
|
1087
|
+
else if (atStart && glob[j] === "/") {
|
|
1088
|
+
out += "(?:.*/)?";
|
|
1089
|
+
j++;
|
|
1090
|
+
} else out += "[^/]*";
|
|
1091
|
+
i = j - 1;
|
|
1092
|
+
} else out += "[^/]*";
|
|
1093
|
+
} else if (c === "?") out += "[^/]";
|
|
1094
|
+
else if (c === "[") {
|
|
1095
|
+
const cls = parseClass(glob, i);
|
|
1096
|
+
if (cls === null) out += "\\[";
|
|
1097
|
+
else {
|
|
1098
|
+
out += cls.source;
|
|
1099
|
+
i = cls.end;
|
|
1100
|
+
}
|
|
1101
|
+
} else if (c === "/") out += "/";
|
|
1102
|
+
else out += escapeRegex(c);
|
|
1103
|
+
}
|
|
1104
|
+
return out;
|
|
1105
|
+
}
|
|
1106
|
+
/** Parses a bracket expression starting at `start`; `null` when it is not closed. */
|
|
1107
|
+
function parseClass(glob, start) {
|
|
1108
|
+
let i = start + 1;
|
|
1109
|
+
let negate = false;
|
|
1110
|
+
if (glob[i] === "!" || glob[i] === "^") {
|
|
1111
|
+
negate = true;
|
|
1112
|
+
i++;
|
|
1113
|
+
}
|
|
1114
|
+
let body = "";
|
|
1115
|
+
let first = true;
|
|
1116
|
+
for (; i < glob.length; i++) {
|
|
1117
|
+
const c = glob[i];
|
|
1118
|
+
if (c === "]" && !first) return {
|
|
1119
|
+
source: negate ? `(?!/)[^${body}]` : `[${body}]`,
|
|
1120
|
+
end: i
|
|
1121
|
+
};
|
|
1122
|
+
first = false;
|
|
1123
|
+
if (c === "\\" && i + 1 < glob.length) body += `\\${glob[++i]}`;
|
|
1124
|
+
else if (c === "[" && glob[i + 1] === ":") {
|
|
1125
|
+
const close = glob.indexOf(":]", i + 2);
|
|
1126
|
+
if (close === -1) return null;
|
|
1127
|
+
const name = glob.slice(i + 2, close);
|
|
1128
|
+
const posix = POSIX_CLASSES[name];
|
|
1129
|
+
if (posix === void 0) return null;
|
|
1130
|
+
body += posix;
|
|
1131
|
+
i = close + 1;
|
|
1132
|
+
} else if (c === "/") return null;
|
|
1133
|
+
else if (c === "-" || c === "]" || c === "^") body += c === "]" ? "\\]" : c === "^" ? "\\^" : "-";
|
|
1134
|
+
else body += escapeInClass(c);
|
|
1135
|
+
}
|
|
1136
|
+
return null;
|
|
1137
|
+
}
|
|
1138
|
+
const POSIX_CLASSES = {
|
|
1139
|
+
alnum: "a-zA-Z0-9",
|
|
1140
|
+
alpha: "a-zA-Z",
|
|
1141
|
+
blank: " \\t",
|
|
1142
|
+
cntrl: "\\x00-\\x1f\\x7f",
|
|
1143
|
+
digit: "0-9",
|
|
1144
|
+
graph: "\\x21-\\x7e",
|
|
1145
|
+
lower: "a-z",
|
|
1146
|
+
print: "\\x20-\\x7e",
|
|
1147
|
+
punct: "!-\\/:-@\\[-`{-~",
|
|
1148
|
+
space: " \\t\\r\\n\\v\\f",
|
|
1149
|
+
upper: "A-Z",
|
|
1150
|
+
xdigit: "0-9A-Fa-f"
|
|
1151
|
+
};
|
|
1152
|
+
function escapeRegex(c) {
|
|
1153
|
+
return /[\\^$.*+?()[\]{}|/-]/.test(c) ? `\\${c}` : c;
|
|
1154
|
+
}
|
|
1155
|
+
function escapeInClass(c) {
|
|
1156
|
+
return c === "\\" ? "\\\\" : c;
|
|
1157
|
+
}
|
|
1158
|
+
//#endregion
|
|
901
1159
|
//#region src/core/exclude.ts
|
|
902
1160
|
/** Directories skipped unless default excludes are turned off. */
|
|
903
1161
|
const DEFAULT_EXCLUDES = [
|
|
@@ -923,7 +1181,8 @@ var Excluder = class {
|
|
|
923
1181
|
this.#ignoreCase = options.ignoreCase ?? process.platform !== "linux";
|
|
924
1182
|
this.#defaults = options.defaultExcludes ?? true ? new Set(DEFAULT_EXCLUDES) : null;
|
|
925
1183
|
const patterns = (options.patterns ?? []).map((p) => p.trim()).filter(Boolean);
|
|
926
|
-
|
|
1184
|
+
const user = patterns.length > 0 ? this.#compile(patterns) : null;
|
|
1185
|
+
this.#user = user?.hasRules ? user : null;
|
|
927
1186
|
}
|
|
928
1187
|
/** True when at least one rule could exclude something. */
|
|
929
1188
|
get active() {
|
|
@@ -939,7 +1198,7 @@ var Excluder = class {
|
|
|
939
1198
|
*/
|
|
940
1199
|
isExcluded(relPath, name, isDirectory, chain) {
|
|
941
1200
|
if (isDirectory && this.#defaults?.has(name)) return true;
|
|
942
|
-
if (this.#user !== null && this.#user.
|
|
1201
|
+
if (this.#user !== null && this.#user.test(relPath, isDirectory) === true) return true;
|
|
943
1202
|
return chain.length > 0 && isGitIgnored(chain, relPath, isDirectory);
|
|
944
1203
|
}
|
|
945
1204
|
/**
|
|
@@ -960,10 +1219,12 @@ var Excluder = class {
|
|
|
960
1219
|
}
|
|
961
1220
|
/** Builds the scope for a `.gitignore` found at `rootRelativeDir` (`''` for the root). */
|
|
962
1221
|
async loadScope(filePath, rootRelativeDir) {
|
|
963
|
-
const content = await readFile(filePath, "utf8").catch(() => null);
|
|
964
|
-
if (content === null
|
|
1222
|
+
const content = await readFile$1(filePath, "utf8").catch(() => null);
|
|
1223
|
+
if (content === null) return null;
|
|
1224
|
+
const matcher = this.#compile(content);
|
|
1225
|
+
if (!matcher.hasRules) return null;
|
|
965
1226
|
return {
|
|
966
|
-
matcher
|
|
1227
|
+
matcher,
|
|
967
1228
|
prefix: "",
|
|
968
1229
|
strip: rootRelativeDir ? rootRelativeDir.length + 1 : 0
|
|
969
1230
|
};
|
|
@@ -992,11 +1253,13 @@ var Excluder = class {
|
|
|
992
1253
|
const scopes = [];
|
|
993
1254
|
const outermostFirst = repoRoot === root ? [] : ancestors.reverse();
|
|
994
1255
|
const add = async (file, base) => {
|
|
995
|
-
const content = await readFile(file, "utf8").catch(() => null);
|
|
996
|
-
if (content === null
|
|
1256
|
+
const content = await readFile$1(file, "utf8").catch(() => null);
|
|
1257
|
+
if (content === null) return;
|
|
1258
|
+
const matcher = this.#compile(content);
|
|
1259
|
+
if (!matcher.hasRules) return;
|
|
997
1260
|
const prefix = relative(base, root).split(sep).join("/");
|
|
998
1261
|
scopes.push({
|
|
999
|
-
matcher
|
|
1262
|
+
matcher,
|
|
1000
1263
|
prefix: prefix ? `${prefix}/` : "",
|
|
1001
1264
|
strip: 0
|
|
1002
1265
|
});
|
|
@@ -1005,11 +1268,8 @@ var Excluder = class {
|
|
|
1005
1268
|
for (const dir of outermostFirst) await add(join(dir, ".gitignore"), dir);
|
|
1006
1269
|
return scopes;
|
|
1007
1270
|
}
|
|
1008
|
-
#
|
|
1009
|
-
return
|
|
1010
|
-
ignorecase: this.#ignoreCase,
|
|
1011
|
-
allowRelativePaths: true
|
|
1012
|
-
});
|
|
1271
|
+
#compile(source) {
|
|
1272
|
+
return GitignoreMatcher.compile(source, { ignoreCase: this.#ignoreCase });
|
|
1013
1273
|
}
|
|
1014
1274
|
};
|
|
1015
1275
|
function isGitIgnored(chain, relPath, isDirectory) {
|
|
@@ -1017,13 +1277,79 @@ function isGitIgnored(chain, relPath, isDirectory) {
|
|
|
1017
1277
|
for (const scope of chain) {
|
|
1018
1278
|
const local = scope.prefix + relPath.slice(scope.strip);
|
|
1019
1279
|
if (local === "") continue;
|
|
1020
|
-
const result = scope.matcher.test(
|
|
1021
|
-
if (result
|
|
1022
|
-
else if (result.unignored) ignored = false;
|
|
1280
|
+
const result = scope.matcher.test(local, isDirectory);
|
|
1281
|
+
if (result !== void 0) ignored = result;
|
|
1023
1282
|
}
|
|
1024
1283
|
return ignored;
|
|
1025
1284
|
}
|
|
1026
1285
|
//#endregion
|
|
1286
|
+
//#region src/core/limiter.ts
|
|
1287
|
+
/**
|
|
1288
|
+
* A tiny concurrency limiter for filesystem work.
|
|
1289
|
+
*
|
|
1290
|
+
* Tasks run at most `concurrency` at a time. Pending tasks are taken
|
|
1291
|
+
* last-in-first-out, which makes a directory walk proceed depth-first and
|
|
1292
|
+
* keeps the pending queue small even on very wide trees.
|
|
1293
|
+
*/
|
|
1294
|
+
var Limiter = class {
|
|
1295
|
+
#concurrency;
|
|
1296
|
+
#queue = [];
|
|
1297
|
+
#active = 0;
|
|
1298
|
+
#failure = void 0;
|
|
1299
|
+
#failed = false;
|
|
1300
|
+
#idle = null;
|
|
1301
|
+
constructor(concurrency) {
|
|
1302
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) throw new RangeError(`concurrency must be a positive integer (got ${concurrency})`);
|
|
1303
|
+
this.#concurrency = concurrency;
|
|
1304
|
+
}
|
|
1305
|
+
/** Schedules a task. Tasks are expected to handle their own errors; a thrown error aborts the run. */
|
|
1306
|
+
run(task) {
|
|
1307
|
+
if (this.#failed) return;
|
|
1308
|
+
this.#queue.push(task);
|
|
1309
|
+
this.#pump();
|
|
1310
|
+
}
|
|
1311
|
+
/** Resolves once every scheduled task, including tasks scheduled by tasks, has finished. */
|
|
1312
|
+
idle() {
|
|
1313
|
+
if (this.#failed) return Promise.reject(this.#failure);
|
|
1314
|
+
if (this.#active === 0 && this.#queue.length === 0) return Promise.resolve();
|
|
1315
|
+
return new Promise((resolve, reject) => {
|
|
1316
|
+
this.#idle = {
|
|
1317
|
+
resolve,
|
|
1318
|
+
reject
|
|
1319
|
+
};
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
/** Stops scheduling new work and rejects `idle()` with `reason`. */
|
|
1323
|
+
abort(reason) {
|
|
1324
|
+
if (this.#failed) return;
|
|
1325
|
+
this.#failed = true;
|
|
1326
|
+
this.#failure = reason;
|
|
1327
|
+
this.#queue.length = 0;
|
|
1328
|
+
this.#idle?.reject(reason);
|
|
1329
|
+
this.#idle = null;
|
|
1330
|
+
}
|
|
1331
|
+
#pump() {
|
|
1332
|
+
while (this.#active < this.#concurrency && this.#queue.length > 0) {
|
|
1333
|
+
const task = this.#queue.pop();
|
|
1334
|
+
this.#active++;
|
|
1335
|
+
task().then(this.#onDone, this.#onError);
|
|
1336
|
+
}
|
|
1337
|
+
if (this.#active === 0 && this.#queue.length === 0 && this.#idle) {
|
|
1338
|
+
const idle = this.#idle;
|
|
1339
|
+
this.#idle = null;
|
|
1340
|
+
idle.resolve();
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
#onDone = () => {
|
|
1344
|
+
this.#active--;
|
|
1345
|
+
this.#pump();
|
|
1346
|
+
};
|
|
1347
|
+
#onError = (error) => {
|
|
1348
|
+
this.#active--;
|
|
1349
|
+
this.abort(error);
|
|
1350
|
+
};
|
|
1351
|
+
};
|
|
1352
|
+
//#endregion
|
|
1027
1353
|
//#region src/core/paths.ts
|
|
1028
1354
|
/** Ordinal comparison, so output order never depends on the locale. */
|
|
1029
1355
|
function comparePaths(a, b) {
|
|
@@ -1123,6 +1449,7 @@ var Scanner = class {
|
|
|
1123
1449
|
try {
|
|
1124
1450
|
entries = await readdir(absPath, { withFileTypes: true });
|
|
1125
1451
|
} catch (error) {
|
|
1452
|
+
this.dirs[index].entries = -1;
|
|
1126
1453
|
this.recordError(this.relativePath(index), error);
|
|
1127
1454
|
return;
|
|
1128
1455
|
}
|
|
@@ -1296,14 +1623,14 @@ const ERROR_DESCRIPTIONS = {
|
|
|
1296
1623
|
//#endregion
|
|
1297
1624
|
//#region src/core/options.ts
|
|
1298
1625
|
/** Default number of concurrent filesystem operations. */
|
|
1299
|
-
const DEFAULT_CONCURRENCY =
|
|
1626
|
+
const DEFAULT_CONCURRENCY = 32;
|
|
1300
1627
|
/** Validates the root and turns user-facing settings into scanner options. */
|
|
1301
1628
|
async function resolveScan(path, input, now) {
|
|
1302
1629
|
const root = resolve(path);
|
|
1303
1630
|
await assertDirectory(root);
|
|
1304
1631
|
const maxDepth = input.maxDepth ?? null;
|
|
1305
1632
|
if (maxDepth !== null && (!Number.isInteger(maxDepth) || maxDepth < 0)) throw new DatError(`maxDepth must be a non-negative integer (got ${maxDepth}).`);
|
|
1306
|
-
const concurrency = input.concurrency ??
|
|
1633
|
+
const concurrency = input.concurrency ?? 32;
|
|
1307
1634
|
if (!Number.isInteger(concurrency) || concurrency < 1) throw new DatError(`concurrency must be a positive integer (got ${concurrency}).`);
|
|
1308
1635
|
const minSize = toBytes(input.minSize, "minSize");
|
|
1309
1636
|
const maxSize = toBytes(input.maxSize, "maxSize");
|
|
@@ -1509,12 +1836,20 @@ async function analyze(path, options = {}) {
|
|
|
1509
1836
|
let duplicates;
|
|
1510
1837
|
if (features.duplicates) {
|
|
1511
1838
|
progress.stop();
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1839
|
+
const interval = options.progressInterval ?? 100;
|
|
1840
|
+
let last = 0;
|
|
1841
|
+
duplicates = await collector.findDuplicates(scanOptions.concurrency, scanOptions.signal, (p) => {
|
|
1842
|
+
if (!report) return;
|
|
1843
|
+
const now = performance.now();
|
|
1844
|
+
if (p.done !== p.total && p.done !== 0 && now - last < interval) return;
|
|
1845
|
+
last = now;
|
|
1846
|
+
report({
|
|
1847
|
+
phase: "hash",
|
|
1848
|
+
files: scanner.counters.files,
|
|
1849
|
+
bytes: scanner.counters.bytes,
|
|
1850
|
+
...p
|
|
1851
|
+
});
|
|
1852
|
+
});
|
|
1518
1853
|
}
|
|
1519
1854
|
const result = collector.build(scanner, {
|
|
1520
1855
|
root: scanOptions.root,
|
|
@@ -1666,8 +2001,8 @@ var Collector = class {
|
|
|
1666
2001
|
dir,
|
|
1667
2002
|
name,
|
|
1668
2003
|
bytes,
|
|
1669
|
-
|
|
1670
|
-
|
|
2004
|
+
dev: stats.dev,
|
|
2005
|
+
ino: stats.ino
|
|
1671
2006
|
};
|
|
1672
2007
|
const existing = this.#bySize.get(bytes);
|
|
1673
2008
|
if (existing === void 0) this.#bySize.set(bytes, candidate);
|
|
@@ -1687,7 +2022,7 @@ var Collector = class {
|
|
|
1687
2022
|
if (dir === void 0) dirPaths.set(c.dir, dir = scanner.absolutePath(c.dir));
|
|
1688
2023
|
return joinRelative(dir, c.name);
|
|
1689
2024
|
},
|
|
1690
|
-
concurrency
|
|
2025
|
+
concurrency,
|
|
1691
2026
|
signal,
|
|
1692
2027
|
onProgress,
|
|
1693
2028
|
onError: (c, error) => scanner.recordError(scanner.relativePath(c.dir, c.name), error)
|
|
@@ -1943,7 +2278,7 @@ async function compare(left, right, options = {}) {
|
|
|
1943
2278
|
}
|
|
1944
2279
|
for (const [path, r] of rightIndex.files) if (!leftIndex.files.has(path)) added.push(toEntry(path, r));
|
|
1945
2280
|
if (sameSize.length > 0) {
|
|
1946
|
-
const differing = await findContentChanges(sameSize, a.options.root, b.options.root, options);
|
|
2281
|
+
const differing = await findContentChanges(sameSize, (path) => leftIndex.files.get(path).bytes, a.options.root, b.options.root, options);
|
|
1947
2282
|
for (const path of sameSize) if (differing.has(path)) {
|
|
1948
2283
|
const bytes = leftIndex.files.get(path).bytes;
|
|
1949
2284
|
changed.push({
|
|
@@ -2020,15 +2355,16 @@ function side(root, scanner, index) {
|
|
|
2020
2355
|
types: index.types
|
|
2021
2356
|
};
|
|
2022
2357
|
}
|
|
2023
|
-
async function findContentChanges(paths, leftRoot, rightRoot, options) {
|
|
2358
|
+
async function findContentChanges(paths, sizeOf, leftRoot, rightRoot, options) {
|
|
2024
2359
|
const differing = /* @__PURE__ */ new Set();
|
|
2025
|
-
const limiter = new Limiter(
|
|
2360
|
+
const limiter = new Limiter(options.concurrency ?? 32);
|
|
2026
2361
|
const onAbort = () => limiter.abort(new AbortError());
|
|
2027
2362
|
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
2028
2363
|
let done = 0;
|
|
2029
2364
|
for (const path of paths) limiter.run(async () => {
|
|
2030
2365
|
try {
|
|
2031
|
-
const
|
|
2366
|
+
const bytes = sizeOf(path);
|
|
2367
|
+
const [l, r] = await Promise.all([hashFileOfSize(joinRelative(leftRoot, path), bytes), hashFileOfSize(joinRelative(rightRoot, path), bytes)]);
|
|
2032
2368
|
if (l !== r) differing.add(path);
|
|
2033
2369
|
} catch {
|
|
2034
2370
|
differing.add(path);
|
|
@@ -2400,7 +2736,7 @@ async function loadConfig(options) {
|
|
|
2400
2736
|
};
|
|
2401
2737
|
let text;
|
|
2402
2738
|
try {
|
|
2403
|
-
text = await readFile(path, "utf8");
|
|
2739
|
+
text = await readFile$1(path, "utf8");
|
|
2404
2740
|
} catch (error) {
|
|
2405
2741
|
throw new DatError(errorCode(error) === "ENOENT" ? `Config file not found: ${path}` : `Cannot read config ${path}: ${errorMessage(error)}`);
|
|
2406
2742
|
}
|
|
@@ -2572,7 +2908,7 @@ function renderAnalysis(result, options = {}) {
|
|
|
2572
2908
|
row("Files", formatCount(sum.files));
|
|
2573
2909
|
row("Folders", formatCount(sum.directories));
|
|
2574
2910
|
if (sum.symlinks > 0) row("Symlinks", `${formatCount(sum.symlinks)} ${s.dim(result.settings.followSymlinks ? "(followed)" : "(not followed)")}`);
|
|
2575
|
-
if (sum.hardlinks > 0) row("Hardlinks", `${formatCount(sum.hardlinks)} ${s.dim(
|
|
2911
|
+
if (sum.hardlinks > 0) row("Hardlinks", `${formatCount(sum.hardlinks)} ${s.dim(`extra ${sum.hardlinks === 1 ? "link" : "links"} counted once`)}`);
|
|
2576
2912
|
const skipped = [];
|
|
2577
2913
|
if (sum.excluded > 0) skipped.push(`${formatCount(sum.excluded)} excluded`);
|
|
2578
2914
|
if (sum.filtered > 0) skipped.push(`${formatCount(sum.filtered)} filtered`);
|
|
@@ -2596,7 +2932,7 @@ function renderAnalysis(result, options = {}) {
|
|
|
2596
2932
|
}
|
|
2597
2933
|
if (result.largeFiles) {
|
|
2598
2934
|
const lf = result.largeFiles;
|
|
2599
|
-
r.heading(`Large files ≥ ${
|
|
2935
|
+
r.heading(`Large files ≥ ${formatBytes(lf.threshold, unitsOf(lf.threshold, r.units))}`, lf.count > 0 ? `${plural(lf.count, "file")} · ${r.bytes(lf.bytes)}` : "none");
|
|
2600
2936
|
r.fileList(lf.items, lf.count);
|
|
2601
2937
|
}
|
|
2602
2938
|
if (result.duplicates) renderDuplicates(r, result.duplicates);
|
|
@@ -2633,12 +2969,7 @@ function renderAnalysis(result, options = {}) {
|
|
|
2633
2969
|
const failed = result.budgets.filter((b) => !b.passed).length;
|
|
2634
2970
|
r.heading("Budgets", failed > 0 ? s.red(`${failed} failed`) : s.green("all passed"));
|
|
2635
2971
|
for (const budget of result.budgets) {
|
|
2636
|
-
const actual =
|
|
2637
|
-
"size",
|
|
2638
|
-
"disk",
|
|
2639
|
-
"largest",
|
|
2640
|
-
"wasted"
|
|
2641
|
-
].includes(budget.metric) ? r.bytes(budget.actual) : formatCount(budget.actual);
|
|
2972
|
+
const actual = formatBudgetActual(budget, r.units);
|
|
2642
2973
|
const mark = budget.passed ? s.green("✔") : s.red("✘");
|
|
2643
2974
|
r.line(` ${mark} ${padEndVisible(budget.rule, 24)} ${s.dim("actual")} ${budget.passed ? actual : s.red(actual)}`);
|
|
2644
2975
|
}
|
|
@@ -2701,7 +3032,7 @@ function renderComparison(result, options = {}) {
|
|
|
2701
3032
|
const signed = (n) => n > 0 ? s.green(`+${r.bytes(n)}`) : n < 0 ? s.red(r.bytes(n)) : s.dim("±0");
|
|
2702
3033
|
const signedCount = (n) => n > 0 ? s.green(`+${formatCount(n)}`) : n < 0 ? s.red(`-${formatCount(-n)}`) : s.dim("±0");
|
|
2703
3034
|
const pct = (left, right) => {
|
|
2704
|
-
if (left === 0) return "";
|
|
3035
|
+
if (left === 0 || left === right) return "";
|
|
2705
3036
|
const value = (right - left) / left * 100;
|
|
2706
3037
|
return s.dim(` (${value > 0 ? "+" : ""}${Math.abs(value) < .1 && value !== 0 ? value.toFixed(2) : value.toFixed(1)}%)`);
|
|
2707
3038
|
};
|
|
@@ -2724,7 +3055,7 @@ function renderComparison(result, options = {}) {
|
|
|
2724
3055
|
for (const c of changedTypes) {
|
|
2725
3056
|
const l = result.left.types[c];
|
|
2726
3057
|
const rt = result.right.types[c];
|
|
2727
|
-
r.line(` ${padEndVisible(CATEGORY_LABELS[c], 10)}${col(r.bytes(l.bytes))}${col(r.bytes(rt.bytes))} ${signed(rt.bytes - l.bytes)} ${s.dim(`${signedCount(rt.files - l.files)} files`)}`);
|
|
3058
|
+
r.line(` ${padEndVisible(CATEGORY_LABELS[c], 10)}${col(r.bytes(l.bytes))}${col(r.bytes(rt.bytes))} ${signed(rt.bytes - l.bytes)} ${s.dim(`${signedCount(rt.files - l.files)} ${Math.abs(rt.files - l.files) === 1 ? "file" : "files"}`)}`);
|
|
2728
3059
|
}
|
|
2729
3060
|
}
|
|
2730
3061
|
const fileSection = (title, list, mark) => {
|
|
@@ -2804,12 +3135,7 @@ function renderAnalysisMarkdown(result, options = {}) {
|
|
|
2804
3135
|
], result.budgets.map((x) => [
|
|
2805
3136
|
x.passed ? "✅" : "❌",
|
|
2806
3137
|
mdCode(x.rule),
|
|
2807
|
-
|
|
2808
|
-
"size",
|
|
2809
|
-
"disk",
|
|
2810
|
-
"largest",
|
|
2811
|
-
"wasted"
|
|
2812
|
-
].includes(x.metric) ? b(x.actual) : formatCount(x.actual)
|
|
3138
|
+
formatBudgetActual(x, units)
|
|
2813
3139
|
])));
|
|
2814
3140
|
}
|
|
2815
3141
|
const types = CATEGORIES.filter((c) => result.types[c].files > 0).sort((x, y) => result.types[y].bytes - result.types[x].bytes);
|
|
@@ -2854,7 +3180,7 @@ function renderAnalysisMarkdown(result, options = {}) {
|
|
|
2854
3180
|
}
|
|
2855
3181
|
if (result.largeFiles && result.largeFiles.count > 0) {
|
|
2856
3182
|
const lf = result.largeFiles;
|
|
2857
|
-
out.push("", `### Large files (≥ ${
|
|
3183
|
+
out.push("", `### Large files (≥ ${formatBytes(lf.threshold, unitsOf(lf.threshold, units))}): ${plural(lf.count, "file")}, ${b(lf.bytes)}`, "");
|
|
2858
3184
|
fileTable(lf.items, lf.count);
|
|
2859
3185
|
}
|
|
2860
3186
|
if (result.duplicates) {
|
|
@@ -3137,6 +3463,8 @@ function renderComparisonCsv(result) {
|
|
|
3137
3463
|
}
|
|
3138
3464
|
//#endregion
|
|
3139
3465
|
//#region src/reporters/html.ts
|
|
3466
|
+
/** Duplicate groups listed in the HTML report; the full list is in JSON and CSV. */
|
|
3467
|
+
const DUPLICATE_GROUPS_SHOWN = 100;
|
|
3140
3468
|
const CATEGORY_COLORS = {
|
|
3141
3469
|
code: "#6366f1",
|
|
3142
3470
|
documents: "#0ea5e9",
|
|
@@ -3167,17 +3495,12 @@ function renderAnalysisHtml(result, options = {}) {
|
|
|
3167
3495
|
const fileTable = (items, total, showModified = false) => {
|
|
3168
3496
|
const shown = items.slice(0, limit);
|
|
3169
3497
|
const max = shown[0]?.bytes ?? 0;
|
|
3170
|
-
return `<table class="filterable"><thead><tr><th class="num">Size</th><th>Path</th>${showModified ? "<th>Modified</th>" : ""}</tr></thead><tbody>${shown.map((f) => `<tr><td class="num">${e(b(f.bytes))}</td><td class="path">${meter(f.bytes, max)}<span>${e(f.path)}</span></td>${showModified ? `<td class="muted">${e(f.modified.slice(0, 10))}</td>` : ""}</tr>`).join("")}</tbody></table>${truncatedNote(shown.length, total)}`;
|
|
3498
|
+
return `<table class="filterable"><thead><tr><th class="num">Size</th><th>Path</th>${showModified ? "<th>Modified</th>" : ""}</tr></thead><tbody>${shown.map((f) => `<tr><td class="num">${e(b(f.bytes))}</td><td class="path">${meter(f.bytes, max)}<span>${e(f.path)}</span></td>${showModified ? `<td class="muted date">${e(f.modified.slice(0, 10))}</td>` : ""}</tr>`).join("")}</tbody></table>${truncatedNote(shown.length, total)}`;
|
|
3171
3499
|
};
|
|
3172
3500
|
if (result.budgets && result.budgets.length > 0) {
|
|
3173
3501
|
const failed = result.budgets.filter((x) => !x.passed).length;
|
|
3174
3502
|
sections.push(section("Budgets", failed > 0 ? `${failed} failed` : "all passed", `<ul class="budgets">${result.budgets.map((x) => {
|
|
3175
|
-
const actual =
|
|
3176
|
-
"size",
|
|
3177
|
-
"disk",
|
|
3178
|
-
"largest",
|
|
3179
|
-
"wasted"
|
|
3180
|
-
].includes(x.metric) ? b(x.actual) : formatCount(x.actual);
|
|
3503
|
+
const actual = formatBudgetActual(x, units);
|
|
3181
3504
|
return `<li class="${x.passed ? "pass" : "fail"}"><span>${x.passed ? "✔" : "✘"}</span><code>${e(x.rule)}</code><span class="muted">actual ${e(actual)}</span></li>`;
|
|
3182
3505
|
}).join("")}</ul>`));
|
|
3183
3506
|
}
|
|
@@ -3197,12 +3520,12 @@ function renderAnalysisHtml(result, options = {}) {
|
|
|
3197
3520
|
}
|
|
3198
3521
|
if (result.largeFiles) {
|
|
3199
3522
|
const lf = result.largeFiles;
|
|
3200
|
-
sections.push(section(`Large files ≥ ${
|
|
3523
|
+
sections.push(section(`Large files ≥ ${formatBytes(lf.threshold, unitsOf(lf.threshold, units))}`, lf.count > 0 ? `${plural(lf.count, "file")} · ${b(lf.bytes)}` : "none", lf.count > 0 ? fileTable(lf.items, lf.count, true) : ""));
|
|
3201
3524
|
}
|
|
3202
3525
|
if (result.duplicates) {
|
|
3203
3526
|
const d = result.duplicates;
|
|
3204
|
-
const groups = d.items.slice(0, limit);
|
|
3205
|
-
sections.push(section("Duplicates", d.count > 0 ? `${plural(d.count, "group")} · ${b(d.wastedBytes)} reclaimable` : "none found", groups.map((g) => `<details class="group"><summary><strong>${e(b(g.wastedBytes))}</strong>
|
|
3527
|
+
const groups = d.items.slice(0, Math.min(limit, DUPLICATE_GROUPS_SHOWN));
|
|
3528
|
+
sections.push(section("Duplicates", d.count > 0 ? `${plural(d.count, "group")} · ${b(d.wastedBytes)} reclaimable` : "none found", groups.map((g) => `<details class="group"><summary><strong>${e(b(g.wastedBytes))}</strong> <span class="muted">${g.files.length} × ${e(b(g.bytes))}</span> <span class="path first">${e(g.files[0])}</span>${g.files.length > 1 ? ` <span class="muted">and ${g.files.length - 1} more</span>` : ""}</summary><ul>${g.files.map((f) => `<li class="path">${e(f)}</li>`).join("")}</ul><p class="muted hash">sha256 ${e(g.hash)}</p></details>`).join("") + truncatedNote(groups.length, d.count)));
|
|
3206
3529
|
}
|
|
3207
3530
|
if (result.emptyFiles || result.emptyDirectories) {
|
|
3208
3531
|
const files = result.emptyFiles?.items ?? [];
|
|
@@ -3318,13 +3641,15 @@ th{font-size:12px;text-transform:uppercase;letter-spacing:.05em;color:var(--mute
|
|
|
3318
3641
|
tr:last-child td{border-bottom:0}
|
|
3319
3642
|
.num{text-align:right;white-space:nowrap}
|
|
3320
3643
|
td.path{position:relative;word-break:break-all}
|
|
3644
|
+
td.date{white-space:nowrap}
|
|
3321
3645
|
td.path span{position:relative}
|
|
3322
3646
|
.meter{position:absolute;left:0;top:4px;bottom:4px;background:var(--meter);border-radius:4px}
|
|
3323
3647
|
.types{display:grid;grid-template-columns:180px 1fr;gap:24px;align-items:center}
|
|
3324
3648
|
.donut{width:180px;height:180px;transform:rotate(0)}
|
|
3325
3649
|
.swatch{display:inline-block;width:10px;height:10px;border-radius:3px;margin-right:8px}
|
|
3326
3650
|
.group{border:1px solid var(--line);border-radius:10px;padding:10px 14px;margin:8px 0}
|
|
3327
|
-
.group summary{cursor:pointer}
|
|
3651
|
+
.group summary{cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
3652
|
+
.group .first{margin-left:6px}
|
|
3328
3653
|
.group ul,.plain{margin:8px 0 0;padding-left:18px}
|
|
3329
3654
|
.hash{font-family:ui-monospace,monospace;font-size:11px;margin:6px 0 0;word-break:break-all}
|
|
3330
3655
|
.budgets{list-style:none;padding:0;margin:0}
|
|
@@ -3332,7 +3657,11 @@ td.path span{position:relative}
|
|
|
3332
3657
|
.pass span:first-child{color:var(--ok)}.fail span:first-child{color:var(--bad)}.fail code{color:var(--bad)}
|
|
3333
3658
|
.tree details>details,.tree details>.leaf{margin-left:22px}
|
|
3334
3659
|
.tree summary,.tree .leaf{display:flex;gap:12px;padding:3px 0;cursor:default}
|
|
3335
|
-
.tree summary{cursor:pointer}
|
|
3660
|
+
.tree summary{cursor:pointer;list-style:none}
|
|
3661
|
+
.tree summary::-webkit-details-marker{display:none}
|
|
3662
|
+
.tree summary::before,.tree .leaf::before{content:'';width:12px;flex:none;color:var(--muted)}
|
|
3663
|
+
.tree summary::before{content:'▸'}
|
|
3664
|
+
.tree details[open]>summary::before{content:'▾'}
|
|
3336
3665
|
.tree .num{min-width:84px}.tree .share{min-width:40px;color:var(--muted);font-size:12px;text-align:right}
|
|
3337
3666
|
.hidden{display:none}
|
|
3338
3667
|
footer{padding:24px;color:var(--muted);font-size:13px;text-align:center}
|
|
@@ -3348,4 +3677,4 @@ document.getElementById('filter').addEventListener('input', function (event) {
|
|
|
3348
3677
|
});
|
|
3349
3678
|
`;
|
|
3350
3679
|
//#endregion
|
|
3351
|
-
export {
|
|
3680
|
+
export { parseBudget as A, errorMessage as B, DEFAULT_EXCLUDES as C, BUDGET_METRICS as D, extensionOf as E, parseDuration as F, parsePointInTime as I, AbortError as L, parseSize as M, CATEGORIES as N, evaluateBudgets as O, VERSION as P, DatError as R, joinRelative as S, classify as T, DEFAULT_LARGE_FILE_THRESHOLD as _, renderComparisonMarkdown as a, DEFAULT_CONCURRENCY as b, formatCount as c, CONFIG_FILE_NAMES as d, findConfigFile as f, compare as g, validateConfig as h, renderAnalysisMarkdown as i, formatBytes as j, formatBudgetActual as k, createStyle as l, buildJsonSchema as m, renderAnalysisCsv as n, renderAnalysis as o, loadConfig as p, renderComparisonCsv as r, renderComparison as s, renderAnalysisHtml as t, supportsColor as u, DEFAULT_STALE_AGE as v, Excluder as w, listDirectories as x, analyze as y, errorCode as z };
|