@ls-stack/utils 1.13.2 → 2.0.1
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/dist/arrayUtils.cjs +91 -0
- package/dist/arrayUtils.d.cts +51 -0
- package/dist/arrayUtils.js +1 -2
- package/dist/assertions.cjs +99 -0
- package/dist/assertions.d.cts +17 -0
- package/dist/assertions.js +1 -2
- package/dist/castValues.cjs +41 -0
- package/dist/castValues.d.cts +4 -0
- package/dist/castValues.js +0 -1
- package/dist/chunk-3PXKYWJF.js +148 -0
- package/dist/{chunk-FYMTZQ42.js → chunk-4UGSP3L3.js} +0 -1
- package/dist/{chunk-HLFWWIDK.js → chunk-5DZT3Z5Z.js} +0 -1
- package/dist/{chunk-W7X6NSGR.js → chunk-AZBBTE33.js} +0 -1
- package/dist/{chunk-MMKMF4GG.js → chunk-IATIXMCE.js} +0 -1
- package/dist/{chunk-M74KKYE4.js → chunk-KCOXGSRA.js} +0 -1
- package/dist/{chunk-NKCC3375.js → chunk-NWXBMMHO.js} +0 -1
- package/dist/{chunk-SNAAKCTH.js → chunk-RVVH3MDA.js} +0 -1
- package/dist/{chunk-4QK76IRX.js → chunk-T5WDDPFI.js} +1 -6
- package/dist/{chunk-GLZ5MZC5.js → chunk-V2L472LS.js} +0 -30
- package/dist/consoleFmt.cjs +84 -0
- package/dist/consoleFmt.d.cts +48 -0
- package/dist/consoleFmt.js +0 -1
- package/dist/conversions.cjs +44 -0
- package/dist/conversions.d.cts +3 -0
- package/dist/conversions.js +1 -2
- package/dist/createThrottleController.cjs +183 -0
- package/dist/createThrottleController.d.cts +16 -0
- package/dist/createThrottleController.js +2 -3
- package/dist/debounce.cjs +125 -0
- package/dist/debounce.d.cts +45 -0
- package/dist/debounce.js +0 -1
- package/dist/dedent.cjs +57 -0
- package/dist/dedent.d.cts +4 -0
- package/dist/dedent.js +0 -1
- package/dist/deepEqual.cjs +88 -0
- package/dist/deepEqual.d.cts +4 -0
- package/dist/deepEqual.js +1 -2
- package/dist/enhancedMap.cjs +128 -0
- package/dist/enhancedMap.d.cts +20 -0
- package/dist/enhancedMap.d.ts +0 -2
- package/dist/enhancedMap.js +2 -3
- package/dist/exhaustiveMatch.cjs +55 -0
- package/dist/exhaustiveMatch.d.cts +6 -0
- package/dist/exhaustiveMatch.js +0 -1
- package/dist/interpolate.cjs +88 -0
- package/dist/interpolate.d.cts +11 -0
- package/dist/interpolate.js +2 -3
- package/dist/levenshtein.cjs +180 -0
- package/dist/levenshtein.d.cts +5 -0
- package/dist/levenshtein.js +0 -1
- package/dist/main.cjs +4 -0
- package/dist/main.d.cts +2 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.js +0 -1
- package/dist/mathUtils.cjs +50 -0
- package/dist/mathUtils.d.cts +6 -0
- package/dist/mathUtils.js +1 -2
- package/dist/objUtils.cjs +67 -0
- package/dist/objUtils.d.cts +7 -0
- package/dist/objUtils.js +1 -2
- package/dist/parallelAsyncCalls.cjs +266 -0
- package/dist/{parallelAsyncResultCalls.d.ts → parallelAsyncCalls.d.cts} +7 -7
- package/dist/parallelAsyncCalls.d.ts +76 -0
- package/dist/{parallelAsyncResultCalls.js → parallelAsyncCalls.js} +7 -9
- package/dist/promiseUtils.cjs +38 -0
- package/dist/promiseUtils.d.cts +8 -0
- package/dist/promiseUtils.js +0 -1
- package/dist/rsResult.cjs +180 -0
- package/dist/rsResult.d.cts +83 -0
- package/dist/rsResult.d.ts +34 -69
- package/dist/rsResult.js +6 -10
- package/dist/runShellCmd.cjs +127 -0
- package/dist/runShellCmd.d.cts +23 -0
- package/dist/runShellCmd.d.ts +2 -1
- package/dist/runShellCmd.js +9 -4
- package/dist/shallowEqual.cjs +88 -0
- package/dist/shallowEqual.d.cts +4 -0
- package/dist/shallowEqual.js +0 -1
- package/dist/sleep.cjs +32 -0
- package/dist/sleep.d.cts +3 -0
- package/dist/sleep.js +1 -2
- package/dist/stringUtils.cjs +66 -0
- package/dist/stringUtils.d.cts +16 -0
- package/dist/stringUtils.d.ts +1 -3
- package/dist/stringUtils.js +1 -4
- package/dist/testUtils.cjs +357 -0
- package/dist/testUtils.d.cts +38 -0
- package/dist/testUtils.js +5 -6
- package/dist/typingFnUtils.cjs +55 -0
- package/dist/typingFnUtils.d.cts +15 -0
- package/dist/typingFnUtils.js +0 -1
- package/dist/typingTestUtils.cjs +43 -0
- package/dist/typingTestUtils.d.cts +12 -0
- package/dist/typingTestUtils.js +0 -1
- package/dist/typingUtils.cjs +18 -0
- package/dist/typingUtils.d.cts +11 -0
- package/dist/typingUtils.js +0 -1
- package/dist/yamlStringify.cjs +346 -0
- package/dist/yamlStringify.d.cts +8 -0
- package/dist/yamlStringify.js +3 -4
- package/package.json +64 -34
- package/dist/arrayUtils.js.map +0 -1
- package/dist/assertions.js.map +0 -1
- package/dist/castValues.js.map +0 -1
- package/dist/chunk-4QK76IRX.js.map +0 -1
- package/dist/chunk-FYMTZQ42.js.map +0 -1
- package/dist/chunk-GLZ5MZC5.js.map +0 -1
- package/dist/chunk-HLFWWIDK.js.map +0 -1
- package/dist/chunk-I7ZDKDUM.js +0 -217
- package/dist/chunk-I7ZDKDUM.js.map +0 -1
- package/dist/chunk-M74KKYE4.js.map +0 -1
- package/dist/chunk-MMKMF4GG.js.map +0 -1
- package/dist/chunk-NKCC3375.js.map +0 -1
- package/dist/chunk-SNAAKCTH.js.map +0 -1
- package/dist/chunk-W7X6NSGR.js.map +0 -1
- package/dist/consoleFmt.js.map +0 -1
- package/dist/conversions.js.map +0 -1
- package/dist/createThrottleController.js.map +0 -1
- package/dist/debounce.js.map +0 -1
- package/dist/dedent.js.map +0 -1
- package/dist/deepEqual.js.map +0 -1
- package/dist/enhancedMap.js.map +0 -1
- package/dist/exhaustiveMatch.js.map +0 -1
- package/dist/interpolate.js.map +0 -1
- package/dist/levenshtein.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/dist/mathUtils.js.map +0 -1
- package/dist/objUtils.js.map +0 -1
- package/dist/parallelAsyncResultCalls.js.map +0 -1
- package/dist/promiseUtils.js.map +0 -1
- package/dist/rsResult.js.map +0 -1
- package/dist/runShellCmd.js.map +0 -1
- package/dist/shallowEqual.js.map +0 -1
- package/dist/sleep.js.map +0 -1
- package/dist/stringUtils.js.map +0 -1
- package/dist/testUtils.js.map +0 -1
- package/dist/typingFnUtils.js.map +0 -1
- package/dist/typingTestUtils.js.map +0 -1
- package/dist/typingUtils.js.map +0 -1
- package/dist/yamlStringify.js.map +0 -1
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/rsResult.ts
|
|
21
|
+
var rsResult_exports = {};
|
|
22
|
+
__export(rsResult_exports, {
|
|
23
|
+
Result: () => Result,
|
|
24
|
+
asyncResultify: () => asyncResultify,
|
|
25
|
+
createTypedResult: () => createTypedResult,
|
|
26
|
+
normalizeError: () => normalizeError,
|
|
27
|
+
resultify: () => resultify,
|
|
28
|
+
safeJsonStringify: () => safeJsonStringify
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(rsResult_exports);
|
|
31
|
+
|
|
32
|
+
// src/assertions.ts
|
|
33
|
+
function isObject(value) {
|
|
34
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/rsResult.ts
|
|
38
|
+
function okUnwrapOr() {
|
|
39
|
+
return this.value;
|
|
40
|
+
}
|
|
41
|
+
function okMap(mapFn) {
|
|
42
|
+
return this.ok ? ok(mapFn(this.value)) : this;
|
|
43
|
+
}
|
|
44
|
+
function errMap(mapFn) {
|
|
45
|
+
return this.ok ? this : err(mapFn(this.error));
|
|
46
|
+
}
|
|
47
|
+
function mapOkAndErr({
|
|
48
|
+
ok: mapFn,
|
|
49
|
+
err: mapErrFn
|
|
50
|
+
}) {
|
|
51
|
+
return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
|
|
52
|
+
}
|
|
53
|
+
function returnResult() {
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
function ok(value = void 0) {
|
|
57
|
+
return {
|
|
58
|
+
ok: true,
|
|
59
|
+
error: false,
|
|
60
|
+
value,
|
|
61
|
+
unwrapOrNull: okUnwrapOr,
|
|
62
|
+
unwrapOr: okUnwrapOr,
|
|
63
|
+
unwrap: okUnwrapOr,
|
|
64
|
+
mapOk: okMap,
|
|
65
|
+
mapErr: returnResult,
|
|
66
|
+
mapOkAndErr
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function err(error) {
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
error,
|
|
73
|
+
unwrapOrNull: () => null,
|
|
74
|
+
unwrapOr: (defaultValue) => defaultValue,
|
|
75
|
+
errorResult() {
|
|
76
|
+
return err(error);
|
|
77
|
+
},
|
|
78
|
+
unwrap: () => {
|
|
79
|
+
if (error instanceof Error) {
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
throw normalizeError(error);
|
|
83
|
+
},
|
|
84
|
+
mapOk: returnResult,
|
|
85
|
+
mapErr: errMap,
|
|
86
|
+
mapOkAndErr
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function unknownToError(error) {
|
|
90
|
+
return err(normalizeError(error));
|
|
91
|
+
}
|
|
92
|
+
async function asyncUnwrap(result) {
|
|
93
|
+
const unwrapped = await result;
|
|
94
|
+
if (unwrapped.ok) {
|
|
95
|
+
return unwrapped.value;
|
|
96
|
+
}
|
|
97
|
+
if (unwrapped.error instanceof Error) {
|
|
98
|
+
throw unwrapped.error;
|
|
99
|
+
}
|
|
100
|
+
throw normalizeError(unwrapped.error);
|
|
101
|
+
}
|
|
102
|
+
function asyncMap(resultPromise) {
|
|
103
|
+
return {
|
|
104
|
+
err: async (mapFn) => {
|
|
105
|
+
const result = await resultPromise;
|
|
106
|
+
return result.ok ? ok(result.value) : err(mapFn(result.error));
|
|
107
|
+
},
|
|
108
|
+
ok: async (mapFn) => {
|
|
109
|
+
const result = await resultPromise;
|
|
110
|
+
return result.ok ? ok(mapFn(result.value)) : err(result.error);
|
|
111
|
+
},
|
|
112
|
+
okAndErr: async ({
|
|
113
|
+
ok: mapFn,
|
|
114
|
+
err: mapErrFn
|
|
115
|
+
}) => {
|
|
116
|
+
const result = await resultPromise;
|
|
117
|
+
return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
var Result = {
|
|
122
|
+
ok,
|
|
123
|
+
err,
|
|
124
|
+
unknownToError,
|
|
125
|
+
asyncUnwrap,
|
|
126
|
+
asyncMap
|
|
127
|
+
};
|
|
128
|
+
function resultify(fn, errorNormalizer) {
|
|
129
|
+
try {
|
|
130
|
+
return ok(fn());
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return err(
|
|
133
|
+
errorNormalizer ? errorNormalizer(error) : normalizeError(error)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function asyncResultify(fn, errorNormalizer) {
|
|
138
|
+
try {
|
|
139
|
+
return ok(await fn());
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return err(
|
|
142
|
+
errorNormalizer ? errorNormalizer(error) : normalizeError(error)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function normalizeError(error) {
|
|
147
|
+
if (error instanceof Error) return error;
|
|
148
|
+
if (typeof error === "string") {
|
|
149
|
+
return new Error(error);
|
|
150
|
+
}
|
|
151
|
+
if (isObject(error)) {
|
|
152
|
+
return new Error(
|
|
153
|
+
"message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
|
|
154
|
+
{ cause: error }
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return new Error(safeJsonStringify(error) ?? "unknown", { cause: error });
|
|
158
|
+
}
|
|
159
|
+
function safeJsonStringify(value) {
|
|
160
|
+
try {
|
|
161
|
+
return JSON.stringify(value);
|
|
162
|
+
} catch (_) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function createTypedResult() {
|
|
167
|
+
return {
|
|
168
|
+
ok,
|
|
169
|
+
err
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
173
|
+
0 && (module.exports = {
|
|
174
|
+
Result,
|
|
175
|
+
asyncResultify,
|
|
176
|
+
createTypedResult,
|
|
177
|
+
normalizeError,
|
|
178
|
+
resultify,
|
|
179
|
+
safeJsonStringify
|
|
180
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
type Ok<T> = {
|
|
2
|
+
ok: true;
|
|
3
|
+
error: false;
|
|
4
|
+
value: T;
|
|
5
|
+
};
|
|
6
|
+
type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
|
|
7
|
+
type Err<E extends ResultValidErrors> = {
|
|
8
|
+
ok: false;
|
|
9
|
+
error: E;
|
|
10
|
+
errorResult: () => Result<any, E>;
|
|
11
|
+
};
|
|
12
|
+
type ResultMethods<T, E extends ResultValidErrors> = {
|
|
13
|
+
/** Returns the value if the result is Ok, otherwise returns null */
|
|
14
|
+
unwrapOrNull: () => T | null;
|
|
15
|
+
/** Returns the value if the result is Ok, otherwise returns the provided default value */
|
|
16
|
+
unwrapOr: <R extends T>(defaultValue: R) => T | R;
|
|
17
|
+
unwrap: () => T;
|
|
18
|
+
mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
|
|
19
|
+
mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
|
|
20
|
+
mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
|
|
21
|
+
ok: (value: T) => NewValue;
|
|
22
|
+
err: (error: E) => NewError;
|
|
23
|
+
}) => Result<NewValue, NewError>;
|
|
24
|
+
};
|
|
25
|
+
type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
|
|
26
|
+
declare function ok(): OkResult<void, any>;
|
|
27
|
+
declare function ok<T>(value: T): OkResult<T, any>;
|
|
28
|
+
type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
|
|
29
|
+
declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
|
|
30
|
+
declare function unknownToError(error: unknown): ErrResult<Error, any>;
|
|
31
|
+
/** Unwraps a promise result */
|
|
32
|
+
declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
|
|
33
|
+
declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
|
|
34
|
+
err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
|
|
35
|
+
ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
|
|
36
|
+
okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
|
|
37
|
+
ok: (value: T) => NewValue;
|
|
38
|
+
err: (error: E) => NewError;
|
|
39
|
+
}) => Promise<Result<NewValue, NewError>>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Util for implementing something similar to Result<T, E> in Rust, for better error handling.
|
|
43
|
+
*
|
|
44
|
+
* Usage:
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* function doSomething(): Result<string, Error> {
|
|
48
|
+
* if (something) {
|
|
49
|
+
* return ok('success');
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* return err(new Error('something went wrong'));
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* const result = doSomething();
|
|
56
|
+
*
|
|
57
|
+
* if (result.ok) {
|
|
58
|
+
* // result.value is a string
|
|
59
|
+
* } else {
|
|
60
|
+
* // result.error is an Error
|
|
61
|
+
* }
|
|
62
|
+
*/
|
|
63
|
+
type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
|
|
64
|
+
declare const Result: {
|
|
65
|
+
ok: typeof ok;
|
|
66
|
+
err: typeof err;
|
|
67
|
+
unknownToError: typeof unknownToError;
|
|
68
|
+
asyncUnwrap: typeof asyncUnwrap;
|
|
69
|
+
asyncMap: typeof asyncMap;
|
|
70
|
+
};
|
|
71
|
+
/** transform a function in a result function */
|
|
72
|
+
declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
|
|
73
|
+
/** transform a async function in a result function */
|
|
74
|
+
declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
|
|
75
|
+
declare function normalizeError(error: unknown): Error;
|
|
76
|
+
declare function safeJsonStringify(value: unknown): string | null;
|
|
77
|
+
type TypedResult<T, E extends ResultValidErrors = Error> = {
|
|
78
|
+
ok: (value: T) => OkResult<T, E, T>;
|
|
79
|
+
err: (error: E) => ErrResult<E, T>;
|
|
80
|
+
};
|
|
81
|
+
declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
|
|
82
|
+
|
|
83
|
+
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
|
package/dist/rsResult.d.ts
CHANGED
|
@@ -3,47 +3,41 @@ type Ok<T> = {
|
|
|
3
3
|
error: false;
|
|
4
4
|
value: T;
|
|
5
5
|
};
|
|
6
|
-
type
|
|
7
|
-
id: string;
|
|
8
|
-
message: string;
|
|
9
|
-
code?: number;
|
|
10
|
-
metadata?: ValidErrorMetadata;
|
|
11
|
-
};
|
|
12
|
-
type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
|
|
6
|
+
type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
|
|
13
7
|
type Err<E extends ResultValidErrors> = {
|
|
14
8
|
ok: false;
|
|
15
9
|
error: E;
|
|
16
10
|
errorResult: () => Result<any, E>;
|
|
17
|
-
normalizedErrorResult: (normalizedErr: E extends NormalizedError ? {
|
|
18
|
-
withMetadata: ValidErrorMetadata;
|
|
19
|
-
} | NormalizedErrorResultProps : NormalizedErrorResultProps) => Result<any, NormalizedError>;
|
|
20
11
|
};
|
|
21
|
-
type
|
|
22
|
-
type ResultMethods<T, E> = {
|
|
12
|
+
type ResultMethods<T, E extends ResultValidErrors> = {
|
|
23
13
|
/** Returns the value if the result is Ok, otherwise returns null */
|
|
24
14
|
unwrapOrNull: () => T | null;
|
|
25
15
|
/** Returns the value if the result is Ok, otherwise returns the provided default value */
|
|
26
16
|
unwrapOr: <R extends T>(defaultValue: R) => T | R;
|
|
27
|
-
unwrap:
|
|
17
|
+
unwrap: () => T;
|
|
18
|
+
mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
|
|
19
|
+
mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
|
|
20
|
+
mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
|
|
21
|
+
ok: (value: T) => NewValue;
|
|
22
|
+
err: (error: E) => NewError;
|
|
23
|
+
}) => Result<NewValue, NewError>;
|
|
28
24
|
};
|
|
29
25
|
type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
|
|
30
|
-
type ResultValidErrors = Error | Record<string, unknown> | string[];
|
|
31
26
|
declare function ok(): OkResult<void, any>;
|
|
32
27
|
declare function ok<T>(value: T): OkResult<T, any>;
|
|
33
28
|
type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
|
|
34
29
|
declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
|
|
35
|
-
declare function
|
|
36
|
-
declare function normalizedErr(message: string): ErrResult<NormalizedError>;
|
|
37
|
-
declare function normalizedErr<T extends string>(id: T, message: string): ErrResult<NormalizedError<T>>;
|
|
38
|
-
declare function normalizedErr<T extends string>(errorProps: {
|
|
39
|
-
id: T;
|
|
40
|
-
message: string;
|
|
41
|
-
code?: number;
|
|
42
|
-
cause?: Error;
|
|
43
|
-
metadata?: ValidErrorMetadata;
|
|
44
|
-
}): ErrResult<NormalizedError<T>>;
|
|
30
|
+
declare function unknownToError(error: unknown): ErrResult<Error, any>;
|
|
45
31
|
/** Unwraps a promise result */
|
|
46
|
-
declare function
|
|
32
|
+
declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
|
|
33
|
+
declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
|
|
34
|
+
err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
|
|
35
|
+
ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
|
|
36
|
+
okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
|
|
37
|
+
ok: (value: T) => NewValue;
|
|
38
|
+
err: (error: E) => NewError;
|
|
39
|
+
}) => Promise<Result<NewValue, NewError>>;
|
|
40
|
+
};
|
|
47
41
|
/**
|
|
48
42
|
* Util for implementing something similar to Result<T, E> in Rust, for better error handling.
|
|
49
43
|
*
|
|
@@ -66,53 +60,24 @@ declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
|
|
|
66
60
|
* // result.error is an Error
|
|
67
61
|
* }
|
|
68
62
|
*/
|
|
69
|
-
type Result<T, E extends ResultValidErrors =
|
|
63
|
+
type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
|
|
70
64
|
declare const Result: {
|
|
71
65
|
ok: typeof ok;
|
|
72
66
|
err: typeof err;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
unknownToError: typeof unknownToError;
|
|
68
|
+
asyncUnwrap: typeof asyncUnwrap;
|
|
69
|
+
asyncMap: typeof asyncMap;
|
|
76
70
|
};
|
|
77
|
-
/**
|
|
78
|
-
declare function resultify<T, E extends ResultValidErrors =
|
|
71
|
+
/** transform a function in a result function */
|
|
72
|
+
declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
|
|
79
73
|
/** transform a async function in a result function */
|
|
80
|
-
declare function asyncResultify<T, E extends Error =
|
|
81
|
-
|
|
82
|
-
declare function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
constructor({ id, message, metadata, cause, code, }: {
|
|
89
|
-
id: T;
|
|
90
|
-
message: string;
|
|
91
|
-
metadata?: ValidErrorMetadata;
|
|
92
|
-
cause?: Error;
|
|
93
|
-
code?: number;
|
|
94
|
-
});
|
|
95
|
-
toString(): string;
|
|
96
|
-
toJSON(): {
|
|
97
|
-
id: T;
|
|
98
|
-
message: string;
|
|
99
|
-
metadata: ValidErrorMetadata;
|
|
100
|
-
code: number;
|
|
101
|
-
cause: string | (Error & Record<"toJSON", unknown>) | undefined;
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
declare class NormalizedErrorWithMetadata<M extends ValidErrorMetadata, T = string> extends NormalizedError<T> {
|
|
105
|
-
metadata: M;
|
|
106
|
-
constructor(props: {
|
|
107
|
-
id: T;
|
|
108
|
-
message: string;
|
|
109
|
-
metadata: M;
|
|
110
|
-
cause?: Error;
|
|
111
|
-
} | {
|
|
112
|
-
error: NormalizedError<T>;
|
|
113
|
-
metadata: M;
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
declare function normalizeError(error: unknown): NormalizedError;
|
|
74
|
+
declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
|
|
75
|
+
declare function normalizeError(error: unknown): Error;
|
|
76
|
+
declare function safeJsonStringify(value: unknown): string | null;
|
|
77
|
+
type TypedResult<T, E extends ResultValidErrors = Error> = {
|
|
78
|
+
ok: (value: T) => OkResult<T, E, T>;
|
|
79
|
+
err: (error: E) => ErrResult<E, T>;
|
|
80
|
+
};
|
|
81
|
+
declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
|
|
117
82
|
|
|
118
|
-
export {
|
|
83
|
+
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
|
package/dist/rsResult.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
|
-
NormalizedError,
|
|
3
|
-
NormalizedErrorWithMetadata,
|
|
4
2
|
Result,
|
|
5
3
|
asyncResultify,
|
|
4
|
+
createTypedResult,
|
|
6
5
|
normalizeError,
|
|
7
6
|
resultify,
|
|
8
|
-
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import "./chunk-
|
|
11
|
-
import "./chunk-FYMTZQ42.js";
|
|
7
|
+
safeJsonStringify
|
|
8
|
+
} from "./chunk-3PXKYWJF.js";
|
|
9
|
+
import "./chunk-4UGSP3L3.js";
|
|
12
10
|
export {
|
|
13
|
-
NormalizedError,
|
|
14
|
-
NormalizedErrorWithMetadata,
|
|
15
11
|
Result,
|
|
16
12
|
asyncResultify,
|
|
13
|
+
createTypedResult,
|
|
17
14
|
normalizeError,
|
|
18
15
|
resultify,
|
|
19
|
-
|
|
16
|
+
safeJsonStringify
|
|
20
17
|
};
|
|
21
|
-
//# sourceMappingURL=rsResult.js.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/runShellCmd.ts
|
|
21
|
+
var runShellCmd_exports = {};
|
|
22
|
+
__export(runShellCmd_exports, {
|
|
23
|
+
concurrentCmd: () => concurrentCmd,
|
|
24
|
+
runCmd: () => runCmd,
|
|
25
|
+
runCmdSilent: () => runCmdSilent,
|
|
26
|
+
runCmdSilentUnwrap: () => runCmdSilentUnwrap,
|
|
27
|
+
runCmdUnwrap: () => runCmdUnwrap
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(runShellCmd_exports);
|
|
30
|
+
var import_child_process = require("child_process");
|
|
31
|
+
function runCmd(label, command, {
|
|
32
|
+
mock,
|
|
33
|
+
silent,
|
|
34
|
+
throwOnError,
|
|
35
|
+
cwd = process.cwd(),
|
|
36
|
+
noCiColorForce
|
|
37
|
+
} = {}) {
|
|
38
|
+
if (mock) return Promise.resolve(mock);
|
|
39
|
+
if (label && (!silent || silent === "timeOnly")) {
|
|
40
|
+
console.log(`----${label}----`);
|
|
41
|
+
console.time("\u2705");
|
|
42
|
+
}
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const [cmd = "", ...args] = Array.isArray(command) ? command.flatMap(
|
|
45
|
+
(item) => item.startsWith("$") ? item.replace("$", "").split(" ") : item
|
|
46
|
+
) : command.split(" ");
|
|
47
|
+
const child = (0, import_child_process.spawn)(cmd, args, {
|
|
48
|
+
cwd,
|
|
49
|
+
env: noCiColorForce ? void 0 : { ...process.env, CLICOLOR_FORCE: "1" }
|
|
50
|
+
});
|
|
51
|
+
let stdout = "";
|
|
52
|
+
let stderr = "";
|
|
53
|
+
let out = "";
|
|
54
|
+
child.stdout.on("data", (data) => {
|
|
55
|
+
stdout += String(data);
|
|
56
|
+
out += String(data);
|
|
57
|
+
if (!silent) {
|
|
58
|
+
console.log(String(data));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
child.stderr.on("data", (data) => {
|
|
62
|
+
stderr += String(data);
|
|
63
|
+
out += String(data);
|
|
64
|
+
if (!silent) {
|
|
65
|
+
console.log(String(data));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
child.on("close", (code) => {
|
|
69
|
+
const hasError = code !== 0;
|
|
70
|
+
if (!silent || silent === "timeOnly") {
|
|
71
|
+
if (!hasError) {
|
|
72
|
+
console.timeEnd("\u2705");
|
|
73
|
+
}
|
|
74
|
+
console.log("\n");
|
|
75
|
+
}
|
|
76
|
+
if (throwOnError && hasError) {
|
|
77
|
+
if (silent) {
|
|
78
|
+
if (label) {
|
|
79
|
+
console.log(`----${label}----`);
|
|
80
|
+
} else {
|
|
81
|
+
console.trace();
|
|
82
|
+
}
|
|
83
|
+
console.error(stderr);
|
|
84
|
+
}
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
resolve({ label, out, stderr, stdout, error: hasError });
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async function concurrentCmd(label, cmd, onResult) {
|
|
92
|
+
const start = Date.now();
|
|
93
|
+
const result = await runCmd(label, cmd, { silent: true });
|
|
94
|
+
onResult(result);
|
|
95
|
+
const elapsedSeconds = (Date.now() - start) / 1e3;
|
|
96
|
+
console.log(
|
|
97
|
+
`${result.error ? "\u{1F534}" : "\u2705"} ${result.label} (${elapsedSeconds.toFixed(
|
|
98
|
+
1
|
|
99
|
+
)}s)`
|
|
100
|
+
);
|
|
101
|
+
return () => {
|
|
102
|
+
if (result.error) {
|
|
103
|
+
console.log(`\u274C ${result.label} errors:`);
|
|
104
|
+
console.log(result.out);
|
|
105
|
+
console.log("\n");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async function runCmdUnwrap(label, command, {
|
|
110
|
+
silent
|
|
111
|
+
} = {}) {
|
|
112
|
+
return (await runCmd(label, command, { silent, throwOnError: true })).stdout;
|
|
113
|
+
}
|
|
114
|
+
function runCmdSilent(command) {
|
|
115
|
+
return runCmd(null, command, { silent: true });
|
|
116
|
+
}
|
|
117
|
+
function runCmdSilentUnwrap(command) {
|
|
118
|
+
return runCmdUnwrap(null, command, { silent: true });
|
|
119
|
+
}
|
|
120
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
121
|
+
0 && (module.exports = {
|
|
122
|
+
concurrentCmd,
|
|
123
|
+
runCmd,
|
|
124
|
+
runCmdSilent,
|
|
125
|
+
runCmdSilentUnwrap,
|
|
126
|
+
runCmdUnwrap
|
|
127
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type CmdResult = {
|
|
2
|
+
label: string | null;
|
|
3
|
+
out: string;
|
|
4
|
+
error: boolean;
|
|
5
|
+
stdout: string;
|
|
6
|
+
stderr: string;
|
|
7
|
+
};
|
|
8
|
+
type RunCmdOptions = {
|
|
9
|
+
mock?: CmdResult;
|
|
10
|
+
silent?: boolean | 'timeOnly';
|
|
11
|
+
cwd?: string;
|
|
12
|
+
throwOnError?: boolean;
|
|
13
|
+
noCiColorForce?: boolean;
|
|
14
|
+
};
|
|
15
|
+
declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
|
|
16
|
+
declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
|
|
17
|
+
declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
|
|
18
|
+
silent?: boolean | 'timeOnly';
|
|
19
|
+
}): Promise<string>;
|
|
20
|
+
declare function runCmdSilent(command: string | string[]): Promise<CmdResult>;
|
|
21
|
+
declare function runCmdSilentUnwrap(command: string | string[]): Promise<string>;
|
|
22
|
+
|
|
23
|
+
export { concurrentCmd, runCmd, runCmdSilent, runCmdSilentUnwrap, runCmdUnwrap };
|
package/dist/runShellCmd.d.ts
CHANGED
|
@@ -10,8 +10,9 @@ type RunCmdOptions = {
|
|
|
10
10
|
silent?: boolean | 'timeOnly';
|
|
11
11
|
cwd?: string;
|
|
12
12
|
throwOnError?: boolean;
|
|
13
|
+
noCiColorForce?: boolean;
|
|
13
14
|
};
|
|
14
|
-
declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd }?: RunCmdOptions): Promise<CmdResult>;
|
|
15
|
+
declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
|
|
15
16
|
declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
|
|
16
17
|
declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
|
|
17
18
|
silent?: boolean | 'timeOnly';
|
package/dist/runShellCmd.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
// src/runShellCmd.ts
|
|
2
2
|
import { spawn } from "child_process";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
function runCmd(label, command, {
|
|
4
|
+
mock,
|
|
5
|
+
silent,
|
|
6
|
+
throwOnError,
|
|
7
|
+
cwd = process.cwd(),
|
|
8
|
+
noCiColorForce
|
|
9
|
+
} = {}) {
|
|
5
10
|
if (mock) return Promise.resolve(mock);
|
|
6
11
|
if (label && (!silent || silent === "timeOnly")) {
|
|
7
12
|
console.log(`----${label}----`);
|
|
@@ -12,7 +17,8 @@ function runCmd(label, command, { mock, silent, throwOnError, cwd = process.cwd(
|
|
|
12
17
|
(item) => item.startsWith("$") ? item.replace("$", "").split(" ") : item
|
|
13
18
|
) : command.split(" ");
|
|
14
19
|
const child = spawn(cmd, args, {
|
|
15
|
-
cwd
|
|
20
|
+
cwd,
|
|
21
|
+
env: noCiColorForce ? void 0 : { ...process.env, CLICOLOR_FORCE: "1" }
|
|
16
22
|
});
|
|
17
23
|
let stdout = "";
|
|
18
24
|
let stderr = "";
|
|
@@ -90,4 +96,3 @@ export {
|
|
|
90
96
|
runCmdSilentUnwrap,
|
|
91
97
|
runCmdUnwrap
|
|
92
98
|
};
|
|
93
|
-
//# sourceMappingURL=runShellCmd.js.map
|