@gtkx/utils 0.21.0 → 1.0.0-rc.2
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 +173 -0
- package/dist/array/index-before-or-end.d.ts +15 -0
- package/dist/array/index-before-or-end.d.ts.map +1 -0
- package/dist/array/index-before-or-end.js +21 -0
- package/dist/array/index-before-or-end.js.map +1 -0
- package/dist/array/index.d.ts +6 -0
- package/dist/array/index.d.ts.map +1 -0
- package/dist/array/index.js +6 -0
- package/dist/array/index.js.map +1 -0
- package/dist/array/remove.d.ts +14 -0
- package/dist/array/remove.d.ts.map +1 -0
- package/dist/array/remove.js +19 -0
- package/dist/array/remove.js.map +1 -0
- package/dist/array/sort-strings-by.d.ts +15 -0
- package/dist/array/sort-strings-by.d.ts.map +1 -0
- package/dist/array/sort-strings-by.js +17 -0
- package/dist/array/sort-strings-by.js.map +1 -0
- package/dist/array/sort-strings.d.ts +12 -0
- package/dist/array/sort-strings.d.ts.map +1 -0
- package/dist/array/sort-strings.js +14 -0
- package/dist/array/sort-strings.js.map +1 -0
- package/dist/array/uniq-by.d.ts +18 -0
- package/dist/array/uniq-by.d.ts.map +1 -0
- package/dist/array/uniq-by.js +29 -0
- package/dist/array/uniq-by.js.map +1 -0
- package/dist/class/any-class.d.ts +8 -0
- package/dist/class/any-class.d.ts.map +1 -0
- package/dist/class/any-class.js +2 -0
- package/dist/class/any-class.js.map +1 -0
- package/dist/class/get-parent-class.d.ts +10 -0
- package/dist/class/get-parent-class.d.ts.map +1 -0
- package/dist/class/get-parent-class.js +12 -0
- package/dist/class/get-parent-class.js.map +1 -0
- package/dist/class/index.d.ts +4 -0
- package/dist/class/index.d.ts.map +1 -0
- package/dist/class/index.js +3 -0
- package/dist/class/index.js.map +1 -0
- package/dist/class/walk-class-chain.d.ts +13 -0
- package/dist/class/walk-class-chain.d.ts.map +1 -0
- package/dist/class/walk-class-chain.js +23 -0
- package/dist/class/walk-class-chain.js.map +1 -0
- package/dist/error/error-like.d.ts +6 -0
- package/dist/error/error-like.d.ts.map +1 -0
- package/dist/error/error-like.js +15 -0
- package/dist/error/error-like.js.map +1 -0
- package/dist/error/error-message.d.ts +13 -0
- package/dist/error/error-message.d.ts.map +1 -0
- package/dist/error/error-message.js +16 -0
- package/dist/error/error-message.js.map +1 -0
- package/dist/error/format-child-process-error.d.ts +12 -0
- package/dist/error/format-child-process-error.d.ts.map +1 -0
- package/dist/error/format-child-process-error.js +21 -0
- package/dist/error/format-child-process-error.js.map +1 -0
- package/dist/error/index.d.ts +4 -0
- package/dist/error/index.d.ts.map +1 -0
- package/dist/error/index.js +4 -0
- package/dist/error/index.js.map +1 -0
- package/dist/error/normalize-error.d.ts +13 -0
- package/dist/error/normalize-error.d.ts.map +1 -0
- package/dist/error/normalize-error.js +20 -0
- package/dist/error/normalize-error.js.map +1 -0
- package/dist/function/call-method.d.ts +3 -0
- package/dist/function/call-method.d.ts.map +1 -0
- package/dist/function/call-method.js +19 -0
- package/dist/function/call-method.js.map +1 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.d.ts.map +1 -0
- package/dist/function/index.js +2 -0
- package/dist/function/index.js.map +1 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -5
- package/dist/index.js.map +1 -1
- package/dist/log/create-logger.d.ts +15 -0
- package/dist/log/create-logger.d.ts.map +1 -0
- package/dist/log/create-logger.js +17 -0
- package/dist/log/create-logger.js.map +1 -0
- package/dist/log/default-logger.d.ts +36 -0
- package/dist/log/default-logger.d.ts.map +1 -0
- package/dist/log/default-logger.js +44 -0
- package/dist/log/default-logger.js.map +1 -0
- package/dist/log/index.d.ts +4 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/index.js +4 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +64 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/logger.js +100 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/map/get-or-insert.d.ts +23 -0
- package/dist/map/get-or-insert.d.ts.map +1 -0
- package/dist/map/get-or-insert.js +25 -0
- package/dist/map/get-or-insert.js.map +1 -0
- package/dist/map/index.d.ts +2 -0
- package/dist/map/index.d.ts.map +1 -0
- package/dist/map/index.js +2 -0
- package/dist/map/index.js.map +1 -0
- package/dist/object/index.d.ts +4 -0
- package/dist/object/index.d.ts.map +1 -0
- package/dist/object/index.js +4 -0
- package/dist/object/index.js.map +1 -0
- package/dist/object/omit.d.ts +15 -0
- package/dist/object/omit.d.ts.map +1 -0
- package/dist/object/omit.js +24 -0
- package/dist/object/omit.js.map +1 -0
- package/dist/object/pick-by.d.ts +15 -0
- package/dist/object/pick-by.d.ts.map +1 -0
- package/dist/object/pick-by.js +24 -0
- package/dist/object/pick-by.js.map +1 -0
- package/dist/object/structured-clone.d.ts +19 -0
- package/dist/object/structured-clone.d.ts.map +1 -0
- package/dist/object/structured-clone.js +28 -0
- package/dist/object/structured-clone.js.map +1 -0
- package/dist/predicate/are-object-keys-equal.d.ts +3 -0
- package/dist/predicate/are-object-keys-equal.d.ts.map +1 -0
- package/dist/predicate/are-object-keys-equal.js +9 -0
- package/dist/predicate/are-object-keys-equal.js.map +1 -0
- package/dist/predicate/index.d.ts +7 -0
- package/dist/predicate/index.d.ts.map +1 -0
- package/dist/predicate/index.js +7 -0
- package/dist/predicate/index.js.map +1 -0
- package/dist/predicate/is-deep-equal.d.ts +3 -0
- package/dist/predicate/is-deep-equal.d.ts.map +1 -0
- package/dist/predicate/is-deep-equal.js +35 -0
- package/dist/predicate/is-deep-equal.js.map +1 -0
- package/dist/predicate/is-plain-object.d.ts +17 -0
- package/dist/predicate/is-plain-object.d.ts.map +1 -0
- package/dist/predicate/is-plain-object.js +23 -0
- package/dist/predicate/is-plain-object.js.map +1 -0
- package/dist/predicate/is-record.d.ts +14 -0
- package/dist/predicate/is-record.d.ts.map +1 -0
- package/dist/predicate/is-record.js +16 -0
- package/dist/predicate/is-record.js.map +1 -0
- package/dist/predicate/is-same-array-by.d.ts +15 -0
- package/dist/predicate/is-same-array-by.d.ts.map +1 -0
- package/dist/predicate/is-same-array-by.js +20 -0
- package/dist/predicate/is-same-array-by.js.map +1 -0
- package/dist/predicate/is-same-array.d.ts +15 -0
- package/dist/predicate/is-same-array.d.ts.map +1 -0
- package/dist/predicate/is-same-array.js +18 -0
- package/dist/predicate/is-same-array.js.map +1 -0
- package/dist/predicate/is-shallow-equal.d.ts +3 -0
- package/dist/predicate/is-shallow-equal.d.ts.map +1 -0
- package/dist/predicate/is-shallow-equal.js +28 -0
- package/dist/predicate/is-shallow-equal.js.map +1 -0
- package/dist/process/exit-code-for-signal.d.ts +14 -0
- package/dist/process/exit-code-for-signal.d.ts.map +1 -0
- package/dist/process/exit-code-for-signal.js +19 -0
- package/dist/process/exit-code-for-signal.js.map +1 -0
- package/dist/process/index.d.ts +4 -0
- package/dist/process/index.d.ts.map +1 -0
- package/dist/process/index.js +4 -0
- package/dist/process/index.js.map +1 -0
- package/dist/process/install-graceful-shutdown.d.ts +16 -0
- package/dist/process/install-graceful-shutdown.d.ts.map +1 -0
- package/dist/process/install-graceful-shutdown.js +89 -0
- package/dist/process/install-graceful-shutdown.js.map +1 -0
- package/dist/process/resolve-executable.d.ts +14 -0
- package/dist/process/resolve-executable.d.ts.map +1 -0
- package/dist/process/resolve-executable.js +47 -0
- package/dist/process/resolve-executable.js.map +1 -0
- package/dist/set/drain.d.ts +11 -0
- package/dist/set/drain.d.ts.map +1 -0
- package/dist/set/drain.js +16 -0
- package/dist/set/drain.js.map +1 -0
- package/dist/set/index.d.ts +2 -0
- package/dist/set/index.d.ts.map +1 -0
- package/dist/set/index.js +2 -0
- package/dist/set/index.js.map +1 -0
- package/dist/source/index.d.ts +4 -0
- package/dist/source/index.d.ts.map +1 -0
- package/dist/source/index.js +4 -0
- package/dist/source/index.js.map +1 -0
- package/dist/source/sanitize-identifier.d.ts +14 -0
- package/dist/source/sanitize-identifier.d.ts.map +1 -0
- package/dist/source/sanitize-identifier.js +65 -0
- package/dist/source/sanitize-identifier.js.map +1 -0
- package/dist/source/source-string-literal.d.ts +13 -0
- package/dist/source/source-string-literal.d.ts.map +1 -0
- package/dist/source/source-string-literal.js +24 -0
- package/dist/source/source-string-literal.js.map +1 -0
- package/dist/source/to-camel-identifier.d.ts +13 -0
- package/dist/source/to-camel-identifier.d.ts.map +1 -0
- package/dist/source/to-camel-identifier.js +17 -0
- package/dist/source/to-camel-identifier.js.map +1 -0
- package/dist/string/camel-case.d.ts +17 -0
- package/dist/string/camel-case.d.ts.map +1 -0
- package/dist/string/camel-case.js +21 -0
- package/dist/string/camel-case.js.map +1 -0
- package/dist/string/index.d.ts +6 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +6 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/kebab-case.d.ts +14 -0
- package/dist/string/kebab-case.d.ts.map +1 -0
- package/dist/string/kebab-case.js +16 -0
- package/dist/string/kebab-case.js.map +1 -0
- package/dist/string/lower-first.d.ts +13 -0
- package/dist/string/lower-first.d.ts.map +1 -0
- package/dist/string/lower-first.js +15 -0
- package/dist/string/lower-first.js.map +1 -0
- package/dist/string/pascal-case.d.ts +16 -0
- package/dist/string/pascal-case.d.ts.map +1 -0
- package/dist/string/pascal-case.js +20 -0
- package/dist/string/pascal-case.js.map +1 -0
- package/dist/string/upper-first.d.ts +13 -0
- package/dist/string/upper-first.d.ts.map +1 -0
- package/dist/string/upper-first.js +15 -0
- package/dist/string/upper-first.js.map +1 -0
- package/dist/string/word-segments.d.ts +3 -0
- package/dist/string/word-segments.d.ts.map +1 -0
- package/dist/string/word-segments.js +12 -0
- package/dist/string/word-segments.js.map +1 -0
- package/dist/util/index.d.ts +2 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +2 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/package-version.d.ts +12 -0
- package/dist/util/package-version.d.ts.map +1 -0
- package/dist/util/package-version.js +15 -0
- package/dist/util/package-version.js.map +1 -0
- package/package.json +18 -7
- package/src/array/index-before-or-end.ts +27 -0
- package/src/array/index.ts +5 -0
- package/src/array/remove.ts +20 -0
- package/src/array/sort-strings-by.ts +17 -0
- package/src/array/sort-strings.ts +14 -0
- package/src/array/uniq-by.ts +33 -0
- package/src/class/any-class.ts +10 -0
- package/src/class/get-parent-class.ts +15 -0
- package/src/class/index.ts +3 -0
- package/src/class/walk-class-chain.ts +29 -0
- package/src/error/error-like.ts +19 -0
- package/src/error/error-message.ts +17 -0
- package/src/error/format-child-process-error.ts +24 -0
- package/src/error/index.ts +3 -0
- package/src/error/normalize-error.ts +22 -0
- package/src/function/call-method.ts +22 -0
- package/src/function/index.ts +1 -0
- package/src/index.ts +13 -10
- package/src/log/create-logger.ts +18 -0
- package/src/log/default-logger.ts +49 -0
- package/src/log/index.ts +3 -0
- package/src/log/logger.ts +142 -0
- package/src/map/get-or-insert.ts +33 -0
- package/src/map/index.ts +1 -0
- package/src/object/index.ts +3 -0
- package/src/object/omit.ts +26 -0
- package/src/object/pick-by.ts +30 -0
- package/src/object/structured-clone.ts +31 -0
- package/src/predicate/are-object-keys-equal.ts +15 -0
- package/src/predicate/index.ts +6 -0
- package/src/predicate/is-deep-equal.ts +41 -0
- package/src/predicate/is-plain-object.ts +25 -0
- package/src/predicate/is-record.ts +16 -0
- package/src/predicate/is-same-array-by.ts +21 -0
- package/src/predicate/is-same-array.ts +19 -0
- package/src/predicate/is-shallow-equal.ts +32 -0
- package/src/process/exit-code-for-signal.ts +20 -0
- package/src/process/index.ts +3 -0
- package/src/process/install-graceful-shutdown.ts +126 -0
- package/src/process/resolve-executable.ts +57 -0
- package/src/set/drain.ts +17 -0
- package/src/set/index.ts +1 -0
- package/src/source/index.ts +3 -0
- package/src/source/sanitize-identifier.ts +66 -0
- package/src/source/source-string-literal.ts +27 -0
- package/src/source/to-camel-identifier.ts +18 -0
- package/src/string/camel-case.ts +22 -0
- package/src/string/index.ts +5 -0
- package/src/string/kebab-case.ts +18 -0
- package/src/string/lower-first.ts +15 -0
- package/src/string/pascal-case.ts +21 -0
- package/src/string/upper-first.ts +15 -0
- package/src/string/word-segments.ts +15 -0
- package/src/util/index.ts +1 -0
- package/src/util/package-version.ts +16 -0
- package/dist/class.d.ts +0 -16
- package/dist/class.d.ts.map +0 -1
- package/dist/class.js +0 -2
- package/dist/class.js.map +0 -1
- package/dist/collection.d.ts +0 -58
- package/dist/collection.d.ts.map +0 -1
- package/dist/collection.js +0 -98
- package/dist/collection.js.map +0 -1
- package/dist/error.d.ts +0 -13
- package/dist/error.d.ts.map +0 -1
- package/dist/error.js +0 -13
- package/dist/error.js.map +0 -1
- package/dist/graceful-shutdown.d.ts +0 -70
- package/dist/graceful-shutdown.d.ts.map +0 -1
- package/dist/graceful-shutdown.js +0 -94
- package/dist/graceful-shutdown.js.map +0 -1
- package/dist/source.d.ts +0 -34
- package/dist/source.d.ts.map +0 -1
- package/dist/source.js +0 -99
- package/dist/source.js.map +0 -1
- package/dist/string.d.ts +0 -59
- package/dist/string.d.ts.map +0 -1
- package/dist/string.js +0 -73
- package/dist/string.js.map +0 -1
- package/src/class.ts +0 -17
- package/src/collection.ts +0 -103
- package/src/error.ts +0 -12
- package/src/graceful-shutdown.ts +0 -141
- package/src/source.ts +0 -103
- package/src/string.ts +0 -74
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import pc from "picocolors";
|
|
2
|
+
|
|
3
|
+
type Colors = ReturnType<typeof pc.createColors>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal writable-stream shape a {@link Logger} writes formatted lines to.
|
|
7
|
+
*/
|
|
8
|
+
type OutputStream = {
|
|
9
|
+
write(chunk: string): unknown;
|
|
10
|
+
/** Whether the stream is a terminal, used to decide if colored output is emitted. */
|
|
11
|
+
isTTY?: boolean | undefined;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Options for constructing a {@link Logger}.
|
|
16
|
+
*/
|
|
17
|
+
type LoggerOptions = {
|
|
18
|
+
/** Namespace appended to the log prefix and matched against debug configuration. */
|
|
19
|
+
namespace?: string | undefined;
|
|
20
|
+
/** Stream to write log lines to; defaults to `process.stderr`. */
|
|
21
|
+
stream?: OutputStream | undefined;
|
|
22
|
+
/** Forces debug output on or off; when omitted it is resolved from `--debug` and `GTKX_DEBUG`. */
|
|
23
|
+
debugEnabled?: boolean | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const BASE_PREFIX = "[gtkx]";
|
|
27
|
+
|
|
28
|
+
function getColors(stream: OutputStream): Colors {
|
|
29
|
+
return pc.createColors(pc.isColorSupported && stream.isTTY === true);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function formatValue(value: unknown): string {
|
|
33
|
+
if (typeof value === "string") {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (value instanceof Error) {
|
|
38
|
+
return value.stack ?? value.message;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
return JSON.stringify(value);
|
|
43
|
+
} catch {
|
|
44
|
+
return String(value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isDebugEnabled(namespace: string | undefined, argv: string[], env: NodeJS.ProcessEnv): boolean {
|
|
49
|
+
if (argv.includes("--debug")) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const spec = env.GTKX_DEBUG;
|
|
54
|
+
|
|
55
|
+
if (!spec) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const names = new Set(spec.split(/[\s,]+/).filter((name) => name.length > 0));
|
|
60
|
+
|
|
61
|
+
if (names.has("1") || names.has("*")) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return namespace !== undefined && names.has(namespace);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function getPrefix(namespace: string | undefined): string {
|
|
69
|
+
return namespace === undefined ? BASE_PREFIX : `[gtkx:${namespace}]`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Writes prefixed, optionally colored log lines to an output stream, with debug lines gated by
|
|
74
|
+
* command-line and environment configuration.
|
|
75
|
+
*/
|
|
76
|
+
class Logger {
|
|
77
|
+
private stream: OutputStream;
|
|
78
|
+
private prefix: string;
|
|
79
|
+
private debugEnabled: boolean;
|
|
80
|
+
private colors: Colors;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param options - Namespace, target stream, and debug configuration for the logger.
|
|
84
|
+
*/
|
|
85
|
+
constructor(options: LoggerOptions = {}) {
|
|
86
|
+
this.stream = options.stream ?? process.stderr;
|
|
87
|
+
this.prefix = getPrefix(options.namespace);
|
|
88
|
+
this.debugEnabled = options.debugEnabled ?? isDebugEnabled(options.namespace, process.argv, process.env);
|
|
89
|
+
this.colors = getColors(this.stream);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private write(message: string, rest: unknown[]): void {
|
|
93
|
+
const suffix = rest.length === 0 ? "" : ` ${rest.map((value) => formatValue(value)).join(" ")}`;
|
|
94
|
+
this.stream.write(`${this.prefix} ${message}${suffix}\n`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Writes an informational line.
|
|
99
|
+
*
|
|
100
|
+
* @param message - The message text.
|
|
101
|
+
* @param rest - Extra values appended after the message, formatted for display.
|
|
102
|
+
*/
|
|
103
|
+
info(message: string, ...rest: unknown[]): void {
|
|
104
|
+
this.write(message, rest);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Writes a line marked as a warning.
|
|
109
|
+
*
|
|
110
|
+
* @param message - The message text.
|
|
111
|
+
* @param rest - Extra values appended after the message, formatted for display.
|
|
112
|
+
*/
|
|
113
|
+
warn(message: string, ...rest: unknown[]): void {
|
|
114
|
+
this.write(`${this.colors.yellow("warn")} ${message}`, rest);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Writes a line marked as an error.
|
|
119
|
+
*
|
|
120
|
+
* @param message - The message text.
|
|
121
|
+
* @param rest - Extra values appended after the message, formatted for display.
|
|
122
|
+
*/
|
|
123
|
+
error(message: string, ...rest: unknown[]): void {
|
|
124
|
+
this.write(`${this.colors.red("error")} ${message}`, rest);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Writes a line only when debug output is enabled for this logger.
|
|
129
|
+
*
|
|
130
|
+
* @param message - The message text.
|
|
131
|
+
* @param rest - Extra values appended after the message, formatted for display.
|
|
132
|
+
*/
|
|
133
|
+
debug(message: string, ...rest: unknown[]): void {
|
|
134
|
+
if (!this.debugEnabled) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.write(message, rest);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { Logger, type OutputStream, type LoggerOptions };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
type GetSet<K, V> = {
|
|
2
|
+
get(key: K): V | undefined;
|
|
3
|
+
set(key: K, value: V): unknown;
|
|
4
|
+
has(key: K): boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns the value stored under `key`, or inserts and returns the value produced by `factory` when
|
|
9
|
+
* the key is absent. Works with both `Map` and `WeakMap`, and caches a computed `undefined` value.
|
|
10
|
+
*
|
|
11
|
+
* @template K - The key type.
|
|
12
|
+
* @template V - The value type.
|
|
13
|
+
* @param map - The `Map` or `WeakMap` to read from and populate.
|
|
14
|
+
* @param key - The key to look up.
|
|
15
|
+
* @param factory - Produces the value to insert when `key` is absent; called at most once.
|
|
16
|
+
* @returns The existing or newly-inserted value.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const counts = new Map<string, number[]>();
|
|
20
|
+
* getOrInsert(counts, "a", () => []).push(1); // counts is Map { "a" => [1] }
|
|
21
|
+
*/
|
|
22
|
+
function getOrInsert<K, V>(map: GetSet<K, V>, key: K, factory: (key: K) => V): V {
|
|
23
|
+
if (map.has(key)) {
|
|
24
|
+
return map.get(key) as V;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const value = factory(key);
|
|
28
|
+
map.set(key, value);
|
|
29
|
+
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { getOrInsert, type GetSet };
|
package/src/map/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getOrInsert } from "./get-or-insert.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object holding every entry of `obj` except the named keys.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the source object.
|
|
5
|
+
* @template K - The keys to leave out.
|
|
6
|
+
* @param obj - The object to copy entries from.
|
|
7
|
+
* @param keys - The keys to leave out of the result.
|
|
8
|
+
* @returns A new object without the named keys.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* omit({ a: 1, b: 2 }, ["a"]); // { b: 2 }
|
|
12
|
+
*/
|
|
13
|
+
function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
|
|
14
|
+
const excluded: Set<PropertyKey> = new Set(keys);
|
|
15
|
+
const result: Record<string, unknown> = {};
|
|
16
|
+
|
|
17
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
18
|
+
if (!excluded.has(key)) {
|
|
19
|
+
result[key] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return result as Omit<T, K>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { omit };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object of the entries of `obj` that satisfy the predicate.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the source object.
|
|
5
|
+
* @param obj - The object to pick entries from.
|
|
6
|
+
* @param shouldPick - Called with each value and key; return `true` to keep the entry.
|
|
7
|
+
* @returns A new object holding only the entries the predicate kept.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* pickBy({ a: 1, b: 2 }, (value) => value > 1); // { b: 2 }
|
|
11
|
+
* pickBy({ a: 1, b: 2 }, (_value, key) => key !== "a"); // { b: 2 }
|
|
12
|
+
*/
|
|
13
|
+
function pickBy<T extends Record<string, unknown>>(
|
|
14
|
+
obj: T,
|
|
15
|
+
shouldPick: (value: T[keyof T], key: keyof T) => boolean,
|
|
16
|
+
): Partial<T> {
|
|
17
|
+
const result: Partial<T> = {};
|
|
18
|
+
|
|
19
|
+
for (const key of Object.keys(obj) as (keyof T)[]) {
|
|
20
|
+
const value = obj[key];
|
|
21
|
+
|
|
22
|
+
if (shouldPick(value, key)) {
|
|
23
|
+
result[key] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { pickBy };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isPlainObject } from "../predicate/is-plain-object.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Recursively copies arrays and plain objects, sharing every other value by reference. The result
|
|
5
|
+
* mirrors exactly the structure `isDeepEqual` traverses, so comparing a value against its earlier
|
|
6
|
+
* clone detects in-place mutations of the original. Unlike the global of the same name, class
|
|
7
|
+
* instances such as `Date` and `Map` are shared rather than copied, and no value is ever rejected.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of the value.
|
|
10
|
+
* @param value - The value to clone.
|
|
11
|
+
* @returns A clone whose arrays and plain objects are fresh and whose other values are shared.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const source = { a: [{ b: 1 }] };
|
|
15
|
+
* const copy = structuredClone(source);
|
|
16
|
+
* source.a[0].b = 2;
|
|
17
|
+
* copy.a[0].b; // 1
|
|
18
|
+
*/
|
|
19
|
+
function structuredClone<T>(value: T): T {
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
return value.map((item: unknown) => structuredClone(item)) as T;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (isPlainObject(value)) {
|
|
25
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, structuredClone(entry)])) as T;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { structuredClone };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const areObjectKeysEqual = (
|
|
2
|
+
a: Record<string, unknown>,
|
|
3
|
+
b: Record<string, unknown>,
|
|
4
|
+
isValueEqual: (a: unknown, b: unknown) => boolean,
|
|
5
|
+
): boolean => {
|
|
6
|
+
const keysA = Object.keys(a);
|
|
7
|
+
|
|
8
|
+
if (keysA.length !== Object.keys(b).length) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return keysA.every((key) => Object.hasOwn(b, key) && isValueEqual(a[key], b[key]));
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { areObjectKeysEqual };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { isDeepEqual } from "./is-deep-equal.js";
|
|
2
|
+
export { isPlainObject } from "./is-plain-object.js";
|
|
3
|
+
export { isRecord } from "./is-record.js";
|
|
4
|
+
export { isSameArrayBy } from "./is-same-array-by.js";
|
|
5
|
+
export { isSameArray } from "./is-same-array.js";
|
|
6
|
+
export { isShallowEqual } from "./is-shallow-equal.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { areObjectKeysEqual } from "./are-object-keys-equal.js";
|
|
2
|
+
import { isPlainObject } from "./is-plain-object.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks whether two values are deeply equal: identical, arrays of the same length whose elements
|
|
6
|
+
* are deeply equal in order, or plain objects with the same own enumerable keys and deeply equal
|
|
7
|
+
* values. Values that are neither both arrays nor both plain objects are equal only when identical.
|
|
8
|
+
*
|
|
9
|
+
* @param a - The first value.
|
|
10
|
+
* @param b - The second value.
|
|
11
|
+
* @returns `true` when the values are deeply equal.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* isDeepEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }); // true
|
|
15
|
+
* isDeepEqual({ a: [1] }, { a: [2] }); // false
|
|
16
|
+
*/
|
|
17
|
+
const isDeepArrayEqual = (a: unknown, b: unknown): boolean => {
|
|
18
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return a.every((item, index) => isDeepEqual(item, b[index]));
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function isDeepEqual(a: unknown, b: unknown): boolean {
|
|
26
|
+
if (a === b) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
31
|
+
return isDeepArrayEqual(a, b);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
35
|
+
return areObjectKeysEqual(a, b, isDeepEqual);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { isDeepEqual };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether a value is a plain object, i.e. one whose prototype is `Object.prototype` or `null`.
|
|
3
|
+
*
|
|
4
|
+
* Class instances, arrays, and other exotic objects return `false`.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The value to test.
|
|
7
|
+
* @returns `true` when `value` is a plain object.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* isPlainObject({ a: 1 }); // true
|
|
11
|
+
* isPlainObject(Object.create(null)); // true
|
|
12
|
+
* isPlainObject([1, 2]); // false
|
|
13
|
+
* isPlainObject(new Date()); // false
|
|
14
|
+
*/
|
|
15
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
16
|
+
if (typeof value !== "object" || value === null) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const proto: unknown = Object.getPrototypeOf(value);
|
|
21
|
+
|
|
22
|
+
return proto === Object.prototype || proto === null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { isPlainObject };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether a value is a non-null object (including arrays and class instances, excluding `null`).
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to test.
|
|
5
|
+
* @returns `true` when `value` is a non-null object.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* isRecord({ a: 1 }); // true
|
|
9
|
+
* isRecord(null); // false
|
|
10
|
+
* isRecord("x"); // false
|
|
11
|
+
*/
|
|
12
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
13
|
+
return typeof value === "object" && value !== null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { isRecord };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether two arrays have the same length and elements the comparator treats as equal, in order.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the array elements.
|
|
5
|
+
* @param a - The first array.
|
|
6
|
+
* @param b - The second array.
|
|
7
|
+
* @param isEqual - Compares elements at the same index; return `true` when they are equal.
|
|
8
|
+
* @returns `true` when the arrays are element-wise equal under `isEqual`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* isSameArrayBy([{ id: 1 }], [{ id: 1 }], (x, y) => x.id === y.id); // true
|
|
12
|
+
*/
|
|
13
|
+
function isSameArrayBy<T>(a: T[], b: T[], isEqual: (x: T, y: T) => boolean): boolean {
|
|
14
|
+
if (a.length !== b.length) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return a.every((item, index) => isEqual(item, b[index] as T));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { isSameArrayBy };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isSameArrayBy } from "./is-same-array-by.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether two arrays have the same length and strictly equal (`===`) elements in order.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of the array elements.
|
|
7
|
+
* @param a - The first array.
|
|
8
|
+
* @param b - The second array.
|
|
9
|
+
* @returns `true` when the arrays are element-wise strictly equal.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* isSameArray([1, 2], [1, 2]); // true
|
|
13
|
+
* isSameArray([1, 2], [2, 1]); // false
|
|
14
|
+
*/
|
|
15
|
+
function isSameArray<T>(a: T[], b: T[]): boolean {
|
|
16
|
+
return isSameArrayBy(a, b, (x, y) => x === y);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { isSameArray };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { areObjectKeysEqual } from "./are-object-keys-equal.js";
|
|
2
|
+
import { isPlainObject } from "./is-plain-object.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks whether two values are shallowly equal: identical, or plain objects with the same own
|
|
6
|
+
* enumerable keys and strictly equal (`===`) values. Values that are not both plain objects are
|
|
7
|
+
* equal only when identical.
|
|
8
|
+
*
|
|
9
|
+
* @param a - The first value.
|
|
10
|
+
* @param b - The second value.
|
|
11
|
+
* @returns `true` when the values are shallowly equal.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* isShallowEqual({ a: 1 }, { a: 1 }); // true
|
|
15
|
+
* isShallowEqual({ a: 1 }, { a: 2 }); // false
|
|
16
|
+
* isShallowEqual(3, 3); // true
|
|
17
|
+
*/
|
|
18
|
+
const isStrictEqual = (a: unknown, b: unknown): boolean => a === b;
|
|
19
|
+
|
|
20
|
+
function isShallowEqual(a: unknown, b: unknown): boolean {
|
|
21
|
+
if (a === b) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
26
|
+
return areObjectKeysEqual(a, b, isStrictEqual);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { isShallowEqual };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps a terminating signal to its conventional process exit code (130 for `SIGINT`, 143 otherwise),
|
|
3
|
+
* or 0 when no signal is given.
|
|
4
|
+
*
|
|
5
|
+
* @param signal - The signal that triggered termination, or `null`.
|
|
6
|
+
* @returns The conventional exit code for the signal.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* exitCodeForSignal("SIGINT"); // 130
|
|
10
|
+
* exitCodeForSignal(null); // 0
|
|
11
|
+
*/
|
|
12
|
+
function exitCodeForSignal(signal: NodeJS.Signals | null): number {
|
|
13
|
+
if (!signal) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return signal === "SIGINT" ? 130 : 143;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { exitCodeForSignal };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { error } from "../log/default-logger.js";
|
|
2
|
+
import { exitCodeForSignal } from "./exit-code-for-signal.js";
|
|
3
|
+
|
|
4
|
+
type GracefulShutdownOptions = {
|
|
5
|
+
onSignal: (signal: NodeJS.Signals) => void | Promise<void>;
|
|
6
|
+
onForce?: () => void;
|
|
7
|
+
forceKillAfterMs?: number;
|
|
8
|
+
coalesceWindowMs?: number;
|
|
9
|
+
exitCode?: (signal: NodeJS.Signals, isGraceful: boolean) => number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type ShutdownState = {
|
|
13
|
+
options: GracefulShutdownOptions;
|
|
14
|
+
forceKillMs: number;
|
|
15
|
+
coalesceWindowMs: number;
|
|
16
|
+
firstSignal: NodeJS.Signals | null;
|
|
17
|
+
exited: boolean;
|
|
18
|
+
coalescing: boolean;
|
|
19
|
+
forceTimer: NodeJS.Timeout | null;
|
|
20
|
+
coalesceTimer: NodeJS.Timeout | null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const HANDLED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"] as const satisfies NodeJS.Signals[];
|
|
24
|
+
const DEFAULT_FORCE_KILL_TIMEOUT_MS = 5000;
|
|
25
|
+
const DEFAULT_COALESCE_WINDOW_MS = 500;
|
|
26
|
+
|
|
27
|
+
const clearTimers = (state: ShutdownState): void => {
|
|
28
|
+
if (state.forceTimer) {
|
|
29
|
+
clearTimeout(state.forceTimer);
|
|
30
|
+
state.forceTimer = null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (state.coalesceTimer) {
|
|
34
|
+
clearTimeout(state.coalesceTimer);
|
|
35
|
+
state.coalesceTimer = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const finish = (state: ShutdownState, signal: NodeJS.Signals, isGraceful: boolean): void => {
|
|
40
|
+
if (state.exited) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
state.exited = true;
|
|
45
|
+
clearTimers(state);
|
|
46
|
+
const { exitCode } = state.options;
|
|
47
|
+
const defaultCode = isGraceful ? 0 : exitCodeForSignal(signal);
|
|
48
|
+
process.exit(exitCode ? exitCode(signal, isGraceful) : defaultCode);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const runShutdown = async (state: ShutdownState, signal: NodeJS.Signals): Promise<void> => {
|
|
52
|
+
try {
|
|
53
|
+
await state.options.onSignal(signal);
|
|
54
|
+
finish(state, signal, true);
|
|
55
|
+
} catch (error_) {
|
|
56
|
+
error("graceful shutdown failed", error_);
|
|
57
|
+
finish(state, signal, false);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const beginShutdown = (state: ShutdownState, signal: NodeJS.Signals): void => {
|
|
62
|
+
state.firstSignal = signal;
|
|
63
|
+
|
|
64
|
+
if (state.coalesceWindowMs > 0) {
|
|
65
|
+
state.coalescing = true;
|
|
66
|
+
|
|
67
|
+
state.coalesceTimer = setTimeout(() => {
|
|
68
|
+
state.coalescing = false;
|
|
69
|
+
}, state.coalesceWindowMs);
|
|
70
|
+
|
|
71
|
+
state.coalesceTimer.unref();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (state.options.onForce && state.forceKillMs > 0) {
|
|
75
|
+
state.forceTimer = setTimeout(() => {
|
|
76
|
+
state.options.onForce?.();
|
|
77
|
+
finish(state, signal, false);
|
|
78
|
+
}, state.forceKillMs);
|
|
79
|
+
|
|
80
|
+
state.forceTimer.unref();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void runShutdown(state, signal);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const handle = (state: ShutdownState, signal: NodeJS.Signals): void => {
|
|
87
|
+
if (state.firstSignal === null) {
|
|
88
|
+
beginShutdown(state, signal);
|
|
89
|
+
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (state.coalescing) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
state.options.onForce?.();
|
|
98
|
+
finish(state, signal, false);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Registers handlers for `SIGINT`, `SIGTERM`, and `SIGHUP` that run the given cleanup callback once
|
|
103
|
+
* and then exit, forcing exit if a repeated signal arrives or the cleanup exceeds its timeout.
|
|
104
|
+
*
|
|
105
|
+
* @param options - The shutdown callbacks and timing configuration.
|
|
106
|
+
*/
|
|
107
|
+
function installGracefulShutdown(options: GracefulShutdownOptions): void {
|
|
108
|
+
const state: ShutdownState = {
|
|
109
|
+
options,
|
|
110
|
+
forceKillMs: options.forceKillAfterMs ?? DEFAULT_FORCE_KILL_TIMEOUT_MS,
|
|
111
|
+
coalesceWindowMs: options.coalesceWindowMs ?? DEFAULT_COALESCE_WINDOW_MS,
|
|
112
|
+
firstSignal: null,
|
|
113
|
+
exited: false,
|
|
114
|
+
coalescing: false,
|
|
115
|
+
forceTimer: null,
|
|
116
|
+
coalesceTimer: null,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
for (const sig of HANDLED_SIGNALS) {
|
|
120
|
+
process.on(sig, () => {
|
|
121
|
+
handle(state, sig);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export { installGracefulShutdown, type GracefulShutdownOptions };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { accessSync, constants } from "node:fs";
|
|
2
|
+
import { delimiter, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
const isExecutable = (path: string): boolean => {
|
|
5
|
+
try {
|
|
6
|
+
accessSync(path, constants.X_OK);
|
|
7
|
+
|
|
8
|
+
return true;
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const findOnPath = (command: string): string | undefined => {
|
|
15
|
+
const searchPaths = (process.env.PATH ?? "").split(delimiter).filter((entry) => entry.length > 0);
|
|
16
|
+
|
|
17
|
+
for (const directory of searchPaths) {
|
|
18
|
+
const candidate = join(directory, command);
|
|
19
|
+
|
|
20
|
+
if (isExecutable(candidate)) {
|
|
21
|
+
return candidate;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resolves an executable to an absolute path, searching `PATH` only for a bare command name, so
|
|
30
|
+
* spawning it never re-runs a lookup against a `PATH` that may have changed.
|
|
31
|
+
*
|
|
32
|
+
* @param command - An executable name, or a path to one.
|
|
33
|
+
* @returns The absolute path of the executable.
|
|
34
|
+
* @throws When a bare command name is not found on `PATH`.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* resolveExecutable("weston"); // "/usr/bin/weston"
|
|
38
|
+
*/
|
|
39
|
+
function resolveExecutable(command: string): string {
|
|
40
|
+
if (isAbsolute(command)) {
|
|
41
|
+
return command;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (command.includes("/")) {
|
|
45
|
+
return resolve(command);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const found = findOnPath(command);
|
|
49
|
+
|
|
50
|
+
if (found === undefined) {
|
|
51
|
+
throw new Error(`Cannot find the "${command}" executable on PATH`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return found;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { resolveExecutable };
|
package/src/set/drain.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Iterates every item currently in `set`, then clears it. Use it to drain a set of pending work
|
|
3
|
+
* accumulated since the last drain.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The set item type.
|
|
6
|
+
* @param set - The set to drain and clear.
|
|
7
|
+
* @param visit - Called with each item before the set is cleared.
|
|
8
|
+
*/
|
|
9
|
+
const drain = <T>(set: Set<T>, visit: (item: T) => void): void => {
|
|
10
|
+
for (const item of set) {
|
|
11
|
+
visit(item);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
set.clear();
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { drain };
|