@gjsify/os 0.3.13 → 0.3.14

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/lib/esm/index.js CHANGED
@@ -1,29 +1,35 @@
1
+ import constants_default from "./constants.js";
2
+ import { cpus as cpus$1, endianness as endianness$1, freemem as freemem$1, loadavg as loadavg$1, networkInterfaces as networkInterfaces$1, totalmem as totalmem$1, uptime as uptime$1 } from "./darwin.js";
3
+ import { cpus as cpus$2, endianness as endianness$2, freemem as freemem$2, loadavg as loadavg$2, networkInterfaces as networkInterfaces$2, totalmem as totalmem$2, uptime as uptime$2 } from "./linux.js";
1
4
  import { cli, getPathSeparator } from "@gjsify/utils";
2
5
  import Gio from "@girs/gio-2.0";
6
+ import GLib from "@girs/glib-2.0";
7
+
8
+ //#region src/index.ts
9
+ /** Cached OS detection result */
3
10
  let _os = "";
11
+ /** Get the OS name (darwin, linux, win32) via uname */
4
12
  const getOs = () => {
5
- if (_os) return _os;
6
- const os = cli("uname -o").trim();
7
- if (/\bDarwin\b/i.test(os)) {
8
- _os = "darwin";
9
- return _os;
10
- }
11
- if (/\bLinux\b/i.test(os)) {
12
- _os = "linux";
13
- return _os;
14
- }
15
- _os = "win32";
16
- return _os;
17
- };
13
+ if (_os) return _os;
14
+ const os = cli("uname -o").trim();
15
+ if (/\bDarwin\b/i.test(os)) {
16
+ _os = "darwin";
17
+ return _os;
18
+ }
19
+ if (/\bLinux\b/i.test(os)) {
20
+ _os = "linux";
21
+ return _os;
22
+ }
23
+ _os = "win32";
24
+ return _os;
25
+ };
26
+ /** Cached PID */
18
27
  let _pid = 0;
28
+ /** Get the current process ID via Gio.Credentials */
19
29
  const getPid = () => {
20
- if (!_pid) _pid = new Gio.Credentials().get_unix_pid();
21
- return _pid;
30
+ if (!_pid) _pid = new Gio.Credentials().get_unix_pid();
31
+ return _pid;
22
32
  };
23
- import * as linux from "./linux.js";
24
- import * as darwin from "./darwin.js";
25
- import GLib from "@girs/glib-2.0";
26
- import constants from "./constants.js";
27
33
  const EOL = getPathSeparator() === "/" ? "\n" : "\r\n";
28
34
  const devNull = getPathSeparator() === "/" ? "/dev/null" : "\\\\.\\nul";
29
35
  const homedir = () => GLib.get_home_dir();
@@ -33,202 +39,170 @@ const tmpdir = () => GLib.get_tmp_dir();
33
39
  const type = () => cli("uname").trim();
34
40
  const platform = () => cli("uname -s").trim().toLowerCase();
35
41
  const arch = () => {
36
- const machine2 = cli("uname -m").trim();
37
- if (machine2 === "x86_64" || machine2 === "amd64") return "x64";
38
- if (machine2 === "aarch64" || machine2 === "arm64") return "arm64";
39
- if (machine2 === "i686" || machine2 === "i386") return "ia32";
40
- if (machine2.startsWith("arm")) return "arm";
41
- return machine2;
42
+ const machine = cli("uname -m").trim();
43
+ if (machine === "x86_64" || machine === "amd64") return "x64";
44
+ if (machine === "aarch64" || machine === "arm64") return "arm64";
45
+ if (machine === "i686" || machine === "i386") return "ia32";
46
+ if (machine.startsWith("arm")) return "arm";
47
+ return machine;
42
48
  };
43
49
  const machine = () => cli("uname -m").trim();
44
50
  const version = () => cli("uname -v").trim();
45
51
  const uptime = () => {
46
- const _os2 = getOs();
47
- switch (_os2) {
48
- case "darwin":
49
- return darwin.uptime();
50
- case "linux":
51
- return linux.uptime();
52
- default:
53
- return 0;
54
- }
52
+ const _os = getOs();
53
+ switch (_os) {
54
+ case "darwin": return uptime$1();
55
+ case "linux": return uptime$2();
56
+ default: return 0;
57
+ }
55
58
  };
56
59
  const totalmem = () => {
57
- const _os2 = getOs();
58
- switch (_os2) {
59
- case "darwin":
60
- return darwin.totalmem();
61
- case "linux":
62
- return linux.totalmem();
63
- default:
64
- return 0;
65
- }
60
+ const _os = getOs();
61
+ switch (_os) {
62
+ case "darwin": return totalmem$1();
63
+ case "linux": return totalmem$2();
64
+ default: return 0;
65
+ }
66
66
  };
