@darthcav/ts-utils 0.8.2 → 0.8.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @darthcav/ts-utils
2
2
 
3
- ![Node][node-version] ![Version][version-image] ![CI][ci-badge] ![Coverage][coverage-badge]
3
+ [![Node][node-version]][node-url] ![Version][version-image] ![CI][ci-badge] [![Coverage][coverage-badge]][coverage-url]
4
4
 
5
5
  A collection of utility functions for TypeScript applications and modules.
6
6
 
@@ -174,7 +174,9 @@ public/ # Documentation output (generated)
174
174
  [Apache-2.0](LICENSE)
175
175
 
176
176
  [node-version]: https://img.shields.io/badge/node-%3E%3D25-orange.svg?style=flat-square
177
- [version-image]: https://img.shields.io/badge/version-0.8.2-blue.svg?style=flat-square
177
+ [node-url]: https://nodejs.org
178
+ [version-image]: https://img.shields.io/badge/version-0.8.4-blue.svg?style=flat-square
178
179
  [ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
179
- [coverage-badge]: https://img.shields.io/badge/coverage-check%20CI-yellow.svg?style=flat-square
180
+ [coverage-badge]: https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
181
+ [coverage-url]: https://codecov.io/github/darthcav/ts-utils
180
182
  [pages-url]: https://darthcav.github.io/ts-utils/
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=os-utils-nofile.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils-nofile.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/os-utils-nofile.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,16 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { mock, suite, test } from "node:test";
4
+ mock.module("node:fs", {
5
+ namedExports: {
6
+ existsSync: () => false,
7
+ readFileSync,
8
+ },
9
+ });
10
+ const { linuxRelease } = await import("../os-utils.js");
11
+ await suite("linuxRelease (file missing)", async () => {
12
+ test("returns null when /etc/os-release does not exist", () => {
13
+ assert.equal(linuxRelease(), null);
14
+ });
15
+ });
16
+ //# sourceMappingURL=os-utils-nofile.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils-nofile.test.js","sourceRoot":"","sources":["../../src/__tests__/os-utils-nofile.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAE7C,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;IACnB,YAAY,EAAE;QACV,UAAU,EAAE,GAAG,EAAE,CAAC,KAAK;QACvB,YAAY;KACf;CACJ,CAAC,CAAA;AAEF,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAEvD,MAAM,KAAK,CAAC,6BAA6B,EAAE,KAAK,IAAI,EAAE;IAClD,IAAI,CAAC,kDAAkD,EAAE,GAAG,EAAE;QAC1D,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=os-utils-windows.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils-windows.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/os-utils-windows.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,67 @@
1
+ import assert from "node:assert/strict";
2
+ import { mock, suite, test } from "node:test";
3
+ let mockRelease = "10.0.22000";
4
+ mock.module("node:os", {
5
+ namedExports: {
6
+ platform: () => "win32",
7
+ release: () => mockRelease,
8
+ arch: () => "x64",
9
+ },
10
+ });
11
+ const { linuxRelease, windowsRelease } = await import("../os-utils.js");
12
+ await suite("linuxRelease (non-linux platform)", async () => {
13
+ test("returns null on non-Linux platforms", () => {
14
+ assert.equal(linuxRelease(), null);
15
+ });
16
+ });
17
+ await suite("windowsRelease (win32 platform)", async () => {
18
+ test("Windows 11 (build >= 22000)", () => {
19
+ mockRelease = "10.0.22000";
20
+ const result = windowsRelease();
21
+ assert.ok(result !== null);
22
+ assert.equal(result.name, "Windows 11");
23
+ assert.equal(result.version, "10.0.22000");
24
+ assert.equal(result.arch, "x64");
25
+ });
26
+ test("Windows 10 (build < 22000)", () => {
27
+ mockRelease = "10.0.19041";
28
+ assert.equal(windowsRelease()?.name, "Windows 10");
29
+ });
30
+ test("Windows 8.1", () => {
31
+ mockRelease = "6.3.9600";
32
+ assert.equal(windowsRelease()?.name, "Windows 8.1");
33
+ });
34
+ test("Windows 8", () => {
35
+ mockRelease = "6.2.9200";
36
+ assert.equal(windowsRelease()?.name, "Windows 8");
37
+ });
38
+ test("Windows 7", () => {
39
+ mockRelease = "6.1.7601";
40
+ assert.equal(windowsRelease()?.name, "Windows 7");
41
+ });
42
+ test("Windows Vista", () => {
43
+ mockRelease = "6.0.6002";
44
+ assert.equal(windowsRelease()?.name, "Windows Vista");
45
+ });
46
+ test("Windows XP 64-Bit Edition", () => {
47
+ mockRelease = "5.2.3790";
48
+ assert.equal(windowsRelease()?.name, "Windows XP 64-Bit Edition");
49
+ });
50
+ test("Windows XP", () => {
51
+ mockRelease = "5.1.2600";
52
+ assert.equal(windowsRelease()?.name, "Windows XP");
53
+ });
54
+ test("Windows 2000", () => {
55
+ mockRelease = "5.0.2195";
56
+ assert.equal(windowsRelease()?.name, "Windows 2000");
57
+ });
58
+ test("unknown version", () => {
59
+ mockRelease = "4.0.1381";
60
+ assert.equal(windowsRelease()?.name, "Windows unknown version");
61
+ });
62
+ test("Windows 10 when build number is absent", () => {
63
+ mockRelease = "10.0";
64
+ assert.equal(windowsRelease()?.name, "Windows 10");
65
+ });
66
+ });
67
+ //# sourceMappingURL=os-utils-windows.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils-windows.test.js","sourceRoot":"","sources":["../../src/__tests__/os-utils-windows.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAE7C,IAAI,WAAW,GAAG,YAAY,CAAA;AAE9B,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;IACnB,YAAY,EAAE;QACV,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO;QACvB,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW;QAC1B,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK;KACpB;CACJ,CAAC,CAAA;AAEF,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAEvE,MAAM,KAAK,CAAC,mCAAmC,EAAE,KAAK,IAAI,EAAE;IACxD,IAAI,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,EAAE,IAAI,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA;AAEF,MAAM,KAAK,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;IACtD,IAAI,CAAC,6BAA6B,EAAE,GAAG,EAAE;QACrC,WAAW,GAAG,YAAY,CAAA;QAC1B,MAAM,MAAM,GAAG,cAAc,EAAE,CAAA;QAC/B,MAAM,CAAC,EAAE,CAAC,MAAM,KAAK,IAAI,CAAC,CAAA;QAC1B,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;QACvC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;QAC1C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAA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YAAY,CAAC,CAAA;IACtD,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,cAAc,EAAE,GAAG,EAAE;QACtB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,CAAA;IACxD,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,iBAAiB,EAAE,GAAG,EAAE;QACzB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE,yBAAyB,CAAC,CAAA;IACnE,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,WAAW,GAAG,MAAM,CAAA;QACpB,MAAM,CAAC,KAAK,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;IACtD,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=os-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/os-utils.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,85 @@
1
+ import assert from "node:assert/strict";
2
+ import { platform } from "node:os";
3
+ import { suite, test } from "node:test";
4
+ import { linuxRelease, windowsRelease } from "../os-utils.js";
5
+ await suite("linuxRelease", () => {
6
+ test("returns an object on Linux", () => {
7
+ if (platform() !== "linux") {
8
+ return;
9
+ }
10
+ const result = linuxRelease();
11
+ assert.notEqual(result, null);
12
+ assert.equal(typeof result, "object");
13
+ });
14
+ test("returns null on non-Linux platforms", () => {
15
+ if (platform() === "linux") {
16
+ return;
17
+ }
18
+ assert.equal(linuxRelease(), null);
19
+ });
20
+ test("parsed object has string values", () => {
21
+ if (platform() !== "linux") {
22
+ return;
23
+ }
24
+ const result = linuxRelease();
25
+ assert.ok(result !== null);
26
+ for (const value of Object.values(result)) {
27
+ assert.equal(typeof value, "string");
28
+ }
29
+ });
30
+ test("parsed object contains expected keys on Linux", () => {
31
+ if (platform() !== "linux") {
32
+ return;
33
+ }
34
+ const result = linuxRelease();
35
+ assert.ok(result !== null);
36
+ assert.ok("ID" in result);
37
+ assert.ok("NAME" in result);
38
+ });
39
+ test("values do not contain surrounding quotes", () => {
40
+ if (platform() !== "linux") {
41
+ return;
42
+ }
43
+ const result = linuxRelease();
44
+ assert.ok(result !== null);
45
+ for (const value of Object.values(result)) {
46
+ assert.ok(!value.startsWith('"'));
47
+ assert.ok(!value.endsWith('"'));
48
+ }
49
+ });
50
+ });
51
+ await suite("windowsRelease", () => {
52
+ test("returns an object on Windows", () => {
53
+ if (platform() !== "win32") {
54
+ return;
55
+ }
56
+ const result = windowsRelease();
57
+ assert.notEqual(result, null);
58
+ assert.equal(typeof result, "object");
59
+ });
60
+ test("returns null on non-Windows platforms", () => {
61
+ if (platform() === "win32") {
62
+ return;
63
+ }
64
+ assert.equal(windowsRelease(), null);
65
+ });
66
+ test("returned object has name, version, and arch on Windows", () => {
67
+ if (platform() !== "win32") {
68
+ return;
69
+ }
70
+ const result = windowsRelease();
71
+ assert.ok(result !== null);
72
+ assert.equal(typeof result.name, "string");
73
+ assert.equal(typeof result.version, "string");
74
+ assert.equal(typeof result.arch, "string");
75
+ });
76
+ test("name starts with 'Windows' on Windows", () => {
77
+ if (platform() !== "win32") {
78
+ return;
79
+ }
80
+ const result = windowsRelease();
81
+ assert.ok(result !== null);
82
+ assert.ok(result.name.startsWith("Windows "));
83
+ });
84
+ });
85
+ //# sourceMappingURL=os-utils.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils.test.js","sourceRoot":"","sources":["../../src/__tests__/os-utils.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,KAAK,EAAE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AC,cAAc,EAAE,EAAE,IAAI,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,wDAAwD,EAAE,GAAG,EAAE;QAChE,IAAI,QAAQ,EAAE,KAAK,OAAO,EAAE,CAAC;YACzB,OAAM;QACV,CAAC;QACD,MAAM,MAAM,GAAG,cAAc,EAAE,CAAA;QAC/B,MAAM,CAAC,EAAE,CAAC,MAAM,KAAK,IAAI,CAAC,CAAA;QAC1B,MAAM,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QAC1C,MAAM,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;QAC7C,MAAM,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;IAC9C,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,IAAI,QAAQ,EAAE,KAAK,OAAO,EAAE,CAAC;YACzB,OAAM;QACV,CAAC;QACD,MAAM,MAAM,GAAG,cAAc,EAAE,CAAA;QAC/B,MAAM,CAAC,EAAE,CAAC,MAAM,KAAK,IAAI,CAAC,CAAA;QAC1B,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAA;IACjD,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -9,5 +9,6 @@ import { type LauncherFunction, main } from "./main.ts";
9
9
  import millisecondsToString from "./millisecondsToString.ts";
10
10
  import monitorMemory from "./monitorMemory.ts";
11
11
  import noop from "./noop.ts";
12
- export { getConsoleLogger, getDummyLogger, type LauncherFunction, main, millisecondsToString, monitorMemory, noop, };
12
+ import { type LinuxRelease, linuxRelease, type WindowsRelease, windowsRelease } from "./os-utils.ts";
13
+ export { getConsoleLogger, getDummyLogger, type LauncherFunction, type LinuxRelease, linuxRelease, main, millisecondsToString, monitorMemory, noop, type WindowsRelease, windowsRelease, };
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;AAC5D,OAAO,cAAc,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAE,KAAK,gBAAgB,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAC5D,OAAO,aAAa,MAAM,oBAAoB,CAAA;AAC9C,OAAO,IAAI,MAAM,WAAW,CAAA;AAE5B,OAAO,EACH,gBAAgB,EAChB,cAAc,EACd,KAAK,gBAAgB,EACrB,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,GACP,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;AAC5D,OAAO,cAAc,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAE,KAAK,gBAAgB,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAC5D,OAAO,aAAa,MAAM,oBAAoB,CAAA;AAC9C,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EACH,KAAK,YAAY,EACjB,YAAY,EACZ,KAAK,cAAc,EACnB,cAAc,EACjB,MAAM,eAAe,CAAA;AAEtB,OAAO,EACH,gBAAgB,EAChB,cAAc,EACd,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,YAAY,EACZ,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,EACJ,KAAK,cAAc,EACnB,cAAc,GACjB,CAAA"}
package/dist/index.js CHANGED
@@ -9,5 +9,6 @@ import { main } from "./main.js";
9
9
  import millisecondsToString from "./millisecondsToString.js";
10
10
  import monitorMemory from "./monitorMemory.js";
11
11
  import noop from "./noop.js";
12
- export { getConsoleLogger, getDummyLogger, main, millisecondsToString, monitorMemory, noop, };
12
+ import { linuxRelease, windowsRelease, } from "./os-utils.js";
13
+ export { getConsoleLogger, getDummyLogger, linuxRelease, main, millisecondsToString, monitorMemory, noop, windowsRelease, };
13
14
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;AAC5D,OAAO,cAAc,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAyB,IAAI,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAC5D,OAAO,aAAa,MAAM,oBAAoB,CAAA;AAC9C,OAAO,IAAI,MAAM,WAAW,CAAA;AAE5B,OAAO,EACH,gBAAgB,EAChB,cAAc,EAEd,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,GACP,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;AAC5D,OAAO,cAAc,MAAM,6BAA6B,CAAA;AACxD,OAAO,EAAyB,IAAI,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAC5D,OAAO,aAAa,MAAM,oBAAoB,CAAA;AAC9C,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAEH,YAAY,EAEZ,cAAc,GACjB,MAAM,eAAe,CAAA;AAEtB,OAAO,EACH,gBAAgB,EAChB,cAAc,EAGd,YAAY,EACZ,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,EAEJ,cAAc,GACjB,CAAA"}
@@ -8,5 +8,5 @@ import type { Logger } from "@logtape/logtape";
8
8
  *
9
9
  * @returns A logger whose methods are all no-ops.
10
10
  */
11
- export default function getDummyLogger(): Logger;
11
+ export default function getDummyLogger(): Promise<Logger>;
12
12
  //# sourceMappingURL=getDummyLogger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getDummyLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,kBAAkB,CAAA;AAGnE;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,IAAI,MAAM,CAiB/C"}
1
+ {"version":3,"file":"getDummyLogger.d.ts","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAuB,MAAM,kBAAkB,CAAA;AAGnE;;;;;;;;GAQG;AACH,wBAA8B,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAiB9D"}
@@ -8,7 +8,7 @@ import noop from "../noop.js";
8
8
  *
9
9
  * @returns A logger whose methods are all no-ops.
10
10
  */
11
- export default function getDummyLogger() {
11
+ export default async function getDummyLogger() {
12
12
  const logger = {
13
13
  category: [],
14
14
  parent: null,
@@ -1 +1 @@
1
- {"version":3,"file":"getDummyLogger.js","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,YAAY,CAAA;AAE7B;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc;IAClC,MAAM,MAAM,GAAW;QACnB,QAAQ,EAAE,EAAE;QACZ,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,IAAkC;QACzC,KAAK,EAAE,IAAkC;QACzC,IAAI,EAAE,IAAiC;QACvC,IAAI,EAAE,IAAiC;QACvC,OAAO,EAAE,IAAoC;QAC7C,KAAK,EAAE,IAAkC;QACzC,KAAK,EAAE,IAAkC;QACzC,IAAI,EAAE,CAAC,OAAoC,EAAQ,EAAE,GAAE,CAAC;QACxD,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM;QACtB,YAAY,EAAE,CAAC,MAAgB,EAAW,EAAE,CAAC,KAAK;QAClD,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM;KACrB,CAAA;IACD,OAAO,MAAM,CAAA;AACjB,CAAC"}
1
+ {"version":3,"file":"getDummyLogger.js","sourceRoot":"","sources":["../../src/loggers/getDummyLogger.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,YAAY,CAAA;AAE7B;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,cAAc;IACxC,MAAM,MAAM,GAAW;QACnB,QAAQ,EAAE,EAAE;QACZ,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,IAAkC;QACzC,KAAK,EAAE,IAAkC;QACzC,IAAI,EAAE,IAAiC;QACvC,IAAI,EAAE,IAAiC;QACvC,OAAO,EAAE,IAAoC;QAC7C,KAAK,EAAE,IAAkC;QACzC,KAAK,EAAE,IAAkC;QACzC,IAAI,EAAE,CAAC,OAAoC,EAAQ,EAAE,GAAE,CAAC;QACxD,QAAQ,EAAE,GAAG,EAAE,CAAC,MAAM;QACtB,YAAY,EAAE,CAAC,MAAgB,EAAW,EAAE,CAAC,KAAK;QAClD,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM;KACrB,CAAA;IACD,OAAO,MAAM,CAAA;AACjB,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Parsed key-value pairs from `/etc/os-release`.
3
+ *
4
+ * Common keys include `ID`, `NAME`, `VERSION_ID`, and `PRETTY_NAME`.
5
+ * See {@link https://www.freedesktop.org/software/systemd/man/os-release.html | os-release(5)} for the full spec.
6
+ */
7
+ export interface LinuxRelease {
8
+ [key: string]: string;
9
+ }
10
+ /**
11
+ * Basic information about the current Windows environment.
12
+ */
13
+ export interface WindowsRelease {
14
+ /** Human-readable Windows version name, e.g. `"Windows 11"`. */
15
+ name: string;
16
+ /** NT kernel version string as returned by `os.release()`, e.g. `"10.0.22000"`. */
17
+ version: string;
18
+ /** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
19
+ arch: string;
20
+ }
21
+ /**
22
+ * Returns parsed key-value pairs from `/etc/os-release` on Linux, or `null` on other platforms
23
+ * or when the file is absent.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { linuxRelease } from "@darthcav/ts-utils"
28
+ *
29
+ * const info = linuxRelease()
30
+ * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS"
31
+ * ```
32
+ */
33
+ export declare function linuxRelease(): LinuxRelease | null;
34
+ /**
35
+ * Returns basic Windows version information, or `null` on non-Windows platforms.
36
+ *
37
+ * Distinguishes Windows 11 from Windows 10 by NT build number (>= 22000 → Windows 11).
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { windowsRelease } from "@darthcav/ts-utils"
42
+ *
43
+ * const info = windowsRelease()
44
+ * if (info) console.log(info.name) // e.g. "Windows 11"
45
+ * ```
46
+ */
47
+ export declare function windowsRelease(): WindowsRelease | null;
48
+ //# sourceMappingURL=os-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils.d.ts","sourceRoot":"","sources":["../src/os-utils.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,WAAW,YAAY;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,mFAAmF;IACnF,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;CACf;AAID;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,IAAI,YAAY,GAAG,IAAI,CAkBlD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,IAAI,cAAc,GAAG,IAAI,CAyCtD"}
@@ -0,0 +1,91 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { arch, release as osRelease, platform } from "node:os";
3
+ const OS_RELEASE = "/etc/os-release";
4
+ /**
5
+ * Returns parsed key-value pairs from `/etc/os-release` on Linux, or `null` on other platforms
6
+ * or when the file is absent.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { linuxRelease } from "@darthcav/ts-utils"
11
+ *
12
+ * const info = linuxRelease()
13
+ * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS"
14
+ * ```
15
+ */
16
+ export function linuxRelease() {
17
+ if (platform() !== "linux" || !existsSync(OS_RELEASE)) {
18
+ return null;
19
+ }
20
+ const result = {};
21
+ for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
22
+ const eq = line.indexOf("=");
23
+ if (eq === -1) {
24
+ continue;
25
+ }
26
+ const key = line.substring(0, eq).trim();
27
+ const value = line
28
+ .substring(eq + 1)
29
+ .trim()
30
+ .replace(/^"|"$/g, "");
31
+ result[key] = value;
32
+ }
33
+ return result;
34
+ }
35
+ /**
36
+ * Returns basic Windows version information, or `null` on non-Windows platforms.
37
+ *
38
+ * Distinguishes Windows 11 from Windows 10 by NT build number (>= 22000 → Windows 11).
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * import { windowsRelease } from "@darthcav/ts-utils"
43
+ *
44
+ * const info = windowsRelease()
45
+ * if (info) console.log(info.name) // e.g. "Windows 11"
46
+ * ```
47
+ */
48
+ export function windowsRelease() {
49
+ if (platform() !== "win32") {
50
+ return null;
51
+ }
52
+ const version = osRelease();
53
+ const [major, , build] = version.split(".").map(Number);
54
+ let name = "Windows ";
55
+ if (major === 10 && (build ?? 0) >= 22000) {
56
+ name += "11";
57
+ }
58
+ else {
59
+ switch (version.substring(0, 3)) {
60
+ case "10.":
61
+ name += "10";
62
+ break;
63
+ case "6.3":
64
+ name += "8.1";
65
+ break;
66
+ case "6.2":
67
+ name += "8";
68
+ break;
69
+ case "6.1":
70
+ name += "7";
71
+ break;
72
+ case "6.0":
73
+ name += "Vista";
74
+ break;
75
+ case "5.2":
76
+ name += "XP 64-Bit Edition";
77
+ break;
78
+ case "5.1":
79
+ name += "XP";
80
+ break;
81
+ case "5.0":
82
+ name += "2000";
83
+ break;
84
+ default:
85
+ name += "unknown version";
86
+ break;
87
+ }
88
+ }
89
+ return { name, version, arch: arch() };
90
+ }
91
+ //# sourceMappingURL=os-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"os-utils.js","sourceRoot":"","sources":["../src/os-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,OAAO,IAAI,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAwB9D,MAAM,UAAU,GAAG,iBAAiB,CAAA;AAEpC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY;IACxB,IAAI,QAAQ,EAAE,KAAK,OAAO,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QACpD,OAAO,IAAI,CAAA;IACf,CAAC;IACD,MAAM,MAAM,GAAiB,EAAE,CAAA;IAC/B,KAAK,MAAM,IAAI,IAAI,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/D,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC5B,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;YACZ,SAAQ;QACZ,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;QACxC,MAAM,KAAK,GAAG,IAAI;aACb,SAAS,CAAC,EAAE,GAAG,CAAC,CAAC;aACjB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AAA;gBACzB,MAAK;QACb,CAAC;IACL,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAA;AAC1C,CAAC"}
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@darthcav/ts-utils",
3
3
  "description": "A collection of utility functions for TypeScript applications and modules",
4
- "version": "0.8.2",
4
+ "version": "0.8.4",
5
5
  "author": {
6
- "name": "darthcav"
6
+ "name": "darthcav",
7
+ "url": "https://github.com/darthcav"
7
8
  },
8
9
  "license": "Apache-2.0",
9
10
  "type": "module",
@@ -17,21 +18,16 @@
17
18
  }
18
19
  },
19
20
  "files": [
20
- "package.json",
21
- "README.md",
22
- "LICENSE",
23
- "src/**/*.ts",
24
- "src/**/*.tsx",
25
- "dist/**/*.js",
26
- "dist/**/*.js.map",
27
- "dist/**/*.d.ts",
28
- "dist/**/*.d.ts.map"
21
+ "src",
22
+ "!src/**/*.test.ts",
23
+ "!src/__tests__",
24
+ "dist"
29
25
  ],
30
26
  "dependencies": {
31
27
  "@logtape/logtape": "2.0.4"
32
28
  },
33
29
  "devDependencies": {
34
- "@biomejs/biome": "2.4.7",
30
+ "@biomejs/biome": "2.4.8",
35
31
  "@types/node": "25.5.0",
36
32
  "asserttt": "1.0.1",
37
33
  "shx": "0.4.0",
@@ -52,8 +48,9 @@
52
48
  "\n===== Typecheck =====": "",
53
49
  "typecheck": "tsc --noEmit",
54
50
  "\n===== Test =====": "",
55
- "test": "node --env-file=.env.local --test --test-reporter=spec 'src/**/*.test.ts'",
56
- "test:coverage": "node --env-file=.env.local --experimental-test-coverage --test 'src/**/*.test.ts'",
51
+ "test": "node --env-file=.env.local --experimental-test-module-mocks --test --test-reporter=spec 'src/**/*.test.ts'",
52
+ "test:coverage": "node --env-file=.env.local --experimental-test-module-mocks --experimental-test-coverage --test 'src/**/*.test.ts'",
53
+ "test:coverage:lcov": "shx mkdir -p coverage && node --env-file=.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'",
57
54
  "\n===== Linter =====": "",
58
55
  "lint": "biome check .",
59
56
  "lint:fix": "biome check --write .",
package/src/index.ts CHANGED
@@ -10,13 +10,23 @@ import { type LauncherFunction, main } from "./main.ts"
10
10
  import millisecondsToString from "./millisecondsToString.ts"
11
11
  import monitorMemory from "./monitorMemory.ts"
12
12
  import noop from "./noop.ts"
13
+ import {
14
+ type LinuxRelease,
15
+ linuxRelease,
16
+ type WindowsRelease,
17
+ windowsRelease,
18
+ } from "./os-utils.ts"
13
19
 
14
20
  export {
15
21
  getConsoleLogger,
16
22
  getDummyLogger,
17
23
  type LauncherFunction,
24
+ type LinuxRelease,
25
+ linuxRelease,
18
26
  main,
19
27
  millisecondsToString,
20
28
  monitorMemory,
21
29
  noop,
30
+ type WindowsRelease,
31
+ windowsRelease,
22
32
  }
@@ -10,7 +10,7 @@ import noop from "../noop.ts"
10
10
  *
11
11
  * @returns A logger whose methods are all no-ops.
12
12
  */
13
- export default function getDummyLogger(): Logger {
13
+ export default async function getDummyLogger(): Promise<Logger> {
14
14
  const logger: Logger = {
15
15
  category: [],
16
16
  parent: null,
@@ -0,0 +1,114 @@
1
+ import { existsSync, readFileSync } from "node:fs"
2
+ import { arch, release as osRelease, platform } from "node:os"
3
+
4
+ /**
5
+ * Parsed key-value pairs from `/etc/os-release`.
6
+ *
7
+ * Common keys include `ID`, `NAME`, `VERSION_ID`, and `PRETTY_NAME`.
8
+ * See {@link https://www.freedesktop.org/software/systemd/man/os-release.html | os-release(5)} for the full spec.
9
+ */
10
+ export interface LinuxRelease {
11
+ [key: string]: string
12
+ }
13
+
14
+ /**
15
+ * Basic information about the current Windows environment.
16
+ */
17
+ export interface WindowsRelease {
18
+ /** Human-readable Windows version name, e.g. `"Windows 11"`. */
19
+ name: string
20
+ /** NT kernel version string as returned by `os.release()`, e.g. `"10.0.22000"`. */
21
+ version: string
22
+ /** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
23
+ arch: string
24
+ }
25
+
26
+ const OS_RELEASE = "/etc/os-release"
27
+
28
+ /**
29
+ * Returns parsed key-value pairs from `/etc/os-release` on Linux, or `null` on other platforms
30
+ * or when the file is absent.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { linuxRelease } from "@darthcav/ts-utils"
35
+ *
36
+ * const info = linuxRelease()
37
+ * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS"
38
+ * ```
39
+ */
40
+ export function linuxRelease(): LinuxRelease | null {
41
+ if (platform() !== "linux" || !existsSync(OS_RELEASE)) {
42
+ return null
43
+ }
44
+ const result: LinuxRelease = {}
45
+ for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
46
+ const eq = line.indexOf("=")
47
+ if (eq === -1) {
48
+ continue
49
+ }
50
+ const key = line.substring(0, eq).trim()
51
+ const value = line
52
+ .substring(eq + 1)
53
+ .trim()
54
+ .replace(/^"|"$/g, "")
55
+ result[key] = value
56
+ }
57
+ return result
58
+ }
59
+
60
+ /**
61
+ * Returns basic Windows version information, or `null` on non-Windows platforms.
62
+ *
63
+ * Distinguishes Windows 11 from Windows 10 by NT build number (>= 22000 → Windows 11).
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { windowsRelease } from "@darthcav/ts-utils"
68
+ *
69
+ * const info = windowsRelease()
70
+ * if (info) console.log(info.name) // e.g. "Windows 11"
71
+ * ```
72
+ */
73
+ export function windowsRelease(): WindowsRelease | null {
74
+ if (platform() !== "win32") {
75
+ return null
76
+ }
77
+ const version = osRelease()
78
+ const [major, , build] = version.split(".").map(Number)
79
+ let name = "Windows "
80
+ if (major === 10 && (build ?? 0) >= 22000) {
81
+ name += "11"
82
+ } else {
83
+ switch (version.substring(0, 3)) {
84
+ case "10.":
85
+ name += "10"
86
+ break
87
+ case "6.3":
88
+ name += "8.1"
89
+ break
90
+ case "6.2":
91
+ name += "8"
92
+ break
93
+ case "6.1":
94
+ name += "7"
95
+ break
96
+ case "6.0":
97
+ name += "Vista"
98
+ break
99
+ case "5.2":
100
+ name += "XP 64-Bit Edition"
101
+ break
102
+ case "5.1":
103
+ name += "XP"
104
+ break
105
+ case "5.0":
106
+ name += "2000"
107
+ break
108
+ default:
109
+ name += "unknown version"
110
+ break
111
+ }
112
+ }
113
+ return { name, version, arch: arch() }
114
+ }
@@ -1,369 +0,0 @@
1
- import assert from "node:assert/strict"
2
- import process from "node:process"
3
- import { afterEach, beforeEach, mock, suite, test } from "node:test"
4
- import type { Logger } from "@logtape/logtape"
5
- import { main } from "../main.ts"
6
-
7
- await suite("main", () => {
8
- const logMock = mock.fn()
9
- const onMock = mock.fn()
10
- const onceMock = mock.fn()
11
- const exitMock = mock.fn()
12
- const setIntervalMock = mock.fn()
13
-
14
- const childLogger = { info: logMock, error: logMock } as unknown as Logger
15
- const logger = { getChild: () => childLogger } as unknown as Logger
16
-
17
- beforeEach(() => {
18
- logMock.mock.resetCalls()
19
- onMock.mock.resetCalls()
20
- onceMock.mock.resetCalls()
21
- exitMock.mock.resetCalls()
22
- setIntervalMock.mock.resetCalls()
23
- mock.method(process, "on", onMock)
24
- mock.method(process, "once", onceMock)
25
- mock.method(process, "exit", exitMock)
26
- mock.method(globalThis, "setInterval", setIntervalMock)
27
- })
28
-
29
- afterEach(() => {
30
- mock.restoreAll()
31
- })
32
-
33
- test("should log startup information", () => {
34
- main("test-app", logger)
35
-
36
- const messages = logMock.mock.calls.map((c) => c.arguments[0])
37
- assert.ok(messages.some((m) => /Main process launched/.test(m)))
38
- assert.ok(messages.some((m) => /Process name:/.test(m)))
39
- assert.ok(messages.some((m) => /Node\.js environment:/.test(m)))
40
- assert.ok(messages.some((m) => /Node\.js process options:/.test(m)))
41
- })
42
-
43
- test("should use empty fallbacks when NODE_ENV and NODE_OPTIONS are not set", () => {
44
- const savedEnv = process.env["NODE_ENV"]
45
- const savedOptions = process.env["NODE_OPTIONS"]
46
- delete process.env["NODE_ENV"]
47
- delete process.env["NODE_OPTIONS"]
48
- try {
49
- main("test-app", logger)
50
- const messages = logMock.mock.calls.map((c) => c.arguments[0])
51
- assert.ok(messages.some((m) => /Node\.js environment: $/.test(m)))
52
- assert.ok(messages.some((m) => /Node\.js process options:/.test(m)))
53
- } finally {
54
- if (savedEnv !== undefined) {
55
- process.env["NODE_ENV"] = savedEnv
56
- }
57
- if (savedOptions !== undefined) {
58
- process.env["NODE_OPTIONS"] = savedOptions
59
- }
60
- }
61
- })
62
-
63
- test("should register SIGINT handler", () => {
64
- main("test-app", logger)
65
-
66
- const sigintCall = onMock.mock.calls.find(
67
- (c) => c.arguments[0] === "SIGINT",
68
- )
69
- assert.ok(sigintCall, "SIGINT handler not registered")
70
- })
71
-
72
- test("should register SIGTERM handler", () => {
73
- main("test-app", logger)
74
-
75
- const sigtermCall = onMock.mock.calls.find(
76
- (c) => c.arguments[0] === "SIGTERM",
77
- )
78
- assert.ok(sigtermCall, "SIGTERM handler not registered")
79
- })
80
-
81
- test("should not register SIGINT or SIGTERM handlers when defaultInterruptionHandler is false", () => {
82
- main("test-app", logger, false)
83
-
84
- const sigintCall = onMock.mock.calls.find(
85
- (c) => c.arguments[0] === "SIGINT",
86
- )
87
- const sigtermCall = onMock.mock.calls.find(
88
- (c) => c.arguments[0] === "SIGTERM",
89
- )
90
- assert.equal(
91
- sigintCall,
92
- undefined,
93
- "SIGINT handler should not be registered",
94
- )
95
- assert.equal(
96
- sigtermCall,
97
- undefined,
98
- "SIGTERM handler should not be registered",
99
- )
100
- })
101
-
102
- test("should register uncaughtException handler", () => {
103
- main("test-app", logger)
104
-
105
- const call = onMock.mock.calls.find(
106
- (c) => c.arguments[0] === "uncaughtException",
107
- )
108
- assert.ok(call, "uncaughtException handler not registered")
109
- })
110
-
111
- test("should register unhandledRejection handler", () => {
112
- main("test-app", logger)
113
-
114
- const call = onMock.mock.calls.find(
115
- (c) => c.arguments[0] === "unhandledRejection",
116
- )
117
- assert.ok(call, "unhandledRejection handler not registered")
118
- })
119
-
120
- test("should exit on SIGINT", () => {
121
- main("test-app", logger)
122
-
123
- const sigintCall = onMock.mock.calls.find(
124
- (c) => c.arguments[0] === "SIGINT",
125
- )
126
- assert.ok(sigintCall)
127
- const handler = sigintCall.arguments[1]
128
- handler("SIGINT")
129
-
130
- assert.ok(
131
- logMock.mock.calls.some((c) =>
132
- /Received signal: SIGINT/.test(c.arguments[0]),
133
- ),
134
- )
135
- assert.equal(exitMock.mock.callCount(), 1)
136
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 0)
137
- })
138
-
139
- test("should exit on SIGTERM", () => {
140
- main("test-app", logger)
141
-
142
- const sigtermCall = onMock.mock.calls.find(
143
- (c) => c.arguments[0] === "SIGTERM",
144
- )
145
- assert.ok(sigtermCall)
146
- const handler = sigtermCall.arguments[1]
147
- handler("SIGTERM")
148
-
149
- assert.ok(
150
- logMock.mock.calls.some((c) =>
151
- /Received signal: SIGTERM/.test(c.arguments[0]),
152
- ),
153
- )
154
- assert.equal(exitMock.mock.callCount(), 1)
155
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 0)
156
- })
157
-
158
- test("should exit on uncaughtException with an Error and log its stack", () => {
159
- main("test-app", logger)
160
-
161
- const call = onMock.mock.calls.find(
162
- (c) => c.arguments[0] === "uncaughtException",
163
- )
164
- assert.ok(call)
165
- const handler = call.arguments[1]
166
- const error = new Error("test error")
167
- handler(error, "unhandledException")
168
-
169
- assert.ok(
170
- logMock.mock.calls.some(
171
- (c) =>
172
- /Uncaught exception:/.test(c.arguments[0]) &&
173
- c.arguments[0].includes(error.stack ?? error.message),
174
- ),
175
- )
176
- assert.ok(
177
- logMock.mock.calls.some((c) =>
178
- /Exception origin:/.test(c.arguments[0]),
179
- ),
180
- )
181
- assert.equal(exitMock.mock.callCount(), 1)
182
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
183
- })
184
-
185
- test("should exit on uncaughtException with an Error and no stack", () => {
186
- main("test-app", logger)
187
-
188
- const call = onMock.mock.calls.find(
189
- (c) => c.arguments[0] === "uncaughtException",
190
- )
191
- assert.ok(call)
192
- const handler = call.arguments[1]
193
- const error = new Error("test error")
194
- delete error.stack
195
- handler(error, "unhandledException")
196
-
197
- assert.ok(
198
- logMock.mock.calls.some((c) =>
199
- /Uncaught exception: Error: test error/.test(c.arguments[0]),
200
- ),
201
- )
202
- assert.equal(exitMock.mock.callCount(), 1)
203
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
204
- })
205
-
206
- test("should exit on uncaughtException with a non-Error value", () => {
207
- main("test-app", logger)
208
-
209
- const call = onMock.mock.calls.find(
210
- (c) => c.arguments[0] === "uncaughtException",
211
- )
212
- assert.ok(call)
213
- const handler = call.arguments[1]
214
- handler("something went wrong", "unhandledException")
215
-
216
- assert.ok(
217
- logMock.mock.calls.some((c) =>
218
- /Uncaught exception: something went wrong/.test(c.arguments[0]),
219
- ),
220
- )
221
- assert.equal(exitMock.mock.callCount(), 1)
222
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
223
- })
224
-
225
- test("should invoke the launcher function when provided", () => {
226
- const launcher = mock.fn()
227
- main("test-app", logger, launcher)
228
- assert.equal(launcher.mock.callCount(), 1)
229
- assert.equal(launcher.mock.calls[0]?.arguments.length, 0)
230
- })
231
-
232
- test("should exit on unhandledRejection with an Error reason and no stack", () => {
233
- main("test-app", logger)
234
-
235
- const call = onMock.mock.calls.find(
236
- (c) => c.arguments[0] === "unhandledRejection",
237
- )
238
- assert.ok(call)
239
- const handler = call.arguments[1]
240
- const error = new Error("rejection error")
241
- delete error.stack
242
- handler(error)
243
-
244
- assert.ok(
245
- logMock.mock.calls.some((c) =>
246
- /Unhandled promise rejection\. Reason:[\s\S]*Error: rejection error/.test(
247
- c.arguments[0],
248
- ),
249
- ),
250
- )
251
- assert.equal(exitMock.mock.callCount(), 1)
252
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
253
- })
254
-
255
- test("should exit on unhandledRejection with a non-Error reason", () => {
256
- main("test-app", logger)
257
-
258
- const call = onMock.mock.calls.find(
259
- (c) => c.arguments[0] === "unhandledRejection",
260
- )
261
- assert.ok(call)
262
- const handler = call.arguments[1]
263
- handler("some reason")
264
-
265
- assert.ok(
266
- logMock.mock.calls.some((c) =>
267
- /Unhandled promise rejection\. Reason:/.test(c.arguments[0]),
268
- ),
269
- )
270
- assert.equal(exitMock.mock.callCount(), 1)
271
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
272
- })
273
-
274
- test("should exit on unhandledRejection with an Error reason and log its stack", () => {
275
- main("test-app", logger)
276
-
277
- const call = onMock.mock.calls.find(
278
- (c) => c.arguments[0] === "unhandledRejection",
279
- )
280
- assert.ok(call)
281
- const handler = call.arguments[1]
282
- const error = new Error("rejection error")
283
- handler(error)
284
-
285
- assert.ok(
286
- logMock.mock.calls.some(
287
- (c) =>
288
- /Unhandled promise rejection\. Reason:/.test(
289
- c.arguments[0],
290
- ) && c.arguments[0].includes(error.stack ?? error.message),
291
- ),
292
- )
293
- assert.equal(exitMock.mock.callCount(), 1)
294
- assert.equal(exitMock.mock.calls[0]?.arguments[0], 1)
295
- })
296
-
297
- // monitorMemoryHours overload combinations
298
-
299
- test("should not start memory monitoring when monitorMemoryHours is 0 (default)", () => {
300
- main("test-app", logger)
301
- assert.equal(setIntervalMock.mock.callCount(), 0)
302
- })
303
-
304
- test("should start memory monitoring with only monitorMemoryHours", () => {
305
- main("test-app", logger, 2)
306
- assert.equal(setIntervalMock.mock.callCount(), 1)
307
- assert.equal(
308
- setIntervalMock.mock.calls[0]?.arguments[1],
309
- 2 * 60 * 60 * 1_000,
310
- )
311
- })
312
-
313
- test("should start memory monitoring with launcher and monitorMemoryHours", () => {
314
- main("test-app", logger, mock.fn(), 2)
315
- assert.equal(setIntervalMock.mock.callCount(), 1)
316
- assert.equal(
317
- setIntervalMock.mock.calls[0]?.arguments[1],
318
- 2 * 60 * 60 * 1_000,
319
- )
320
- })
321
-
322
- test("should start memory monitoring with monitorMemoryHours and defaultInterruptionHandler", () => {
323
- main("test-app", logger, 3, false)
324
- assert.equal(setIntervalMock.mock.callCount(), 1)
325
- assert.equal(
326
- setIntervalMock.mock.calls[0]?.arguments[1],
327
- 3 * 60 * 60 * 1_000,
328
- )
329
- assert.equal(
330
- onMock.mock.calls.find((c) => c.arguments[0] === "SIGINT"),
331
- undefined,
332
- )
333
- })
334
-
335
- test("should start memory monitoring with all three optional parameters", () => {
336
- const launcher = mock.fn()
337
- main("test-app", logger, launcher, 4, true)
338
- assert.equal(setIntervalMock.mock.callCount(), 1)
339
- assert.equal(
340
- setIntervalMock.mock.calls[0]?.arguments[1],
341
- 4 * 60 * 60 * 1_000,
342
- )
343
- assert.equal(launcher.mock.callCount(), 1)
344
- })
345
-
346
- // defaultInterruptionHandler overload combinations
347
-
348
- test("should disable interruption handler with only defaultInterruptionHandler=false", () => {
349
- main("test-app", logger, false)
350
- assert.equal(
351
- onMock.mock.calls.find((c) => c.arguments[0] === "SIGINT"),
352
- undefined,
353
- )
354
- assert.equal(
355
- onMock.mock.calls.find((c) => c.arguments[0] === "SIGTERM"),
356
- undefined,
357
- )
358
- })
359
-
360
- test("should disable interruption handler with launcher and defaultInterruptionHandler=false", () => {
361
- const launcher = mock.fn()
362
- main("test-app", logger, launcher, false)
363
- assert.equal(
364
- onMock.mock.calls.find((c) => c.arguments[0] === "SIGINT"),
365
- undefined,
366
- )
367
- assert.equal(launcher.mock.callCount(), 1)
368
- })
369
- })
@@ -1,37 +0,0 @@
1
- import assert from "node:assert/strict"
2
- import { suite, test } from "node:test"
3
- import millisecondsToString from "../millisecondsToString.ts"
4
-
5
- await suite("millisecondsToString", () => {
6
- test("seconds only", () => {
7
- assert.equal(millisecondsToString(5_000), "5s")
8
- })
9
-
10
- test("zero milliseconds", () => {
11
- assert.equal(millisecondsToString(0), "0s")
12
- })
13
-
14
- test("sub-second values are rounded to nearest second", () => {
15
- assert.equal(millisecondsToString(499), "0s")
16
- assert.equal(millisecondsToString(500), "1s")
17
- assert.equal(millisecondsToString(1_499), "1s")
18
- assert.equal(millisecondsToString(1_500), "2s")
19
- })
20
-
21
- test("minutes and seconds", () => {
22
- assert.equal(millisecondsToString(90_000), "1m 30s")
23
- })
24
-
25
- test("hours, minutes, and seconds", () => {
26
- assert.equal(millisecondsToString(3_661_000), "1h 1m 1s")
27
- })
28
-
29
- test("days, hours, minutes, and seconds", () => {
30
- assert.equal(millisecondsToString(90_061_000), "1d 1h 1m 1s")
31
- })
32
-
33
- test("leading zero components are omitted", () => {
34
- assert.equal(millisecondsToString(3_600_000), "1h 0s")
35
- assert.equal(millisecondsToString(86_400_000), "1d 0s")
36
- })
37
- })
@@ -1,47 +0,0 @@
1
- import assert from "node:assert/strict"
2
- import { afterEach, beforeEach, mock, suite, test } from "node:test"
3
- import type { Logger } from "@logtape/logtape"
4
- import monitorMemory from "../monitorMemory.ts"
5
-
6
- await suite("monitorMemory", () => {
7
- const logMock = mock.fn()
8
- const logger = { info: logMock } as unknown as Logger
9
-
10
- let setIntervalMock: ReturnType<typeof mock.method>
11
-
12
- beforeEach(() => {
13
- logMock.mock.resetCalls()
14
- setIntervalMock = mock.method(
15
- globalThis,
16
- "setInterval",
17
- (fn: () => void, _delay: number) => {
18
- fn()
19
- return 0
20
- },
21
- )
22
- })
23
-
24
- afterEach(() => {
25
- mock.restoreAll()
26
- })
27
-
28
- test("defaults to 24-hour interval", () => {
29
- monitorMemory(logger)
30
- const delay = setIntervalMock.mock.calls[0]?.arguments[1]
31
- assert.equal(delay, 24 * 60 * 60 * 1_000)
32
- })
33
-
34
- test("uses the provided hours parameter", () => {
35
- monitorMemory(logger, 1)
36
- const delay = setIntervalMock.mock.calls[0]?.arguments[1]
37
- assert.equal(delay, 1 * 60 * 60 * 1_000)
38
- })
39
-
40
- test("logs uptime and memory info on each tick", () => {
41
- monitorMemory(logger)
42
- assert.equal(logMock.mock.calls.length, 2)
43
- const messages = logMock.mock.calls.map((c) => String(c.arguments[0]))
44
- assert.match(messages[0] ?? "", /Process uptime:/)
45
- assert.match(messages[1] ?? "", /Process memory/)
46
- })
47
- })
@@ -1,13 +0,0 @@
1
- import assert from "node:assert/strict"
2
- import { suite, test } from "node:test"
3
- import noop from "../noop.ts"
4
-
5
- await suite("noop", () => {
6
- test("returns undefined", () => {
7
- assert.equal(noop(), undefined)
8
- })
9
-
10
- test("is a function", () => {
11
- assert.equal(typeof noop, "function")
12
- })
13
- })