Skip to content

Commit fe538bf

Browse files
committed
Improved resilience if a command is not available
1 parent 6c084b3 commit fe538bf

File tree

9 files changed

+287
-303
lines changed

9 files changed

+287
-303
lines changed

.github/workflows/on-push.yml

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: On Push
22
on: [push]
33
jobs:
44
build:
5-
name: Building
5+
name: Build & Test
66
runs-on: Linux
77
steps:
88
- name: Checking out repository
@@ -16,20 +16,7 @@ jobs:
1616
run: npm install
1717
- name: Building
1818
run: npm run build
19-
20-
testing:
21-
name: Testing
22-
needs: build
23-
runs-on: Linux
24-
steps:
25-
- name: "Setup Node"
26-
uses: actions/setup-node@v4
27-
with:
28-
node-version: '22'
29-
cache: 'npm'
30-
- name: Installing
31-
run: npm install
32-
- name: Running linter
19+
- name: Linting
3320
run: npm run lint
34-
- name: Running tests
35-
run: npm run test
21+
- name: Testing
22+
run: npm run test

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-system",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "NodeJS library to retrieve system information and utilization.",
55
"files": [
66
"lib/**/*"

src/cpu.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import os from 'os';
22
import { sleep } from './utils';
33

4-
54
export type CPUUtilization = {
6-
75
/** Overall CPU utilization as a percentage (0.0-1.0). */
86
overall: number;
97

108
/** Utilization of each CPU core as a percentage (0.0-1.0). */
119
cores: number[];
12-
1310
};
1411

1512
export type CPU = {
16-
1713
/** Name of the CPU. */
1814
name: string;
1915

@@ -33,19 +29,15 @@ export type CPU = {
3329
utilization: CPUUtilization;
3430
};
3531

36-
37-
38-
3932
/** Intervall in milliseconds at which CPU utilization is computed. */
4033
export let CPU_COMPUTE_UTILIZATION_INTERVAL = 1000;
4134

42-
4335
const CPU_COMPUTE_UTILIZATION_INITIAL_DELAY = 50;
4436

4537
let PREV_CPU_CORES: os.CpuInfo[] = [];
4638
const CPU_UTILIZATION: CPUUtilization = {
4739
overall: 0,
48-
cores: []
40+
cores: [],
4941
};
5042
let CPU_COMPUTE_RUNNING = false;
5143
let CPU_COMPUTE_TIMEOUT: NodeJS.Timeout | null = null;
@@ -96,11 +88,9 @@ export function stopCpuUtilizationComputation() {
9688
CPU_COMPUTE_RUNNING = false;
9789
}
9890

99-
100-
10191
/**
10292
* Returns information about the CPU.
103-
*
93+
*
10494
* @returns CPU information.
10595
*/
10696
export async function getCpuInfo(): Promise<CPU> {
@@ -112,14 +102,13 @@ export async function getCpuInfo(): Promise<CPU> {
112102
name: cpuCores[0].model,
113103
speed: cpuCores[0].speed,
114104
utilization: await getCpuUtilization(),
115-
}
105+
};
116106
}
117107

118-
119108
/**
120109
* Returns the current CPU utilization.
121110
* If the computation is not running, it will start the computation and return the initial values.
122-
*
111+
*
123112
* @returns CPU utilization data.
124113
*/
125114
export async function getCpuUtilization(): Promise<CPUUtilization> {
@@ -131,8 +120,6 @@ export async function getCpuUtilization(): Promise<CPUUtilization> {
131120
return CPU_UTILIZATION;
132121
}
133122

134-
135-
136123
/**
137124
* Returns the number of CPU cores available on the system.
138125
*
@@ -141,4 +128,3 @@ export async function getCpuUtilization(): Promise<CPUUtilization> {
141128
export function getCpuCoreCount(): number {
142129
return os.cpus().length;
143130
}
144-

src/drive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function getDrives(includeVirtual: boolean = false): Promise<DriveI
4040
switch (process.platform) {
4141
case 'darwin':
4242
case 'linux': {
43-
const output = await execCommand('df -PT --block-size=1');
43+
const output = await execCommand('df -PT --block-size=1').catch(() => '');
4444
const lines = output.split('\n');
4545
for (let i = 1; i < lines.length; i++) {
4646
// skip first line (header)
@@ -68,7 +68,7 @@ export async function getDrives(includeVirtual: boolean = false): Promise<DriveI
6868
case 'win32': {
6969
const output = await execCommand(
7070
'powershell -Command "Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, VolumeName, Size, FreeSpace, FileSystem | ConvertTo-Json"',
71-
);
71+
).catch(() => '');
7272
const json = JSON.parse(output);
7373
for (const drive of json) {
7474
const total = parseInt(drive.Size, 10) || 0;

0 commit comments

Comments
 (0)