67
67
  const availableParallelism = () => {
68
- const c = cpus();
69
- return c ? c.length : 1;
68
+ const c = cpus();
69
+ return c ? c.length : 1;
70
70
  };
71
71
  const userInfo = () => {
72
- let uid = 1e3;
73
- let gid = 100;
74
- let shell = "";
75
- try {
76
- uid = parseInt(cli("id -u"), 10);
77
- gid = parseInt(cli("id -g"), 10);
78
- shell = GLib.getenv("SHELL") || "";
79
- } catch {
80
- }
81
- return {
82
- uid,
83
- gid,
84
- username: GLib.get_user_name(),
85
- homedir: GLib.get_home_dir(),
86
- shell
87
- };
72
+ let uid = 1e3;
73
+ let gid = 100;
74
+ let shell = "";
75
+ try {
76
+ uid = parseInt(cli("id -u"), 10);
77
+ gid = parseInt(cli("id -g"), 10);
78
+ shell = GLib.getenv("SHELL") || "";
79
+ } catch {}
80
+ return {
81
+ uid,
82
+ gid,
83
+ username: GLib.get_user_name(),
84
+ homedir: GLib.get_home_dir(),
85
+ shell
86
+ };
88
87
  };
89
88
  const cpus = () => {
90
- const _os2 = getOs();
91
- switch (_os2) {
92
- case "darwin":
93
- return darwin.cpus();
94
- case "linux":
95
- return linux.cpus();
96
- default:
97
- console.warn(`${_os2} is not supported!`);
98
- break;
99
- }
89
+ const _os = getOs();
90
+ switch (_os) {
91
+ case "darwin": return cpus$1();
92
+ case "linux": return cpus$2();
93
+ default:
94
+ console.warn(`${_os} is not supported!`);
95
+ break;
96
+ }
100
97
  };
101
98
  const endianness = () => {
102
- const _os2 = getOs();
103
- switch (_os2) {
104
- case "darwin":
105
- return darwin.endianness();
106
- case "linux":
107
- return linux.endianness();
108
- default:
109
- console.warn(`${_os2} is not supported!`);
110
- break;
111
- }
99
+ const _os = getOs();
100
+ switch (_os) {
101
+ case "darwin": return endianness$1();
102
+ case "linux": return endianness$2();
103
+ default:
104
+ console.warn(`${_os} is not supported!`);
105
+ break;
106
+ }
112
107
  };
113
108
  const freemem = () => {
114
- const _os2 = getOs();
115
- switch (_os2) {
116
- case "darwin":
117
- return darwin.freemem();
118
- case "linux":
119
- return linux.freemem();
120
- default:
121
- console.warn(`${_os2} is not supported!`);
122
- break;
123
- }
109
+ const _os = getOs();
110
+ switch (_os) {
111
+ case "darwin": return freemem$1();
112
+ case "linux": return freemem$2();
113
+ default:
114
+ console.warn(`${_os} is not supported!`);
115
+ break;
116
+ }
124
117
  };
125
118
  const loadavg = () => {
126
- const _os2 = getOs();
127
- switch (_os2) {
128
- case "darwin":
129
- return darwin.loadavg();
130
- case "linux":
131
- return linux.loadavg();
132
- default:
133
- console.warn(`${_os2} is not supported!`);
134
- break;
135
- }
119
+ const _os = getOs();
120
+ switch (_os) {
121
+ case "darwin": return loadavg$1();
122
+ case "linux": return loadavg$2();
123
+ default:
124
+ console.warn(`${_os} is not supported!`);
125
+ break;
126
+ }
136
127
  };
