@nomicfoundation/hardhat-utils 4.0.5 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/src/debug.d.ts +40 -8
- package/dist/src/debug.d.ts.map +1 -1
- package/dist/src/debug.js +54 -18
- package/dist/src/debug.js.map +1 -1
- package/dist/src/fast-semver.d.ts +49 -0
- package/dist/src/fast-semver.d.ts.map +1 -0
- package/dist/src/fast-semver.js +73 -0
- package/dist/src/fast-semver.js.map +1 -0
- package/dist/src/fs.d.ts +47 -0
- package/dist/src/fs.d.ts.map +1 -1
- package/dist/src/fs.js +183 -13
- package/dist/src/fs.js.map +1 -1
- package/dist/src/internal/debug.d.ts +35 -0
- package/dist/src/internal/debug.d.ts.map +1 -0
- package/dist/src/internal/debug.js +91 -0
- package/dist/src/internal/debug.js.map +1 -0
- package/dist/src/subprocess.d.ts +2 -0
- package/dist/src/subprocess.d.ts.map +1 -1
- package/dist/src/subprocess.js +1 -0
- package/dist/src/subprocess.js.map +1 -1
- package/dist/src/synchronization.d.ts +97 -0
- package/dist/src/synchronization.d.ts.map +1 -1
- package/dist/src/synchronization.js +171 -2
- package/dist/src/synchronization.js.map +1 -1
- package/package.json +3 -4
- package/src/debug.ts +73 -28
- package/src/fast-semver.ts +95 -0
- package/src/fs.ts +249 -23
- package/src/internal/debug.ts +119 -0
- package/src/subprocess.ts +2 -0
- package/src/synchronization.ts +211 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomicfoundation/hardhat-utils",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Utilities for Hardhat and its plugins",
|
|
5
5
|
"homepage": "https://github.com/NomicFoundation/hardhat/tree/main/packages/hardhat-utils",
|
|
6
6
|
"repository": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"./env": "./dist/src/env.js",
|
|
24
24
|
"./error": "./dist/src/error.js",
|
|
25
25
|
"./eth": "./dist/src/eth.js",
|
|
26
|
+
"./fast-semver": "./dist/src/fast-semver.js",
|
|
26
27
|
"./format": "./dist/src/format.js",
|
|
27
28
|
"./fs": "./dist/src/fs.js",
|
|
28
29
|
"./global-dir": "./dist/src/global-dir.js",
|
|
@@ -53,9 +54,8 @@
|
|
|
53
54
|
"README.md"
|
|
54
55
|
],
|
|
55
56
|
"devDependencies": {
|
|
56
|
-
"@nomicfoundation/hardhat-node-test-reporter": "^3.0.
|
|
57
|
+
"@nomicfoundation/hardhat-node-test-reporter": "^3.0.6",
|
|
57
58
|
"@types/bn.js": "^5.1.5",
|
|
58
|
-
"@types/debug": "^4.1.7",
|
|
59
59
|
"@types/node": "^22.0.0",
|
|
60
60
|
"c8": "^9.1.0",
|
|
61
61
|
"eslint": "9.25.1",
|
|
@@ -67,7 +67,6 @@
|
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"@streamparser/json-node": "^0.0.22",
|
|
70
|
-
"debug": "^4.3.2",
|
|
71
70
|
"env-paths": "^2.2.0",
|
|
72
71
|
"ethereum-cryptography": "^2.2.1",
|
|
73
72
|
"fast-equals": "^5.4.0",
|
package/src/debug.ts
CHANGED
|
@@ -1,38 +1,83 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { formatWithOptions } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { NOOP, isEnabled, selectColor, useColors } from "./internal/debug.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A logger function returned by {@link createDebug}.
|
|
7
|
+
* The `enabled` property is `true` when the logger will produce output.
|
|
8
|
+
*/
|
|
9
|
+
export interface DebugLogger {
|
|
10
|
+
(format: unknown, ...args: unknown[]): void;
|
|
11
|
+
readonly enabled: boolean;
|
|
12
|
+
}
|
|
2
13
|
|
|
3
14
|
/**
|
|
4
|
-
*
|
|
15
|
+
* Creates a namespaced logger controlled by the `DEBUG` env var.
|
|
5
16
|
*
|
|
6
|
-
*
|
|
17
|
+
* If `namespace` matches `DEBUG`, the logger writes to `process.stderr`;
|
|
18
|
+
* otherwise, it is a no-op. Additionally, `logger.enabled` is `true` when
|
|
19
|
+
* the namespace matches `DEBUG`, allowing you to conditionally run expensive
|
|
20
|
+
* diagnostics.
|
|
7
21
|
*
|
|
8
|
-
*
|
|
22
|
+
* `DEBUG` should be a comma- or whitespace-separated list of patterns.
|
|
23
|
+
* `*` is a wildcard and a leading `-` negates a pattern (e.g.
|
|
24
|
+
* `hardhat:*,-hardhat:noisy`).
|
|
9
25
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
26
|
+
* Messages are formatted using `node:util.format`, allowing you to use format
|
|
27
|
+
* specifiers like `%O`, `%o`, `%s`, `%d`, `%j`. Extra arguments without a
|
|
28
|
+
* matching specifier are inspected automatically.
|
|
29
|
+
*
|
|
30
|
+
* Output is colorized per namespace when `stderr` is a TTY. Set
|
|
31
|
+
* `DEBUG_COLORS=no` or `false` to disable colors.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const log = createDebug("hardhat:utils:foo");
|
|
36
|
+
* log("Starting up");
|
|
37
|
+
* log("Received %O", payload);
|
|
38
|
+
* log("Saved data", id, filePath);
|
|
39
|
+
*
|
|
40
|
+
* if (log.enabled) {
|
|
41
|
+
* // expensive diagnostics that should only run while debugging
|
|
14
42
|
* }
|
|
15
43
|
* ```
|
|
44
|
+
*
|
|
45
|
+
* ```sh
|
|
46
|
+
* DEBUG="hardhat:*,-hardhat:noisy" DEBUG_COLORS=no pnpm hardhat run script.js
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param namespace Namespace used for filtering and as the log prefix.
|
|
50
|
+
* @returns Logger function, or a shared no-op if disabled.
|
|
16
51
|
*/
|
|
17
|
-
export function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
export function createDebug(namespace: string): DebugLogger {
|
|
53
|
+
if (!isEnabled(namespace, process.env.DEBUG ?? "")) {
|
|
54
|
+
return NOOP;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const colors = useColors();
|
|
58
|
+
const color = colors ? selectColor(namespace) : undefined;
|
|
59
|
+
const prefix =
|
|
60
|
+
color !== undefined
|
|
61
|
+
? `\x1b[38;5;${color};1m ${namespace}\x1b[0m`
|
|
62
|
+
: ` ${namespace}`;
|
|
63
|
+
const suffixOpen = color !== undefined ? `\x1b[38;5;${color}m` : "";
|
|
64
|
+
const suffixClose = color !== undefined ? `\x1b[0m` : "";
|
|
65
|
+
let prev = 0;
|
|
66
|
+
|
|
67
|
+
const logger = (format: unknown, ...args: unknown[]): void => {
|
|
68
|
+
const now = Date.now();
|
|
69
|
+
const diff = prev === 0 ? 0 : now - prev;
|
|
70
|
+
prev = now;
|
|
71
|
+
|
|
72
|
+
const body =
|
|
73
|
+
args.length === 0 && typeof format === "string"
|
|
74
|
+
? format
|
|
75
|
+
: formatWithOptions({ colors }, format, ...args);
|
|
76
|
+
|
|
77
|
+
process.stderr.write(
|
|
78
|
+
`${prefix} ${body} ${suffixOpen}+${diff}ms${suffixClose}\n`,
|
|
79
|
+
);
|
|
37
80
|
};
|
|
81
|
+
|
|
82
|
+
return Object.assign(logger, { enabled: true });
|
|
38
83
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, fast subset of semver: strict `MAJOR.MINOR.PATCH` parsing and
|
|
3
|
+
* triple-wise comparison helpers.
|
|
4
|
+
*
|
|
5
|
+
* This module exists because the full `semver` package is slow to load and is
|
|
6
|
+
* overkill for the many call sites in Hardhat that compare against hard-coded
|
|
7
|
+
* `x.y.z` literals. For range grammar (caret/tilde, disjunctions, prerelease
|
|
8
|
+
* subset, etc.) keep using `semver`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type SemverVersion = [major: number, minor: number, patch: number];
|
|
12
|
+
|
|
13
|
+
const VERSION_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:\+[0-9A-Za-z.-]+)?$/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses a strict `MAJOR.MINOR.PATCH` version string into a `SemverVersion`
|
|
17
|
+
* tuple.
|
|
18
|
+
*
|
|
19
|
+
* An optional `+build` suffix is accepted and stripped silently. A
|
|
20
|
+
* `-prerelease` suffix is rejected.
|
|
21
|
+
*
|
|
22
|
+
* @param version The version string to parse.
|
|
23
|
+
* @returns The parsed `SemverVersion`, or `undefined` if the input does not
|
|
24
|
+
* match the strict `\d+\.\d+\.\d+` shape.
|
|
25
|
+
*/
|
|
26
|
+
export function parseVersion(version: string): SemverVersion | undefined {
|
|
27
|
+
const match = VERSION_REGEX.exec(version);
|
|
28
|
+
if (match === null) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* `Array#sort`-style comparator for `SemverVersion` tuples: returns a negative
|
|
37
|
+
* number, zero, or a positive number depending on whether `a` is lower than,
|
|
38
|
+
* equal to, or greater than `b`.
|
|
39
|
+
*/
|
|
40
|
+
export function compare(a: SemverVersion, b: SemverVersion): number {
|
|
41
|
+
if (a[0] !== b[0]) {
|
|
42
|
+
return a[0] - b[0];
|
|
43
|
+
}
|
|
44
|
+
if (a[1] !== b[1]) {
|
|
45
|
+
return a[1] - b[1];
|
|
46
|
+
}
|
|
47
|
+
return a[2] - b[2];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns `true` if `a` and `b` represent the same `MAJOR.MINOR.PATCH`.
|
|
52
|
+
*/
|
|
53
|
+
export function equals(a: SemverVersion, b: SemverVersion): boolean {
|
|
54
|
+
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns `true` if `compared` is strictly lower than `comparator`.
|
|
59
|
+
*/
|
|
60
|
+
export function lowerThan(
|
|
61
|
+
compared: SemverVersion,
|
|
62
|
+
comparator: SemverVersion,
|
|
63
|
+
): boolean {
|
|
64
|
+
return compare(compared, comparator) < 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns `true` if `compared` is lower than or equal to `comparator`.
|
|
69
|
+
*/
|
|
70
|
+
export function lowerThanOrEqual(
|
|
71
|
+
compared: SemverVersion,
|
|
72
|
+
comparator: SemverVersion,
|
|
73
|
+
): boolean {
|
|
74
|
+
return compare(compared, comparator) <= 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns `true` if `compared` is strictly greater than `comparator`.
|
|
79
|
+
*/
|
|
80
|
+
export function greaterThan(
|
|
81
|
+
compared: SemverVersion,
|
|
82
|
+
comparator: SemverVersion,
|
|
83
|
+
): boolean {
|
|
84
|
+
return compare(compared, comparator) > 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns `true` if `compared` is greater than or equal to `comparator`.
|
|
89
|
+
*/
|
|
90
|
+
export function greaterThanOrEqual(
|
|
91
|
+
compared: SemverVersion,
|
|
92
|
+
comparator: SemverVersion,
|
|
93
|
+
): boolean {
|
|
94
|
+
return compare(compared, comparator) >= 0;
|
|
95
|
+
}
|
package/src/fs.ts
CHANGED
|
@@ -25,6 +25,253 @@ import {
|
|
|
25
25
|
readdirWithFileTypesOrEmpty,
|
|
26
26
|
} from "./internal/fs.js";
|
|
27
27
|
|
|
28
|
+
const AMBIGUOUS_CASING_DIR_ENTRY = Symbol("ambiguous");
|
|
29
|
+
|
|
30
|
+
type CaseFoldedEntry = string | typeof AMBIGUOUS_CASING_DIR_ENTRY;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The entries in a directory, which stores their exact name, and also a
|
|
34
|
+
* case-folded mapping to support case-insensitive lookups.
|
|
35
|
+
*/
|
|
36
|
+
interface DirEntries {
|
|
37
|
+
/**
|
|
38
|
+
* The exact names present in the directory, as returned by `readdir`.
|
|
39
|
+
*/
|
|
40
|
+
readonly exactNames: Set<string>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Case-folded key -> the actual on-disk spelling, or
|
|
44
|
+
* AMBIGUOUS_CASING_DIR_ENTRY when multiple entries in the directory fold to
|
|
45
|
+
* the same key.
|
|
46
|
+
*/
|
|
47
|
+
readonly caseFoldedNames: Map<string, CaseFoldedEntry>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves paths to their true (on-disk) casing, caching directory listings
|
|
52
|
+
* and resolutions so repeated lookups against the same directories don't re-hit
|
|
53
|
+
* the filesystem.
|
|
54
|
+
*
|
|
55
|
+
* Intended to be used in hot paths where the same `from` directories are seen
|
|
56
|
+
* over and over.
|
|
57
|
+
*
|
|
58
|
+
* Does not resolve symbolic links.
|
|
59
|
+
*
|
|
60
|
+
* This class caches successful resolutions internally, and may do some
|
|
61
|
+
* duplicate work when multiple concurrent lookups for the same path are made
|
|
62
|
+
* before the first one finishes. It does not cache failed resolutions as
|
|
63
|
+
* negative result entries, but it does cache directory listings, so filesystem
|
|
64
|
+
* changes may not be observed until `clear()` is called. After `clear()`,
|
|
65
|
+
* previously cached directory and resolution data is discarded. If profiling
|
|
66
|
+
* shows that this work duplication is a problem, we can either cache in-flight
|
|
67
|
+
* operations, or add a mutex.
|
|
68
|
+
*/
|
|
69
|
+
export class TrueCasePathResolver {
|
|
70
|
+
/**
|
|
71
|
+
* A cache of DirEntries for the directories we've seen, keyed by their
|
|
72
|
+
* normalized absolute path as read. For example, if the same physical
|
|
73
|
+
* directory is read as `/a/foo` and `/a/Foo`, each path gets its own entry.
|
|
74
|
+
*/
|
|
75
|
+
readonly #dirCache = new Map<string, DirEntries>();
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A cache of successful resolutions, grouped by their `from` trusted starting
|
|
79
|
+
* directory.
|
|
80
|
+
*
|
|
81
|
+
* The outer key is the normalized absolute `from` path, and the inner key is
|
|
82
|
+
* the normalized `relativePath`.
|
|
83
|
+
*
|
|
84
|
+
* This keeps paths like `/a/B` + `foo.ts` distinct from `/a` + `B/foo.ts`,
|
|
85
|
+
* even if they point to the same location.
|
|
86
|
+
*/
|
|
87
|
+
readonly #resultCache = new Map<string, Map<string, string>>();
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Determines the true-case path of a given relative path from a specified
|
|
91
|
+
* directory, without resolving symbolic links.
|
|
92
|
+
*
|
|
93
|
+
* Note that the casing of the `from` path is not checked against the
|
|
94
|
+
* filesystem, and is trusted as-is. This avoids unnecessary directory
|
|
95
|
+
* listings for every ancestor of `from`, which can result in permission
|
|
96
|
+
* errors for directories that are otherwise accessible.
|
|
97
|
+
*
|
|
98
|
+
* @param from The absolute path of the directory to start the search from.
|
|
99
|
+
* @param relativePath The relative path to get the true case of.
|
|
100
|
+
* @returns The true case of the relative path. Returns an empty string if
|
|
101
|
+
* relativePath points to from.
|
|
102
|
+
* @throws FileNotFoundError if the starting directory or the relative path
|
|
103
|
+
* doesn't exist or is ambiguous.
|
|
104
|
+
* @throws NotADirectoryError if the starting directory, or an intermediate
|
|
105
|
+
* segment, is not a directory.
|
|
106
|
+
* @throws FileSystemAccessError for any other error.
|
|
107
|
+
*/
|
|
108
|
+
public async getFileTrueCase(
|
|
109
|
+
from: string,
|
|
110
|
+
relativePath: string,
|
|
111
|
+
): Promise<string> {
|
|
112
|
+
const absoluteFrom = path.resolve(from);
|
|
113
|
+
|
|
114
|
+
if (path.normalize(relativePath) === ".") {
|
|
115
|
+
// There's no casing to resolve, but we still read `from` so that callers
|
|
116
|
+
// get the documented FileNotFoundError / NotADirectoryError if it
|
|
117
|
+
// doesn't exist or isn't a directory.
|
|
118
|
+
await this.#getDirEntries(absoluteFrom);
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const resolved = await this.#resolveFrom(absoluteFrom, relativePath);
|
|
123
|
+
const resolvedRelativePath = path.relative(absoluteFrom, resolved);
|
|
124
|
+
|
|
125
|
+
if (
|
|
126
|
+
resolvedRelativePath === ".." ||
|
|
127
|
+
resolvedRelativePath.startsWith(`..${path.sep}`) ||
|
|
128
|
+
path.isAbsolute(resolvedRelativePath)
|
|
129
|
+
) {
|
|
130
|
+
throw new FileNotFoundError(path.resolve(absoluteFrom, relativePath));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return resolvedRelativePath;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Clears all cached directory listings and resolutions.
|
|
138
|
+
*/
|
|
139
|
+
public clear(): void {
|
|
140
|
+
this.#dirCache.clear();
|
|
141
|
+
this.#resultCache.clear();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async #resolveFrom(from: string, relativePath: string): Promise<string> {
|
|
145
|
+
const fromCacheKey = this.#getResultFromCacheKey(from);
|
|
146
|
+
const relativePathCacheKey =
|
|
147
|
+
this.#getResultRelativePathCacheKey(relativePath);
|
|
148
|
+
|
|
149
|
+
const cached = this.#resultCache
|
|
150
|
+
.get(fromCacheKey)
|
|
151
|
+
?.get(relativePathCacheKey);
|
|
152
|
+
|
|
153
|
+
if (cached !== undefined) {
|
|
154
|
+
return cached;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const resolved = await this.#doResolveFrom(from, relativePath);
|
|
158
|
+
|
|
159
|
+
let resultsFromCache = this.#resultCache.get(fromCacheKey);
|
|
160
|
+
if (resultsFromCache === undefined) {
|
|
161
|
+
resultsFromCache = new Map<string, string>();
|
|
162
|
+
this.#resultCache.set(fromCacheKey, resultsFromCache);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
resultsFromCache.set(relativePathCacheKey, resolved);
|
|
166
|
+
|
|
167
|
+
return resolved;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async #doResolveFrom(from: string, relativePath: string): Promise<string> {
|
|
171
|
+
let currentPath = from;
|
|
172
|
+
|
|
173
|
+
const segments = path
|
|
174
|
+
.normalize(relativePath)
|
|
175
|
+
.split(path.sep)
|
|
176
|
+
.filter((s) => s.length > 0 || s === ".");
|
|
177
|
+
|
|
178
|
+
for (const requestedName of segments) {
|
|
179
|
+
if (requestedName === "..") {
|
|
180
|
+
currentPath = path.join(currentPath, requestedName);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const entries = await this.#getDirEntries(currentPath);
|
|
185
|
+
const actualName = this.#lookupChild(entries, requestedName);
|
|
186
|
+
|
|
187
|
+
if (actualName === undefined) {
|
|
188
|
+
throw new FileNotFoundError(path.resolve(from, relativePath));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
currentPath = path.join(currentPath, actualName);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return currentPath;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async #getDirEntries(dirPath: string): Promise<DirEntries> {
|
|
198
|
+
const cacheKey = this.#getDirCacheKey(dirPath);
|
|
199
|
+
const cached = this.#dirCache.get(cacheKey);
|
|
200
|
+
if (cached !== undefined) {
|
|
201
|
+
return cached;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const entries = await this.#readDirEntries(dirPath);
|
|
205
|
+
this.#dirCache.set(cacheKey, entries);
|
|
206
|
+
|
|
207
|
+
return entries;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async #readDirEntries(dirPath: string): Promise<DirEntries> {
|
|
211
|
+
const names = await readdir(dirPath);
|
|
212
|
+
|
|
213
|
+
const exactNames = new Set<string>();
|
|
214
|
+
const caseFoldedNames = new Map<string, CaseFoldedEntry>();
|
|
215
|
+
|
|
216
|
+
for (const name of names) {
|
|
217
|
+
exactNames.add(name);
|
|
218
|
+
|
|
219
|
+
const folded = this.#caseFold(name);
|
|
220
|
+
const previous = caseFoldedNames.get(folded);
|
|
221
|
+
if (previous === undefined) {
|
|
222
|
+
caseFoldedNames.set(folded, name);
|
|
223
|
+
} else if (previous !== name) {
|
|
224
|
+
caseFoldedNames.set(folded, AMBIGUOUS_CASING_DIR_ENTRY);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { exactNames, caseFoldedNames };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#lookupChild(entries: DirEntries, requestedName: string): string | undefined {
|
|
232
|
+
if (entries.exactNames.has(requestedName)) {
|
|
233
|
+
return requestedName;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const candidate = entries.caseFoldedNames.get(
|
|
237
|
+
this.#caseFold(requestedName),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
if (candidate === undefined || candidate === AMBIGUOUS_CASING_DIR_ENTRY) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return candidate;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
#getResultFromCacheKey(from: string): string {
|
|
248
|
+
return path.normalize(from);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#getResultRelativePathCacheKey(relativePath: string): string {
|
|
252
|
+
return path.normalize(relativePath);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
#getDirCacheKey(dirPath: string): string {
|
|
256
|
+
return path.normalize(dirPath);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Returns a case-folded version of the given name, which can be thought of as
|
|
261
|
+
* a "normalized uppercase" form. This is used to implement case-insensitive
|
|
262
|
+
* comparisons.
|
|
263
|
+
*
|
|
264
|
+
* This is not an exact match with what every filesystem would do, but a good
|
|
265
|
+
* enough approximation for our purposes.
|
|
266
|
+
*
|
|
267
|
+
* @param name The name to fold.
|
|
268
|
+
* @returns The case-folded version of the name.
|
|
269
|
+
*/
|
|
270
|
+
#caseFold(name: string): string {
|
|
271
|
+
return name.normalize("NFC").toUpperCase();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
28
275
|
/**
|
|
29
276
|
* Determines the canonical pathname for a given path, resolving any symbolic
|
|
30
277
|
* links, and returns it.
|
|
@@ -141,34 +388,13 @@ export async function getAllDirectoriesMatching(
|
|
|
141
388
|
* @throws FileNotFoundError if the starting directory or the relative path doesn't exist.
|
|
142
389
|
* @throws NotADirectoryError if the starting directory is not a directory.
|
|
143
390
|
* @throws FileSystemAccessError for any other error.
|
|
391
|
+
* @deprecated Use {@link TrueCasePathResolver} instead.
|
|
144
392
|
*/
|
|
145
393
|
export async function getFileTrueCase(
|
|
146
394
|
from: string,
|
|
147
395
|
relativePath: string,
|
|
148
396
|
): Promise<string> {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const segments = relativePath.split(path.sep);
|
|
152
|
-
const nextDir = segments[0];
|
|
153
|
-
const nextDirLowerCase = nextDir.toLowerCase();
|
|
154
|
-
|
|
155
|
-
for (const dirEntry of dirEntries) {
|
|
156
|
-
if (dirEntry.toLowerCase() === nextDirLowerCase) {
|
|
157
|
-
if (segments.length === 1) {
|
|
158
|
-
return dirEntry;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return path.join(
|
|
162
|
-
dirEntry,
|
|
163
|
-
await getFileTrueCase(
|
|
164
|
-
path.join(from, dirEntry),
|
|
165
|
-
path.relative(nextDir, relativePath),
|
|
166
|
-
),
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
throw new FileNotFoundError(path.join(from, relativePath));
|
|
397
|
+
return await new TrueCasePathResolver().getFileTrueCase(from, relativePath);
|
|
172
398
|
}
|
|
173
399
|
|
|
174
400
|
/**
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { DebugLogger } from "../debug.js";
|
|
2
|
+
|
|
3
|
+
export const NOOP: DebugLogger = Object.assign(
|
|
4
|
+
(_format: unknown, ..._args: unknown[]): void => {},
|
|
5
|
+
{ enabled: false },
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
interface ParsedPatterns {
|
|
9
|
+
include: RegExp[];
|
|
10
|
+
exclude: RegExp[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let cached: { env: string; parsed: ParsedPatterns } | undefined;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses a `DEBUG`-style pattern string into include/exclude regex lists.
|
|
17
|
+
* Patterns prefixed with `-` go into `exclude`; everything else into `include`.
|
|
18
|
+
*
|
|
19
|
+
* Results are memoized against the last-seen `env` string, since
|
|
20
|
+
* `process.env.DEBUG` is effectively constant within a process and
|
|
21
|
+
* `createDebug` is called many times at startup.
|
|
22
|
+
*/
|
|
23
|
+
export function parsePatterns(env: string): ParsedPatterns {
|
|
24
|
+
if (cached !== undefined && cached.env === env) {
|
|
25
|
+
return cached.parsed;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const include: RegExp[] = [];
|
|
29
|
+
const exclude: RegExp[] = [];
|
|
30
|
+
|
|
31
|
+
for (const raw of env.split(/[\s,]+/)) {
|
|
32
|
+
if (raw === "") {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (raw.startsWith("-")) {
|
|
36
|
+
exclude.push(namespaceToRegExp(raw.slice(1)));
|
|
37
|
+
} else {
|
|
38
|
+
include.push(namespaceToRegExp(raw));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const parsed: ParsedPatterns = { include, exclude };
|
|
43
|
+
cached = { env, parsed };
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Converts a `DEBUG`-style namespace pattern into an anchored regex.
|
|
49
|
+
* Regex metacharacters are escaped to match literally, and `*` is translated
|
|
50
|
+
* into `.*?` so it behaves as a glob wildcard.
|
|
51
|
+
*/
|
|
52
|
+
function namespaceToRegExp(namespace: string): RegExp {
|
|
53
|
+
const escaped = namespace
|
|
54
|
+
.replace(/[.+?^${}()|[\]\\]/g, "\\$&")
|
|
55
|
+
.replace(/\*/g, ".*?");
|
|
56
|
+
|
|
57
|
+
return new RegExp(`^${escaped}$`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Checks whether a namespace is enabled under the given `DEBUG` pattern string.
|
|
62
|
+
*/
|
|
63
|
+
export function isEnabled(namespace: string, env: string): boolean {
|
|
64
|
+
if (env === "") {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { include, exclude } = parsePatterns(env);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
!exclude.some((p) => p.test(namespace)) &&
|
|
72
|
+
include.some((p) => p.test(namespace))
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// `debug@4`'s 76-colour 256-palette with the red and red-orange families
|
|
77
|
+
// stripped, so red stays reserved for error output.
|
|
78
|
+
export const COLORS: readonly number[] = [
|
|
79
|
+
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
|
|
80
|
+
69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
|
|
81
|
+
135, 148, 149, 178, 179, 184, 185, 201, 214, 215, 220, 221,
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Picks an ANSI 256-colour code for a namespace, deterministic from its
|
|
86
|
+
* characters.
|
|
87
|
+
*
|
|
88
|
+
* Uses a 32-bit FNV-1a hash followed by an xorshift finalizer. The finalizer
|
|
89
|
+
* improves bit avalanche so the low bits used by `% COLORS.length`
|
|
90
|
+
* distribute Hardhat's namespaces more evenly across the palette.
|
|
91
|
+
*/
|
|
92
|
+
export function selectColor(namespace: string): number {
|
|
93
|
+
let hash = 0x811c9dc5;
|
|
94
|
+
|
|
95
|
+
for (let i = 0; i < namespace.length; i++) {
|
|
96
|
+
// eslint-disable-next-line no-bitwise -- FNV-1a hash
|
|
97
|
+
hash ^= namespace.charCodeAt(i);
|
|
98
|
+
hash = Math.imul(hash, 0x01000193);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// xorshift finalizer for better avalanche on the low bits
|
|
102
|
+
/* eslint-disable no-bitwise -- xorshift finalizer */
|
|
103
|
+
hash ^= hash >>> 16;
|
|
104
|
+
hash = Math.imul(hash, 0x85ebca6b);
|
|
105
|
+
hash ^= hash >>> 13;
|
|
106
|
+
/* eslint-enable no-bitwise */
|
|
107
|
+
|
|
108
|
+
return COLORS[Math.abs(hash) % COLORS.length];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Reports whether ANSI colours should be used, honouring `DEBUG_COLORS` and TTY.
|
|
113
|
+
*/
|
|
114
|
+
export function useColors(): boolean {
|
|
115
|
+
const isDisabled =
|
|
116
|
+
process.env.DEBUG_COLORS === "no" || process.env.DEBUG_COLORS === "false";
|
|
117
|
+
|
|
118
|
+
return !isDisabled && process.stderr.isTTY === true;
|
|
119
|
+
}
|
package/src/subprocess.ts
CHANGED
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
} from "./errors/subprocess.js";
|
|
7
7
|
import { exists, isDirectory } from "./fs.js";
|
|
8
8
|
|
|
9
|
+
export { SubprocessFileNotFoundError, SubprocessPathIsDirectoryError };
|
|
10
|
+
|
|
9
11
|
/**
|
|
10
12
|
* Spawns a detached subprocess to execute a given file with optional arguments.
|
|
11
13
|
*
|