@darthcav/ts-utils 0.10.10 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -39
- package/dist/index.d.ts +16 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/loggers/getConsoleLogger.d.ts +10 -4
- package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
- package/dist/loggers/getConsoleLogger.js +36 -9
- package/dist/loggers/getConsoleLogger.js.map +1 -1
- package/dist/loggers/getDummyLogger.d.ts +14 -3
- package/dist/loggers/getDummyLogger.d.ts.map +1 -1
- package/dist/loggers/getDummyLogger.js +29 -11
- package/dist/loggers/getDummyLogger.js.map +1 -1
- package/dist/main.d.ts +83 -51
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +200 -40
- package/dist/main.js.map +1 -1
- package/dist/millisecondsToString.d.ts +1 -1
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +1 -1
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +25 -7
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +52 -14
- package/dist/monitorMemory.js.map +1 -1
- package/dist/noop.d.ts +1 -2
- package/dist/noop.d.ts.map +1 -1
- package/dist/noop.js +1 -2
- package/dist/noop.js.map +1 -1
- package/dist/{os-utils.d.ts → osRelease.d.ts} +11 -7
- package/dist/osRelease.d.ts.map +1 -0
- package/dist/osRelease.js +153 -0
- package/dist/osRelease.js.map +1 -0
- package/dist/type-testing/Assert.d.ts +23 -0
- package/dist/type-testing/Assert.d.ts.map +1 -0
- package/dist/type-testing/Assert.js +2 -0
- package/dist/type-testing/Assert.js.map +1 -0
- package/dist/type-testing/Equal.d.ts +25 -0
- package/dist/type-testing/Equal.d.ts.map +1 -0
- package/dist/type-testing/Equal.js +2 -0
- package/dist/type-testing/Equal.js.map +1 -0
- package/dist/type-testing/Extends.d.ts +22 -0
- package/dist/type-testing/Extends.d.ts.map +1 -0
- package/dist/type-testing/Extends.js +2 -0
- package/dist/type-testing/Extends.js.map +1 -0
- package/dist/type-testing/IsAny.d.ts +17 -0
- package/dist/type-testing/IsAny.d.ts.map +1 -0
- package/dist/type-testing/IsAny.js +2 -0
- package/dist/type-testing/IsAny.js.map +1 -0
- package/dist/type-testing/IsNever.d.ts +17 -0
- package/dist/type-testing/IsNever.d.ts.map +1 -0
- package/dist/type-testing/IsNever.js +2 -0
- package/dist/type-testing/IsNever.js.map +1 -0
- package/dist/type-testing/IsUnknown.d.ts +20 -0
- package/dist/type-testing/IsUnknown.d.ts.map +1 -0
- package/dist/type-testing/IsUnknown.js +2 -0
- package/dist/type-testing/IsUnknown.js.map +1 -0
- package/dist/type-testing/Not.d.ts +17 -0
- package/dist/type-testing/Not.d.ts.map +1 -0
- package/dist/type-testing/Not.js +2 -0
- package/dist/type-testing/Not.js.map +1 -0
- package/dist/types.d.ts +42 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +61 -16
- package/dist/types.js.map +1 -1
- package/package.json +10 -12
- package/src/index.ts +26 -7
- package/src/loggers/getConsoleLogger.ts +41 -8
- package/src/loggers/getDummyLogger.ts +31 -11
- package/src/main.ts +249 -126
- package/src/millisecondsToString.ts +1 -4
- package/src/monitorMemory.ts +58 -19
- package/src/noop.ts +1 -3
- package/src/osRelease.ts +172 -0
- package/src/type-testing/Assert.ts +22 -0
- package/src/type-testing/Equal.ts +27 -0
- package/src/type-testing/Extends.ts +21 -0
- package/src/type-testing/IsAny.ts +16 -0
- package/src/type-testing/IsNever.ts +16 -0
- package/src/type-testing/IsUnknown.ts +21 -0
- package/src/type-testing/Not.ts +16 -0
- package/src/types.ts +65 -17
- package/dist/os-utils.d.ts.map +0 -1
- package/dist/os-utils.js +0 -94
- package/dist/os-utils.js.map +0 -1
- package/src/os-utils.ts +0 -110
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAA;IAChB,CAAC;IACD,MAAM,SAAS,GAAY,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IACvD,oEAAoE;IACpE,4CAA4C;IAC5C,IACI,SAAS,KAAK,IAAI;QAClB,SAAS,KAAK,MAAM,CAAC,SAAS;QAC9B,MAAM,CAAC,cAAc,CAAC,SAAS,CAAC,KAAK,IAAI,EAC3C,CAAC;QACC,OAAO,KAAK,CAAA;IAChB,CAAC;IACD,mEAAmE;IACnE,OAAO,CAAC,CAAC,MAAM,CAAC,WAAW,IAAI,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,CAAA;AACrE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC1C,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACrD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAA;AACpC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACnC,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAC9C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAc;IAC/C,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;AACpE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darthcav/ts-utils",
|
|
3
3
|
"description": "A collection of utility functions for TypeScript applications and modules",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.12.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "darthcav",
|
|
7
7
|
"url": "https://github.com/darthcav"
|
|
@@ -24,14 +24,12 @@
|
|
|
24
24
|
"!dist/__tests__"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logtape/logtape": "2.3.
|
|
27
|
+
"@logtape/logtape": "2.3.11"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@biomejs/biome": "2.5.
|
|
31
|
-
"@types/node": "26.
|
|
32
|
-
"
|
|
33
|
-
"prettier": "3.9.6",
|
|
34
|
-
"shx": "0.4.0",
|
|
30
|
+
"@biomejs/biome": "2.5.15",
|
|
31
|
+
"@types/node": "26.6.4",
|
|
32
|
+
"prettier": "3.9.9",
|
|
35
33
|
"typedoc": "0.28.20"
|
|
36
34
|
},
|
|
37
35
|
"repository": {
|
|
@@ -43,19 +41,19 @@
|
|
|
43
41
|
},
|
|
44
42
|
"scripts": {
|
|
45
43
|
"\n===== Build =====": "",
|
|
46
|
-
"clean": "
|
|
44
|
+
"clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
|
|
47
45
|
"build": "npm run clean && tsc",
|
|
48
46
|
"prepare": "npm run build",
|
|
49
47
|
"\n===== Typecheck =====": "",
|
|
50
48
|
"typecheck": "tsc --noEmit",
|
|
51
49
|
"\n===== Test =====": "",
|
|
52
|
-
"test": "node --env-file=.env.local --experimental-test-module-mocks --test --test-reporter=spec
|
|
53
|
-
"test:coverage": "node --env-file=.env.local --experimental-test-module-mocks --experimental-test-coverage --test
|
|
54
|
-
"test:coverage:lcov": "
|
|
50
|
+
"test": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --test --test-reporter=spec \"src/**/*.test.ts\"",
|
|
51
|
+
"test:coverage": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test \"src/**/*.test.ts\"",
|
|
52
|
+
"test:coverage:lcov": "node -e \"fs.mkdirSync('coverage', { recursive: true })\" && node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout --test \"src/**/*.test.ts\"",
|
|
55
53
|
"\n===== Linter =====": "",
|
|
56
54
|
"lint": "biome check . && prettier --check \"**/*.md\" --ignore-path .gitignore",
|
|
57
55
|
"lint:fix": "biome check --write . && prettier --write \"**/*.md\" --ignore-path .gitignore",
|
|
58
56
|
"\n===== TypeDoc =====": "",
|
|
59
|
-
"doc": "
|
|
57
|
+
"doc": "typedoc --tsconfig tsconfig.json"
|
|
60
58
|
}
|
|
61
59
|
}
|
package/src/index.ts
CHANGED
|
@@ -4,29 +4,48 @@
|
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import getConsoleLogger from "./loggers/getConsoleLogger.ts"
|
|
8
|
-
import getDummyLogger from "./loggers/getDummyLogger.ts"
|
|
9
|
-
import { type LauncherFunction, main } from "./main.ts"
|
|
10
|
-
import millisecondsToString from "./millisecondsToString.ts"
|
|
11
|
-
import monitorMemory from "./monitorMemory.ts"
|
|
12
|
-
import noop from "./noop.ts"
|
|
13
|
-
import { type OsRelease, osRelease } from "./
|
|
7
|
+
import { getConsoleLogger } from "./loggers/getConsoleLogger.ts"
|
|
8
|
+
import { getDummyLogger } from "./loggers/getDummyLogger.ts"
|
|
9
|
+
import { type LauncherFunction, type MainOptions, main } from "./main.ts"
|
|
10
|
+
import { millisecondsToString } from "./millisecondsToString.ts"
|
|
11
|
+
import { monitorMemory } from "./monitorMemory.ts"
|
|
12
|
+
import { noop } from "./noop.ts"
|
|
13
|
+
import { type OsRelease, osRelease } from "./osRelease.ts"
|
|
14
|
+
import type { Assert } from "./type-testing/Assert.ts"
|
|
15
|
+
import type { Equal } from "./type-testing/Equal.ts"
|
|
16
|
+
import type { Extends } from "./type-testing/Extends.ts"
|
|
17
|
+
import type { IsAny } from "./type-testing/IsAny.ts"
|
|
18
|
+
import type { IsNever } from "./type-testing/IsNever.ts"
|
|
19
|
+
import type { IsUnknown } from "./type-testing/IsUnknown.ts"
|
|
20
|
+
import type { Not } from "./type-testing/Not.ts"
|
|
14
21
|
import {
|
|
15
22
|
asRuntimeObject,
|
|
16
23
|
asString,
|
|
24
|
+
isRuntimeObject,
|
|
25
|
+
isString,
|
|
17
26
|
type RuntimeObject,
|
|
18
27
|
toRuntimeObjectArray,
|
|
19
28
|
} from "./types.ts"
|
|
20
29
|
|
|
21
30
|
export {
|
|
31
|
+
type Assert,
|
|
22
32
|
asRuntimeObject,
|
|
23
33
|
asString,
|
|
34
|
+
type Equal,
|
|
35
|
+
type Extends,
|
|
24
36
|
getConsoleLogger,
|
|
25
37
|
getDummyLogger,
|
|
38
|
+
type IsAny,
|
|
39
|
+
type IsNever,
|
|
40
|
+
type IsUnknown,
|
|
41
|
+
isRuntimeObject,
|
|
42
|
+
isString,
|
|
26
43
|
type LauncherFunction,
|
|
44
|
+
type MainOptions,
|
|
27
45
|
main,
|
|
28
46
|
millisecondsToString,
|
|
29
47
|
monitorMemory,
|
|
48
|
+
type Not,
|
|
30
49
|
noop,
|
|
31
50
|
type OsRelease,
|
|
32
51
|
osRelease,
|
|
@@ -1,18 +1,52 @@
|
|
|
1
|
+
import process from "node:process"
|
|
1
2
|
import {
|
|
2
3
|
configure,
|
|
3
4
|
getAnsiColorFormatter,
|
|
4
5
|
getConsoleSink,
|
|
5
6
|
getLogger,
|
|
7
|
+
getTextFormatter,
|
|
6
8
|
type Logger,
|
|
7
9
|
type LogLevel,
|
|
10
|
+
type TextFormatterOptions,
|
|
8
11
|
} from "@logtape/logtape"
|
|
9
12
|
|
|
13
|
+
/** Formatter options shared by the colored and the plain formatter. */
|
|
14
|
+
const FORMATTER_OPTIONS: TextFormatterOptions = {
|
|
15
|
+
timestamp: "rfc3339",
|
|
16
|
+
level: "FULL",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decides whether console output should use ANSI colors.
|
|
21
|
+
*
|
|
22
|
+
* `FORCE_COLOR` wins when set (`"0"` or `"false"` disable colors, any other
|
|
23
|
+
* value enables them). Otherwise colors are used only when both stdout and
|
|
24
|
+
* stderr are terminals that support them, since the console sink writes
|
|
25
|
+
* warnings and errors to stderr. `hasColors()` also honors `NO_COLOR`,
|
|
26
|
+
* `NODE_DISABLE_COLORS`, and `TERM=dumb`.
|
|
27
|
+
*/
|
|
28
|
+
function useColors(): boolean {
|
|
29
|
+
const force = process.env["FORCE_COLOR"]
|
|
30
|
+
if (force !== undefined) {
|
|
31
|
+
return force !== "0" && force !== "false"
|
|
32
|
+
}
|
|
33
|
+
return [process.stdout, process.stderr].every(
|
|
34
|
+
(stream) => stream.isTTY === true && stream.hasColors(),
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
10
38
|
/**
|
|
11
39
|
* Configures logging and returns a {@link Logger} for the given category name.
|
|
12
40
|
*
|
|
13
|
-
* Records at or above `lowestLevel` are written to the console
|
|
14
|
-
*
|
|
15
|
-
*
|
|
41
|
+
* Records at or above `lowestLevel` are written to the console with RFC 3339
|
|
42
|
+
* timestamps and full level names. The internal `logtape/meta` logger is
|
|
43
|
+
* silenced.
|
|
44
|
+
*
|
|
45
|
+
* ANSI colors are used only when both stdout and stderr are terminals that
|
|
46
|
+
* support them (respecting `NO_COLOR`, `NODE_DISABLE_COLORS`, and `TERM=dumb`),
|
|
47
|
+
* so output redirected to files or pipes stays free of escape codes. Set
|
|
48
|
+
* `FORCE_COLOR` to override: `"0"` or `"false"` disables colors, any other
|
|
49
|
+
* value enables them. The decision is made when this function is called.
|
|
16
50
|
*
|
|
17
51
|
* Logging is configured with `reset: true`, so this function is safe to call
|
|
18
52
|
* more than once: each call reconfigures logtape from scratch and the most
|
|
@@ -23,17 +57,16 @@ import {
|
|
|
23
57
|
* @param lowestLevel - The minimum log level to output. Defaults to `"info"`.
|
|
24
58
|
* @returns A promise resolving to a logger scoped to the given category.
|
|
25
59
|
*/
|
|
26
|
-
export
|
|
60
|
+
export async function getConsoleLogger(
|
|
27
61
|
name: string,
|
|
28
62
|
lowestLevel: LogLevel = "info",
|
|
29
63
|
): Promise<Logger> {
|
|
30
64
|
await configure({
|
|
31
65
|
sinks: {
|
|
32
66
|
console: getConsoleSink({
|
|
33
|
-
formatter:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}),
|
|
67
|
+
formatter: useColors()
|
|
68
|
+
? getAnsiColorFormatter(FORMATTER_OPTIONS)
|
|
69
|
+
: getTextFormatter(FORMATTER_OPTIONS),
|
|
37
70
|
}),
|
|
38
71
|
},
|
|
39
72
|
loggers: [
|
|
@@ -1,26 +1,46 @@
|
|
|
1
1
|
import type { Logger, LogLevel, LogRecord } from "@logtape/logtape"
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Already-settled promise returned by every logging method. It satisfies the
|
|
5
|
+
* overloads that return `Promise<void>` (asynchronous lazy properties), just as
|
|
6
|
+
* a real logtape logger does for a disabled level.
|
|
7
|
+
*/
|
|
8
|
+
const settled: Promise<void> = Promise.resolve()
|
|
9
|
+
|
|
10
|
+
/** A logging method that accepts every logtape overload and does nothing. */
|
|
11
|
+
const log = (..._args: readonly unknown[]): Promise<void> => settled
|
|
3
12
|
|
|
4
13
|
/**
|
|
5
14
|
* Returns a no-op {@link Logger} useful as a placeholder in tests.
|
|
6
15
|
*
|
|
7
16
|
* All logging methods (`trace`, `debug`, `info`, `warn`, `warning`, `error`,
|
|
8
|
-
* `fatal`) are no-ops
|
|
9
|
-
*
|
|
17
|
+
* `fatal`) are no-ops; the ones logtape types as returning a promise return an
|
|
18
|
+
* already-settled one. `isEnabledFor` always returns `false`. `getChild` and
|
|
19
|
+
* `with` return the same dummy logger instance.
|
|
20
|
+
*
|
|
21
|
+
* Each call returns a new instance, so mocking a method on one (e.g. with
|
|
22
|
+
* `mock.method(logger, "info")`) does not affect others.
|
|
10
23
|
*
|
|
11
24
|
* @returns A logger whose methods are all no-ops.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { getDummyLogger, main } from "@darthcav/ts-utils"
|
|
29
|
+
*
|
|
30
|
+
* main("test-app", getDummyLogger())
|
|
31
|
+
* ```
|
|
12
32
|
*/
|
|
13
|
-
export
|
|
33
|
+
export function getDummyLogger(): Logger {
|
|
14
34
|
const logger: Logger = {
|
|
15
35
|
category: [],
|
|
16
36
|
parent: null,
|
|
17
|
-
trace:
|
|
18
|
-
debug:
|
|
19
|
-
info:
|
|
20
|
-
warn:
|
|
21
|
-
warning:
|
|
22
|
-
error:
|
|
23
|
-
fatal:
|
|
37
|
+
trace: log,
|
|
38
|
+
debug: log,
|
|
39
|
+
info: log,
|
|
40
|
+
warn: log,
|
|
41
|
+
warning: log,
|
|
42
|
+
error: log,
|
|
43
|
+
fatal: log,
|
|
24
44
|
emit: (_record: Omit<LogRecord, "category">): void => {},
|
|
25
45
|
getChild: () => logger,
|
|
26
46
|
isEnabledFor: (_level: LogLevel): boolean => false,
|