@darthcav/ts-utils 0.8.7 → 0.9.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 +52 -21
- package/dist/__tests__/os-utils-nofile.test.js +3 -3
- package/dist/__tests__/os-utils-nofile.test.js.map +1 -1
- package/dist/__tests__/os-utils-windows.test.js +13 -18
- package/dist/__tests__/os-utils-windows.test.js.map +1 -1
- package/dist/__tests__/os-utils.test.js +27 -32
- package/dist/__tests__/os-utils.test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/os-utils.d.ts +20 -31
- package/dist/os-utils.d.ts.map +1 -1
- package/dist/os-utils.js +75 -75
- package/dist/os-utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -10
- package/src/os-utils.ts +82 -89
package/README.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# @darthcav/ts-utils
|
|
2
2
|
|
|
3
|
-
[![Node][node-version]][node-url] ![Version][version-image] ![CI][ci-badge]
|
|
3
|
+
[![Node][node-version]][node-url] ![Version][version-image] ![CI][ci-badge]
|
|
4
|
+
[![Coverage][coverage-badge]][coverage-url]
|
|
4
5
|
|
|
5
6
|
A collection of utility functions for TypeScript applications and modules.
|
|
6
7
|
|
|
8
|
+
## Documentation
|
|
9
|
+
|
|
7
10
|
[API Documentation][pages-url]
|
|
8
11
|
|
|
9
12
|
## Features
|
|
@@ -25,8 +28,9 @@ npm install @darthcav/ts-utils
|
|
|
25
28
|
|
|
26
29
|
### `getConsoleLogger`
|
|
27
30
|
|
|
28
|
-
Configures logging and returns a `Logger` for the given category name. Records at or above
|
|
29
|
-
the console using an ANSI color formatter with RFC 3339 timestamps. The
|
|
31
|
+
Configures logging and returns a `Logger` for the given category name. Records at or above
|
|
32
|
+
`lowestLevel` are written to the console using an ANSI color formatter with RFC 3339 timestamps. The
|
|
33
|
+
internal `logtape/meta` logger is silenced.
|
|
30
34
|
|
|
31
35
|
```ts
|
|
32
36
|
import { getConsoleLogger, main } from "@darthcav/ts-utils"
|
|
@@ -47,8 +51,8 @@ const logger = await getConsoleLogger("my-app", "debug")
|
|
|
47
51
|
|
|
48
52
|
### `monitorMemory`
|
|
49
53
|
|
|
50
|
-
Starts a periodic interval that logs process uptime and memory usage (in bytes). The interval
|
|
51
|
-
hours.
|
|
54
|
+
Starts a periodic interval that logs process uptime and memory usage (in bytes). The interval
|
|
55
|
+
defaults to every 24 hours.
|
|
52
56
|
|
|
53
57
|
```ts
|
|
54
58
|
import { getConsoleLogger, main, monitorMemory } from "@darthcav/ts-utils"
|
|
@@ -63,9 +67,10 @@ main("my-app", logger, () => {
|
|
|
63
67
|
|
|
64
68
|
### `millisecondsToString`
|
|
65
69
|
|
|
66
|
-
Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`. Sub-second
|
|
67
|
-
to the nearest second, and any zero-valued components are omitted — so a
|
|
68
|
-
An optional BCP 47 locale tag (default `"en"`)
|
|
70
|
+
Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`. Sub-second
|
|
71
|
+
values are rounded to the nearest second, and any zero-valued components are omitted — so a
|
|
72
|
+
zero-millisecond input returns an empty string. An optional BCP 47 locale tag (default `"en"`)
|
|
73
|
+
controls the unit labels.
|
|
69
74
|
|
|
70
75
|
```ts
|
|
71
76
|
import { millisecondsToString } from "@darthcav/ts-utils"
|
|
@@ -78,7 +83,8 @@ millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
|
|
|
78
83
|
|
|
79
84
|
### `noop`
|
|
80
85
|
|
|
81
|
-
A no-op function that does nothing and returns `void`. Useful as a placeholder callback or default
|
|
86
|
+
A no-op function that does nothing and returns `void`. Useful as a placeholder callback or default
|
|
87
|
+
handler.
|
|
82
88
|
|
|
83
89
|
```ts
|
|
84
90
|
import { noop } from "@darthcav/ts-utils"
|
|
@@ -89,8 +95,8 @@ element.addEventListener("click", noop)
|
|
|
89
95
|
|
|
90
96
|
### `RuntimeObject`
|
|
91
97
|
|
|
92
|
-
Represents an object whose keys and values are only known at runtime. It is defined as
|
|
93
|
-
values must be narrowed before use.
|
|
98
|
+
Represents an object whose keys and values are only known at runtime. It is defined as
|
|
99
|
+
`Record<string, unknown>` so values must be narrowed before use.
|
|
94
100
|
|
|
95
101
|
```ts
|
|
96
102
|
import type { RuntimeObject } from "@darthcav/ts-utils"
|
|
@@ -101,10 +107,31 @@ const payload: RuntimeObject = {
|
|
|
101
107
|
}
|
|
102
108
|
```
|
|
103
109
|
|
|
110
|
+
### `osRelease`
|
|
111
|
+
|
|
112
|
+
Returns OS release information for the current platform as an `OsRelease` object, or `null` on
|
|
113
|
+
unsupported platforms or when `/etc/os-release` is absent on Linux.
|
|
114
|
+
|
|
115
|
+
The `OsRelease` type exposes three normalized fields — `name`, `version`, and `arch` — present on
|
|
116
|
+
all platforms. On Linux, all raw key-value pairs from `/etc/os-release` (e.g. `PRETTY_NAME`, `ID`,
|
|
117
|
+
`ID_LIKE`) are also accessible by string index.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { osRelease } from "@darthcav/ts-utils"
|
|
121
|
+
|
|
122
|
+
const info = osRelease()
|
|
123
|
+
if (info) {
|
|
124
|
+
console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
125
|
+
console.log(info.version) // e.g. "24.04" or "10.0.22000"
|
|
126
|
+
console.log(info.arch) // e.g. "x64"
|
|
127
|
+
console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
104
131
|
### `getDummyLogger`
|
|
105
132
|
|
|
106
|
-
Returns a no-op `Logger` useful as a placeholder in tests. All logging methods are no-ops and
|
|
107
|
-
returns `false`. `getChild` and `with` return the same dummy logger instance.
|
|
133
|
+
Returns a no-op `Logger` useful as a placeholder in tests. All logging methods are no-ops and
|
|
134
|
+
`isEnabledFor` always returns `false`. `getChild` and `with` return the same dummy logger instance.
|
|
108
135
|
|
|
109
136
|
```ts
|
|
110
137
|
import { getDummyLogger } from "@darthcav/ts-utils"
|
|
@@ -115,13 +142,15 @@ const logger = getDummyLogger()
|
|
|
115
142
|
|
|
116
143
|
### `main`
|
|
117
144
|
|
|
118
|
-
Bootstraps an application process: logs startup information, optionally registers handlers for
|
|
119
|
-
(controlled by `defaultInterruptionHandler`, defaults to `true`), always
|
|
120
|
-
`unhandledRejection`, then delegates to an optional
|
|
145
|
+
Bootstraps an application process: logs startup information, optionally registers handlers for
|
|
146
|
+
`SIGINT` and `SIGTERM` (controlled by `defaultInterruptionHandler`, defaults to `true`), always
|
|
147
|
+
registers handlers for `uncaughtException` and `unhandledRejection`, then delegates to an optional
|
|
148
|
+
launcher function.
|
|
121
149
|
|
|
122
|
-
The three optional parameters — `launcher` (function), `monitorMemoryHours` (number, defaults to
|
|
123
|
-
`defaultInterruptionHandler` (boolean, defaults to `true`) — have distinct types. Any
|
|
124
|
-
the function resolves each by type, so middle parameters can be
|
|
150
|
+
The three optional parameters — `launcher` (function), `monitorMemoryHours` (number, defaults to
|
|
151
|
+
`0`), and `defaultInterruptionHandler` (boolean, defaults to `true`) — have distinct types. Any
|
|
152
|
+
subset can be passed in order and the function resolves each by type, so middle parameters can be
|
|
153
|
+
omitted:
|
|
125
154
|
|
|
126
155
|
```ts
|
|
127
156
|
import { getLogger } from "@logtape/logtape"
|
|
@@ -171,6 +200,7 @@ src/
|
|
|
171
200
|
index.ts # Public API entry point
|
|
172
201
|
main.ts # Main module
|
|
173
202
|
noop.ts # No-op function
|
|
203
|
+
os-utils.ts # OS release utilities
|
|
174
204
|
loggers/ # Logger utilities
|
|
175
205
|
__tests__/ # Test files
|
|
176
206
|
dist/ # Compiled output (generated)
|
|
@@ -183,8 +213,9 @@ public/ # Documentation output (generated)
|
|
|
183
213
|
|
|
184
214
|
[node-version]: https://img.shields.io/badge/node-%3E%3D25-orange.svg?style=flat-square
|
|
185
215
|
[node-url]: https://nodejs.org
|
|
186
|
-
[version-image]: https://img.shields.io/badge/version-0.
|
|
216
|
+
[version-image]: https://img.shields.io/badge/version-0.9.0-blue.svg?style=flat-square
|
|
187
217
|
[ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
|
|
188
|
-
[coverage-badge]:
|
|
218
|
+
[coverage-badge]:
|
|
219
|
+
https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
|
|
189
220
|
[coverage-url]: https://codecov.io/github/darthcav/ts-utils
|
|
190
221
|
[pages-url]: https://darthcav.github.io/ts-utils/
|
|
@@ -7,10 +7,10 @@ mock.module("node:fs", {
|
|
|
7
7
|
readFileSync,
|
|
8
8
|
},
|
|
9
9
|
});
|
|
10
|
-
const {
|
|
11
|
-
await suite("
|
|
10
|
+
const { osRelease } = await import("../os-utils.js");
|
|
11
|
+
await suite("osRelease (file missing)", async () => {
|
|
12
12
|
test("returns null when /etc/os-release does not exist", () => {
|
|
13
|
-
assert.equal(
|
|
13
|
+
assert.equal(osRelease(), null);
|
|
14
14
|
});
|
|
15
15
|
});
|
|
16
16
|
//# sourceMappingURL=os-utils-nofile.test.js.map
|
|
@@ -1 +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,
|
|
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,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAEpD,MAAM,KAAK,CAAC,0BAA0B,EAAE,KAAK,IAAI,EAAE;IAC/C,IAAI,CAAC,kDAAkD,EAAE,GAAG,EAAE;QAC1D,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
|
|
@@ -8,16 +8,11 @@ mock.module("node:os", {
|
|
|
8
8
|
arch: () => "x64",
|
|
9
9
|
},
|
|
10
10
|
});
|
|
11
|
-
const {
|
|
12
|
-
await suite("
|
|
13
|
-
test("returns null on non-Linux platforms", () => {
|
|
14
|
-
assert.equal(linuxRelease(), null);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
await suite("windowsRelease (win32 platform)", async () => {
|
|
11
|
+
const { osRelease } = await import("../os-utils.js");
|
|
12
|
+
await suite("osRelease (win32 platform)", async () => {
|
|
18
13
|
test("Windows 11 (build >= 22000)", () => {
|
|
19
14
|
mockRelease = "10.0.22000";
|
|
20
|
-
const result =
|
|
15
|
+
const result = osRelease();
|
|
21
16
|
assert.ok(result !== null);
|
|
22
17
|
assert.equal(result.name, "Windows 11");
|
|
23
18
|
assert.equal(result.version, "10.0.22000");
|
|
@@ -25,43 +20,43 @@ await suite("windowsRelease (win32 platform)", async () => {
|
|
|
25
20
|
});
|
|
26
21
|
test("Windows 10 (build < 22000)", () => {
|
|
27
22
|
mockRelease = "10.0.19041";
|
|
28
|
-
assert.equal(
|
|
23
|
+
assert.equal(osRelease()?.name, "Windows 10");
|
|
29
24
|
});
|
|
30
25
|
test("Windows 8.1", () => {
|
|
31
26
|
mockRelease = "6.3.9600";
|
|
32
|
-
assert.equal(
|
|
27
|
+
assert.equal(osRelease()?.name, "Windows 8.1");
|
|
33
28
|
});
|
|
34
29
|
test("Windows 8", () => {
|
|
35
30
|
mockRelease = "6.2.9200";
|
|
36
|
-
assert.equal(
|
|
31
|
+
assert.equal(osRelease()?.name, "Windows 8");
|
|
37
32
|
});
|
|
38
33
|
test("Windows 7", () => {
|
|
39
34
|
mockRelease = "6.1.7601";
|
|
40
|
-
assert.equal(
|
|
35
|
+
assert.equal(osRelease()?.name, "Windows 7");
|
|
41
36
|
});
|
|
42
37
|
test("Windows Vista", () => {
|
|
43
38
|
mockRelease = "6.0.6002";
|
|
44
|
-
assert.equal(
|
|
39
|
+
assert.equal(osRelease()?.name, "Windows Vista");
|
|
45
40
|
});
|
|
46
41
|
test("Windows XP 64-Bit Edition", () => {
|
|
47
42
|
mockRelease = "5.2.3790";
|
|
48
|
-
assert.equal(
|
|
43
|
+
assert.equal(osRelease()?.name, "Windows XP 64-Bit Edition");
|
|
49
44
|
});
|
|
50
45
|
test("Windows XP", () => {
|
|
51
46
|
mockRelease = "5.1.2600";
|
|
52
|
-
assert.equal(
|
|
47
|
+
assert.equal(osRelease()?.name, "Windows XP");
|
|
53
48
|
});
|
|
54
49
|
test("Windows 2000", () => {
|
|
55
50
|
mockRelease = "5.0.2195";
|
|
56
|
-
assert.equal(
|
|
51
|
+
assert.equal(osRelease()?.name, "Windows 2000");
|
|
57
52
|
});
|
|
58
53
|
test("unknown version", () => {
|
|
59
54
|
mockRelease = "4.0.1381";
|
|
60
|
-
assert.equal(
|
|
55
|
+
assert.equal(osRelease()?.name, "Windows unknown version");
|
|
61
56
|
});
|
|
62
57
|
test("Windows 10 when build number is absent", () => {
|
|
63
58
|
mockRelease = "10.0";
|
|
64
|
-
assert.equal(
|
|
59
|
+
assert.equal(osRelease()?.name, "Windows 10");
|
|
65
60
|
});
|
|
66
61
|
});
|
|
67
62
|
//# sourceMappingURL=os-utils-windows.test.js.map
|
|
@@ -1 +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;AAI7C,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,
|
|
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;AAI7C,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,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAA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QACvB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,eAAe,CAAC,CAAA;IACpD,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,2BAA2B,EAAE,GAAG,EAAE;QACnC,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,2BAA2B,CAAC,CAAA;IAChE,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,YAAY,EAAE,GAAG,EAAE;QACpB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;IACjD,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,cAAc,EAAE,GAAG,EAAE;QACtB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,CAAA;IACnD,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,iBAAiB,EAAE,GAAG,EAAE;QACzB,WAAW,GAAG,UAAU,CAAA;QACxB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,yBAAyB,CAAC,CAAA;IAC9D,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,WAAW,GAAG,MAAM,CAAA;QACpB,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,CAAA;IACjD,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
|
|
@@ -1,73 +1,68 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { platform } from "node:os";
|
|
3
3
|
import { suite, test } from "node:test";
|
|
4
|
-
import {
|
|
5
|
-
await suite("
|
|
4
|
+
import { osRelease } from "../os-utils.js";
|
|
5
|
+
await suite("osRelease", () => {
|
|
6
6
|
test("returns an object on Linux", () => {
|
|
7
7
|
if (platform() !== "linux") {
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
|
-
const result =
|
|
10
|
+
const result = osRelease();
|
|
11
11
|
assert.notEqual(result, null);
|
|
12
12
|
assert.equal(typeof result, "object");
|
|
13
13
|
});
|
|
14
|
-
test("returns
|
|
15
|
-
if (platform()
|
|
14
|
+
test("returns an object on Windows", () => {
|
|
15
|
+
if (platform() !== "win32") {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const result = osRelease();
|
|
19
|
+
assert.notEqual(result, null);
|
|
20
|
+
assert.equal(typeof result, "object");
|
|
21
|
+
});
|
|
22
|
+
test("returns null on unsupported platforms", () => {
|
|
23
|
+
if (platform() === "linux" || platform() === "win32") {
|
|
16
24
|
return;
|
|
17
25
|
}
|
|
18
|
-
assert.equal(
|
|
26
|
+
assert.equal(osRelease(), null);
|
|
19
27
|
});
|
|
20
|
-
test("
|
|
28
|
+
test("all values are strings on Linux", () => {
|
|
21
29
|
if (platform() !== "linux") {
|
|
22
30
|
return;
|
|
23
31
|
}
|
|
24
|
-
const result =
|
|
32
|
+
const result = osRelease();
|
|
25
33
|
assert.ok(result !== null);
|
|
26
34
|
for (const value of Object.values(result)) {
|
|
27
35
|
assert.equal(typeof value, "string");
|
|
28
36
|
}
|
|
29
37
|
});
|
|
30
|
-
test("
|
|
38
|
+
test("contains expected raw keys on Linux", () => {
|
|
31
39
|
if (platform() !== "linux") {
|
|
32
40
|
return;
|
|
33
41
|
}
|
|
34
|
-
const result =
|
|
42
|
+
const result = osRelease();
|
|
35
43
|
assert.ok(result !== null);
|
|
36
44
|
assert.ok("ID" in result);
|
|
37
45
|
assert.ok("NAME" in result);
|
|
38
46
|
});
|
|
39
|
-
test("values do not contain surrounding quotes", () => {
|
|
47
|
+
test("values do not contain surrounding quotes on Linux", () => {
|
|
40
48
|
if (platform() !== "linux") {
|
|
41
49
|
return;
|
|
42
50
|
}
|
|
43
|
-
const result =
|
|
51
|
+
const result = osRelease();
|
|
44
52
|
assert.ok(result !== null);
|
|
45
53
|
for (const value of Object.values(result)) {
|
|
54
|
+
if (typeof value !== "string") {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
46
57
|
assert.ok(!value.startsWith('"'));
|
|
47
58
|
assert.ok(!value.endsWith('"'));
|
|
48
59
|
}
|
|
49
60
|
});
|
|
50
|
-
|
|
51
|
-
|
|
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") {
|
|
61
|
+
test("name, version, and arch are strings", () => {
|
|
62
|
+
if (platform() !== "linux" && platform() !== "win32") {
|
|
68
63
|
return;
|
|
69
64
|
}
|
|
70
|
-
const result =
|
|
65
|
+
const result = osRelease();
|
|
71
66
|
assert.ok(result !== null);
|
|
72
67
|
assert.equal(typeof result.name, "string");
|
|
73
68
|
assert.equal(typeof result.version, "string");
|
|
@@ -77,7 +72,7 @@ await suite("windowsRelease", () => {
|
|
|
77
72
|
if (platform() !== "win32") {
|
|
78
73
|
return;
|
|
79
74
|
}
|
|
80
|
-
const result =
|
|
75
|
+
const result = osRelease();
|
|
81
76
|
assert.ok(result !== null);
|
|
82
77
|
assert.ok(result.name.startsWith("Windows "));
|
|
83
78
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"os-utils.test.js","sourceRoot":"","sources":["../../src/__tests__/os-utils.test.ts"],"names":[],"m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./millisecondsToString.ts";
|
|
10
10
|
import monitorMemory from "./monitorMemory.ts";
|
|
11
11
|
import noop from "./noop.ts";
|
|
12
|
-
import { type
|
|
12
|
+
import { type OsRelease, osRelease } from "./os-utils.ts";
|
|
13
13
|
import type { RuntimeObject } from "./types.ts";
|
|
14
|
-
export { getConsoleLogger, getDummyLogger, type LauncherFunction,
|
|
14
|
+
export { getConsoleLogger, getDummyLogger, type LauncherFunction, main, millisecondsToString, monitorMemory, noop, type OsRelease, osRelease, type RuntimeObject, };
|
|
15
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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;AAC5B,OAAO,
|
|
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,EAAE,KAAK,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C,OAAO,EACH,gBAAgB,EAChB,cAAc,EACd,KAAK,gBAAgB,EACrB,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,EACJ,KAAK,SAAS,EACd,SAAS,EACT,KAAK,aAAa,GACrB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -9,6 +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
|
-
import {
|
|
13
|
-
export { getConsoleLogger, getDummyLogger,
|
|
12
|
+
import { osRelease } from "./os-utils.js";
|
|
13
|
+
export { getConsoleLogger, getDummyLogger, main, millisecondsToString, monitorMemory, noop, osRelease, };
|
|
14
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;AAC5B,OAAO,
|
|
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,EAAkB,SAAS,EAAE,MAAM,eAAe,CAAA;AAGzD,OAAO,EACH,gBAAgB,EAChB,cAAc,EAEd,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,EAEJ,SAAS,GAEZ,CAAA"}
|
package/dist/os-utils.d.ts
CHANGED
|
@@ -1,48 +1,37 @@
|
|
|
1
|
+
import type { RuntimeObject } from "./types.ts";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* OS release information with normalized fields and optional platform-specific extras.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
5
|
+
* `name`, `version`, and `arch` are always present. On Linux, all keys from
|
|
6
|
+
* `/etc/os-release` (e.g. `ID`, `PRETTY_NAME`, `ID_LIKE`) are also accessible by string index.
|
|
6
7
|
*/
|
|
7
|
-
export
|
|
8
|
-
|
|
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"`. */
|
|
8
|
+
export type OsRelease = {
|
|
9
|
+
/** Human-readable OS name, e.g. `"Ubuntu"` or `"Windows 11"`. */
|
|
15
10
|
name: string;
|
|
16
|
-
/**
|
|
11
|
+
/** OS version string, e.g. `"24.04"` or `"10.0.22000"`. */
|
|
17
12
|
version: string;
|
|
18
13
|
/** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
|
|
19
14
|
arch: string;
|
|
20
|
-
}
|
|
15
|
+
} & RuntimeObject;
|
|
21
16
|
/**
|
|
22
|
-
* Returns
|
|
23
|
-
* or when
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* import { linuxRelease } from "@darthcav/ts-utils"
|
|
17
|
+
* Returns OS release information for the current platform, or `null` on unsupported
|
|
18
|
+
* platforms or when `/etc/os-release` is absent on Linux.
|
|
28
19
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*/
|
|
33
|
-
export declare function linuxRelease(): LinuxRelease | null;
|
|
34
|
-
/**
|
|
35
|
-
* Returns basic Windows version information, or `null` on non-Windows platforms.
|
|
20
|
+
* On Linux, `name`, `version`, and `arch` are normalized from `NAME`/`PRETTY_NAME`,
|
|
21
|
+
* `VERSION_ID`/`VERSION`, and `os.arch()` respectively. All raw keys from
|
|
22
|
+
* `/etc/os-release` are also present on the returned object.
|
|
36
23
|
*
|
|
37
|
-
*
|
|
24
|
+
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
25
|
+
* (>= 22000 → Windows 11).
|
|
38
26
|
*
|
|
39
27
|
* @example
|
|
40
28
|
* ```ts
|
|
41
|
-
* import {
|
|
29
|
+
* import { osRelease } from "@darthcav/ts-utils"
|
|
42
30
|
*
|
|
43
|
-
* const info =
|
|
44
|
-
* if (info) console.log(info.name)
|
|
31
|
+
* const info = osRelease()
|
|
32
|
+
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
33
|
+
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
45
34
|
* ```
|
|
46
35
|
*/
|
|
47
|
-
export declare function
|
|
36
|
+
export declare function osRelease(): OsRelease | null;
|
|
48
37
|
//# sourceMappingURL=os-utils.d.ts.map
|
package/dist/os-utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"os-utils.d.ts","sourceRoot":"","sources":["../src/os-utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"os-utils.d.ts","sourceRoot":"","sources":["../src/os-utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IACpB,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAA;IACZ,2DAA2D;IAC3D,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;CACf,GAAG,aAAa,CAAA;AAIjB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,IAAI,SAAS,GAAG,IAAI,CAiE5C"}
|
package/dist/os-utils.js
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { arch, release as
|
|
2
|
+
import { arch, release as kernelRelease, platform } from "node:os";
|
|
3
3
|
const OS_RELEASE = "/etc/os-release";
|
|
4
4
|
/**
|
|
5
|
-
* Returns
|
|
6
|
-
* or when
|
|
5
|
+
* Returns OS release information for the current platform, or `null` on unsupported
|
|
6
|
+
* platforms or when `/etc/os-release` is absent on Linux.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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.
|
|
8
|
+
* On Linux, `name`, `version`, and `arch` are normalized from `NAME`/`PRETTY_NAME`,
|
|
9
|
+
* `VERSION_ID`/`VERSION`, and `os.arch()` respectively. All raw keys from
|
|
10
|
+
* `/etc/os-release` are also present on the returned object.
|
|
37
11
|
*
|
|
38
|
-
*
|
|
12
|
+
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
13
|
+
* (>= 22000 → Windows 11).
|
|
39
14
|
*
|
|
40
15
|
* @example
|
|
41
16
|
* ```ts
|
|
42
|
-
* import {
|
|
17
|
+
* import { osRelease } from "@darthcav/ts-utils"
|
|
43
18
|
*
|
|
44
|
-
* const info =
|
|
45
|
-
* if (info) console.log(info.name)
|
|
19
|
+
* const info = osRelease()
|
|
20
|
+
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
21
|
+
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
46
22
|
* ```
|
|
47
23
|
*/
|
|
48
|
-
export function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
24
|
+
export function osRelease() {
|
|
25
|
+
const p = platform();
|
|
26
|
+
if (p === "linux") {
|
|
27
|
+
if (!existsSync(OS_RELEASE)) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
const raw = {};
|
|
31
|
+
for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
|
|
32
|
+
const eq = line.indexOf("=");
|
|
33
|
+
if (eq === -1) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const key = line.substring(0, eq).trim();
|
|
37
|
+
raw[key] = line
|
|
38
|
+
.substring(eq + 1)
|
|
39
|
+
.trim()
|
|
40
|
+
.replace(/^"|"$/g, "");
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
...raw,
|
|
44
|
+
name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
|
|
45
|
+
version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
|
|
46
|
+
arch: arch(),
|
|
47
|
+
};
|
|
57
48
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
49
|
+
if (p === "win32") {
|
|
50
|
+
const version = kernelRelease();
|
|
51
|
+
const [major, , build] = version.split(".").map(Number);
|
|
52
|
+
let name = "Windows ";
|
|
53
|
+
if (major === 10 && (build ?? 0) >= 22000) {
|
|
54
|
+
name += "11";
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
switch (version.substring(0, 3)) {
|
|
58
|
+
case "10.":
|
|
59
|
+
name += "10";
|
|
60
|
+
break;
|
|
61
|
+
case "6.3":
|
|
62
|
+
name += "8.1";
|
|
63
|
+
break;
|
|
64
|
+
case "6.2":
|
|
65
|
+
name += "8";
|
|
66
|
+
break;
|
|
67
|
+
case "6.1":
|
|
68
|
+
name += "7";
|
|
69
|
+
break;
|
|
70
|
+
case "6.0":
|
|
71
|
+
name += "Vista";
|
|
72
|
+
break;
|
|
73
|
+
case "5.2":
|
|
74
|
+
name += "XP 64-Bit Edition";
|
|
75
|
+
break;
|
|
76
|
+
case "5.1":
|
|
77
|
+
name += "XP";
|
|
78
|
+
break;
|
|
79
|
+
case "5.0":
|
|
80
|
+
name += "2000";
|
|
81
|
+
break;
|
|
82
|
+
default:
|
|
83
|
+
name += "unknown version";
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
87
86
|
}
|
|
87
|
+
return { name, version, arch: arch() };
|
|
88
88
|
}
|
|
89
|
-
return
|
|
89
|
+
return null;
|
|
90
90
|
}
|
|
91
91
|
//# sourceMappingURL=os-utils.js.map
|
package/dist/os-utils.js.map
CHANGED
|
@@ -1 +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,
|
|
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,aAAa,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAkBlE,MAAM,UAAU,GAAG,iBAAiB,CAAA;AAEpC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS;IACrB,MAAM,CAAC,GAAG,QAAQ,EAAE,CAAA;IACpB,IAAI,CAAC,KAAK,OAAO,EAAE,CAAC;QAChB,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAA;QACf,CAAC;QACD,MAAM,GAAG,GAA2B,EAAE,CAAA;QACtC,KAAK,MAAM,IAAI,IAAI,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/D,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;YAC5B,IAAI,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;gBACZ,SAAQ;YACZ,CAAC;YACD,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;YACxC,GAA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AC3B,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,IAAI,CAAA;oBACZ,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,MAAM,CAAA;oBACd,MAAK;gBACT;oBACI,IAAI,IAAI,iBAAiB,CAAA;oBACzB,MAAK;YACb,CAAC;QACL,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAA;IAC1C,CAAC;IACD,OAAO,IAAI,CAAA;AACf,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,25 +10,18 @@ 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
|
+
import { type OsRelease, osRelease } from "./os-utils.ts"
|
|
19
14
|
import type { RuntimeObject } from "./types.ts"
|
|
20
15
|
|
|
21
16
|
export {
|
|
22
17
|
getConsoleLogger,
|
|
23
18
|
getDummyLogger,
|
|
24
19
|
type LauncherFunction,
|
|
25
|
-
type LinuxRelease,
|
|
26
|
-
linuxRelease,
|
|
27
20
|
main,
|
|
28
21
|
millisecondsToString,
|
|
29
22
|
monitorMemory,
|
|
30
23
|
noop,
|
|
24
|
+
type OsRelease,
|
|
25
|
+
osRelease,
|
|
31
26
|
type RuntimeObject,
|
|
32
|
-
type WindowsRelease,
|
|
33
|
-
windowsRelease,
|
|
34
27
|
}
|
package/src/os-utils.ts
CHANGED
|
@@ -1,114 +1,107 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs"
|
|
2
|
-
import { arch, release as
|
|
2
|
+
import { arch, release as kernelRelease, platform } from "node:os"
|
|
3
|
+
import type { RuntimeObject } from "./types.ts"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* OS release information with normalized fields and optional platform-specific extras.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
8
|
+
* `name`, `version`, and `arch` are always present. On Linux, all keys from
|
|
9
|
+
* `/etc/os-release` (e.g. `ID`, `PRETTY_NAME`, `ID_LIKE`) are also accessible by string index.
|
|
9
10
|
*/
|
|
10
|
-
export
|
|
11
|
-
|
|
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"`. */
|
|
11
|
+
export type OsRelease = {
|
|
12
|
+
/** Human-readable OS name, e.g. `"Ubuntu"` or `"Windows 11"`. */
|
|
19
13
|
name: string
|
|
20
|
-
/**
|
|
14
|
+
/** OS version string, e.g. `"24.04"` or `"10.0.22000"`. */
|
|
21
15
|
version: string
|
|
22
16
|
/** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
|
|
23
17
|
arch: string
|
|
24
|
-
}
|
|
18
|
+
} & RuntimeObject
|
|
25
19
|
|
|
26
20
|
const OS_RELEASE = "/etc/os-release"
|
|
27
21
|
|
|
28
22
|
/**
|
|
29
|
-
* Returns
|
|
30
|
-
* or when
|
|
23
|
+
* Returns OS release information for the current platform, or `null` on unsupported
|
|
24
|
+
* platforms or when `/etc/os-release` is absent on Linux.
|
|
31
25
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
26
|
+
* On Linux, `name`, `version`, and `arch` are normalized from `NAME`/`PRETTY_NAME`,
|
|
27
|
+
* `VERSION_ID`/`VERSION`, and `os.arch()` respectively. All raw keys from
|
|
28
|
+
* `/etc/os-release` are also present on the returned object.
|
|
35
29
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
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).
|
|
30
|
+
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
31
|
+
* (>= 22000 → Windows 11).
|
|
64
32
|
*
|
|
65
33
|
* @example
|
|
66
34
|
* ```ts
|
|
67
|
-
* import {
|
|
35
|
+
* import { osRelease } from "@darthcav/ts-utils"
|
|
68
36
|
*
|
|
69
|
-
* const info =
|
|
70
|
-
* if (info) console.log(info.name)
|
|
37
|
+
* const info = osRelease()
|
|
38
|
+
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
39
|
+
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
71
40
|
* ```
|
|
72
41
|
*/
|
|
73
|
-
export function
|
|
74
|
-
|
|
75
|
-
|
|
42
|
+
export function osRelease(): OsRelease | null {
|
|
43
|
+
const p = platform()
|
|
44
|
+
if (p === "linux") {
|
|
45
|
+
if (!existsSync(OS_RELEASE)) {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
const raw: Record<string, string> = {}
|
|
49
|
+
for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
|
|
50
|
+
const eq = line.indexOf("=")
|
|
51
|
+
if (eq === -1) {
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
const key = line.substring(0, eq).trim()
|
|
55
|
+
raw[key] = line
|
|
56
|
+
.substring(eq + 1)
|
|
57
|
+
.trim()
|
|
58
|
+
.replace(/^"|"$/g, "")
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
...raw,
|
|
62
|
+
name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
|
|
63
|
+
version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
|
|
64
|
+
arch: arch(),
|
|
65
|
+
}
|
|
76
66
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
67
|
+
if (p === "win32") {
|
|
68
|
+
const version = kernelRelease()
|
|
69
|
+
const [major, , build] = version.split(".").map(Number)
|
|
70
|
+
let name = "Windows "
|
|
71
|
+
if (major === 10 && (build ?? 0) >= 22000) {
|
|
72
|
+
name += "11"
|
|
73
|
+
} else {
|
|
74
|
+
switch (version.substring(0, 3)) {
|
|
75
|
+
case "10.":
|
|
76
|
+
name += "10"
|
|
77
|
+
break
|
|
78
|
+
case "6.3":
|
|
79
|
+
name += "8.1"
|
|
80
|
+
break
|
|
81
|
+
case "6.2":
|
|
82
|
+
name += "8"
|
|
83
|
+
break
|
|
84
|
+
case "6.1":
|
|
85
|
+
name += "7"
|
|
86
|
+
break
|
|
87
|
+
case "6.0":
|
|
88
|
+
name += "Vista"
|
|
89
|
+
break
|
|
90
|
+
case "5.2":
|
|
91
|
+
name += "XP 64-Bit Edition"
|
|
92
|
+
break
|
|
93
|
+
case "5.1":
|
|
94
|
+
name += "XP"
|
|
95
|
+
break
|
|
96
|
+
case "5.0":
|
|
97
|
+
name += "2000"
|
|
98
|
+
break
|
|
99
|
+
default:
|
|
100
|
+
name += "unknown version"
|
|
101
|
+
break
|
|
102
|
+
}
|
|
111
103
|
}
|
|
104
|
+
return { name, version, arch: arch() }
|
|
112
105
|
}
|
|
113
|
-
return
|
|
106
|
+
return null
|
|
114
107
|
}
|