-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (29 loc) · 810 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const cp = require('child_process');
const os = require('os');
function getFromPlatform() {
switch (process.platform) {
case 'linux': {
const result = cp.spawnSync('nproc');
return Number(result.stdout.toString().trim());
}
case 'darwin': {
const result = cp.spawnSync('sysctl', ['-n', 'hw.logicalcpu']);
return Number(result.stdout.toString().trim());
}
case 'win32': {
return Number(process.env.NUMBER_OF_PROCESSORS);
}
}
}
module.exports = function processingUnits() {
try {
const result = getFromPlatform();
if (typeof result === 'number' && !Number.isNaN(result) && result > 0) {
return result
}
} catch (err) {
// ignore
}
return null;
};