137
128
  const networkInterfaces = () => {
138
- const _os2 = getOs();
139
- switch (_os2) {
140
- case "darwin":
141
- return darwin.networkInterfaces();
142
- case "linux":
143
- return linux.networkInterfaces();
144
- default:
145
- console.warn(`${_os2} is not supported!`);
146
- break;
147
- }
148
- };
129
+ const _os = getOs();
130
+ switch (_os) {
131
+ case "darwin": return networkInterfaces$1();
132
+ case "linux": return networkInterfaces$2();
133
+ default:
134
+ console.warn(`${_os} is not supported!`);
135
+ break;
136
+ }
137
+ };
138
+ /**
139
+ * Get process scheduling priority.
140
+ * Uses `ps -o ni=` to read the nice value for a given process.
141
+ * pid 0 (or omitted) means the current process.
142
+ */
149
143
  const getPriority = (pid) => {
150
- const targetPid = pid === void 0 || pid === 0 ? getPid() : pid;
151
- try {
152
- const nice = cli(`ps -o ni= -p ${targetPid}`).trim();
153
- const val = parseInt(nice, 10);
154
- if (!isNaN(val)) return val;
155
- } catch {
156
- }
157
- return 0;
158
- };
144
+ const targetPid = pid === undefined || pid === 0 ? getPid() : pid;
145
+ try {
146
+ const nice = cli(`ps -o ni= -p ${targetPid}`).trim();
147
+ const val = parseInt(nice, 10);
148
+ if (!isNaN(val)) return val;
149
+ } catch {}
150
+ return 0;
151
+ };
152
+ /**
153
+ * Set process scheduling priority.
154
+ * Uses `renice` command. Requires appropriate permissions for other processes.
155
+ */
159
156
  const setPriority = (pidOrPriority, priority) => {
160
- let pid;
161
- let prio;
162
- if (priority === void 0) {
163
- prio = pidOrPriority;
164
- pid = 0;
165
- } else {
166
- pid = pidOrPriority;
167
- prio = priority;
168
- }
169
- if (typeof pid !== "number" || !Number.isInteger(pid)) {
170
- throw new TypeError('The "pid" argument must be an integer');
171
- }
172
- if (typeof prio !== "number" || !Number.isInteger(prio) || prio < -20 || prio > 19) {
173
- throw new RangeError('The "priority" argument must be an integer between -20 and 19');
174
- }
175
- try {
176
- const actualPid = pid === 0 ? getPid() : pid;
177
- cli(`renice -n ${prio} -p ${actualPid}`);
178
- } catch (err) {
179
- const error = new Error(`A system error occurred: priority could not be set`);
180
- error.code = "ERR_SYSTEM_ERROR";
181
- throw error;
182
- }
183
- };
184
- var index_default = {
185
- EOL,
186
- arch,
187
- availableParallelism,
188
- constants,
189
- cpus,
190
- devNull,
191
- endianness,
192
- freemem,
193
- getPriority,
194
- homedir,
195
- hostname,
196
- loadavg,
197
- machine,
198
- networkInterfaces,
199
- platform,
200
- release,
201
- setPriority,
202
- tmpdir,
203
- totalmem,
204
- type,
205
- uptime,
206
- userInfo,
207
- version
208
- };
209
- export {
210
- EOL,
211
- arch,
212
- availableParallelism,
213
- constants,
214
- cpus,
215
- index_default as default,
216
- devNull,
217
- endianness,
218
- freemem,
219
- getPriority,
220
- homedir,
221
- hostname,
222
- loadavg,
223
- machine,
224
- networkInterfaces,
225
- platform,
226
- release,
227
- setPriority,
228
- tmpdir,
229
- totalmem,
230
- type,
231
- uptime,
232
- userInfo,
233
- version
234
- };
157
+ let pid;
158
+ let prio;
159
+ if (priority === undefined) {
160
+ prio = pidOrPriority;
161
+ pid = 0;
162
+ } else {
163
+ pid = pidOrPriority;
164
+ prio = priority;
165
+ }
166
+ if (typeof pid !== "number" || !Number.isInteger(pid)) {
167
+ throw new TypeError("The \"pid\" argument must be an integer");
168
+ }
169
+ if (typeof prio !== "number" || !Number.isInteger(prio) || prio < -20 || prio > 19) {
170
+ throw new RangeError("The \"priority\" argument must be an integer between -20 and 19");
171
+ }
172
+ try {
173
+ const actualPid = pid === 0 ? getPid() : pid;
174
+ cli(`renice -n ${prio} -p ${actualPid}`);
175
+ } catch (err) {
176
+ const error = new Error(`A system error occurred: priority could not be set`);
177
+ error.code = "ERR_SYSTEM_ERROR";
178
+ throw error;
179
+ }
180
+ };
181
+ var src_default = {
182
+ EOL,
183
+ arch,
184
+ availableParallelism,
185
+ constants: constants_default,
186
+ cpus,
187
+ devNull,
188
+ endianness,
189
+ freemem,
190
+ getPriority,
191
+ homedir,
192
+ hostname,
193
+ loadavg,
194
+ machine,
195
+ networkInterfaces,
196
+ platform,
197
+ release,
198
+ setPriority,
199
+ tmpdir,
200
+ totalmem,
201
+ type,
202
+ uptime,
203
+ userInfo,
204
+ version
205
+ };
206
+
207
+ //#endregion
208
+ export { EOL, arch, availableParallelism, constants_default as constants, cpus, src_default as default, devNull, endianness, freemem, getPriority, homedir, hostname, loadavg, machine, networkInterfaces, platform, release, setPriority, tmpdir, totalmem, type, uptime, userInfo, version };