Skip to content

Commit ef2d838

Browse files
committed
Asyncify
1 parent 3a92931 commit ef2d838

13 files changed

+233
-248
lines changed

lib/audio.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// ----------------------------------------------------------------------------------
1515

1616
const exec = require('child_process').exec;
17-
const execSync = require('child_process').execSync;
1817
const util = require('./util');
18+
const execAsync = util.execAsync;
1919

2020
let _platform = process.platform;
2121

@@ -55,11 +55,11 @@ function parseAudioType(str, input, output) {
5555
}
5656

5757

58-
function getLinuxAudioPci() {
58+
async function getLinuxAudioPci() {
5959
let cmd = 'lspci -v 2>/dev/null';
6060
let result = [];
6161
try {
62-
const parts = execSync(cmd, util.execOptsLinux).toString().split('\n\n');
62+
const parts = (await execAsync(cmd, util.execOptsLinux)).toString().split('\n\n');
6363
parts.forEach(element => {
6464
const lines = element.split('\n');
6565
if (lines && lines.length && lines[0].toLowerCase().indexOf('audio') >= 0) {
@@ -154,10 +154,10 @@ function audio(callback) {
154154
let result = [];
155155
if (_linux || _freebsd || _openbsd || _netbsd) {
156156
let cmd = 'lspci -vmm 2>/dev/null';
157-
exec(cmd, function (error, stdout) {
157+
exec(cmd, async function (error, stdout) {
158158
// PCI
159159
if (!error) {
160-
const audioPCI = getLinuxAudioPci();
160+
const audioPCI = await getLinuxAudioPci();
161161
const parts = stdout.toString().split('\n\n');
162162
parts.forEach(element => {
163163
const lines = element.split('\n');

lib/battery.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
6464
module.exports = function (callback) {
6565

6666
return new Promise((resolve) => {
67-
process.nextTick(() => {
67+
process.nextTick(async () => {
6868
let result = {
6969
hasBattery: false,
7070
cycleCount: 0,
@@ -100,7 +100,7 @@ module.exports = function (callback) {
100100
}
101101

102102
if (acPath) {
103-
const file = fs.readFileSync(acPath);
103+
const file = await fs.promises.readFile(acPath);
104104
acConnected = file.toString().trim() === '1';
105105
}
106106

lib/bluetooth.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
// ----------------------------------------------------------------------------------
1515

1616
const exec = require('child_process').exec;
17-
const execSync = require('child_process').execSync;
1817
const path = require('path');
1918
const util = require('./util');
19+
const execAsync = util.execAsync;
2020
const fs = require('fs');
2121

2222
let _platform = process.platform;
@@ -112,24 +112,24 @@ function parseWindowsBluetooth(lines) {
112112
function bluetoothDevices(callback) {
113113

114114
return new Promise((resolve) => {
115-
process.nextTick(() => {
115+
process.nextTick(async () => {
116116
let result = [];
117117
if (_linux) {
118118
// get files in /var/lib/bluetooth/ recursive
119119
const btFiles = util.getFilesInPath('/var/lib/bluetooth/');
120-
btFiles.forEach((element) => {
120+
btFiles.forEach(async (element) => {
121121
const filename = path.basename(element);
122122
const pathParts = element.split('/');
123123
const macAddr1 = pathParts.length >= 6 ? pathParts[pathParts.length - 2] : null;
124124
const macAddr2 = pathParts.length >= 7 ? pathParts[pathParts.length - 3] : null;
125125
if (filename === 'info') {
126-
const infoFile = fs.readFileSync(element, { encoding: 'utf8' }).split('\n');
126+
const infoFile = (await fs.promises.readFile(element, { encoding: 'utf8' })).split('\n');
127127
result.push(parseLinuxBluetoothInfo(infoFile, macAddr1, macAddr2));
128128
}
129129
});
130130
// determine "connected" with hcitool con
131131
try {
132-
const hdicon = execSync('hcitool con', util.execOptsLinux).toString().toLowerCase();
132+
const hdicon = (await execAsync('hcitool con', util.execOptsLinux)).toString().toLowerCase();
133133
for (let i = 0; i < result.length; i++) {
134134
if (result[i].macDevice && result[i].macDevice.length > 10 && hdicon.indexOf(result[i].macDevice.toLowerCase()) >= 0) {
135135
result[i].connected = true;

lib/cpu.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
const os = require('os');
1717
const exec = require('child_process').exec;
18-
const execSync = require('child_process').execSync;
1918
const fs = require('fs');
2019
const util = require('./util');
20+
const execAsync = util.execAsync;
2121

2222
let _platform = process.platform;
2323

@@ -676,7 +676,7 @@ function getCpu() {
676676
result.flags = flags;
677677
result.virtualization = flags.indexOf('vmx') > -1 || flags.indexOf('svm') > -1;
678678
if (_darwin) {
679-
exec('sysctl machdep.cpu hw.cpufrequency_max hw.cpufrequency_min hw.packages hw.physicalcpu_max hw.ncpu hw.tbfrequency hw.cpufamily hw.cpusubfamily', function (error, stdout) {
679+
exec('sysctl machdep.cpu hw.cpufrequency_max hw.cpufrequency_min hw.packages hw.physicalcpu_max hw.ncpu hw.tbfrequency hw.cpufamily hw.cpusubfamily', async function (error, stdout) {
680680
let lines = stdout.toString().split('\n');
681681
const modelline = util.getValue(lines, 'machdep.cpu.brand_string');
682682
const modellineParts = modelline.split('@');
@@ -702,7 +702,7 @@ function getCpu() {
702702
if (os.arch() === 'arm64') {
703703
result.socket = 'SOC';
704704
try {
705-
const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type').toString().split('\n');
705+
const clusters = (await execAsync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type')).toString().split('\n');
706706
const efficiencyCores = clusters.filter(line => line.indexOf('"E"') >= 0).length;
707707
const performanceCores = clusters.filter(line => line.indexOf('"P"') >= 0).length;
708708
result.efficiencyCores = efficiencyCores;
@@ -728,7 +728,7 @@ function getCpu() {
728728
let modelline = '';
729729
let lines = [];
730730
if (os.cpus()[0] && os.cpus()[0].model) { modelline = os.cpus()[0].model; }
731-
exec('export LC_ALL=C; lscpu; echo -n "Governor: "; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; echo; unset LC_ALL', function (error, stdout) {
731+
exec('export LC_ALL=C; lscpu; echo -n "Governor: "; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; echo; unset LC_ALL', async function (error, stdout) {
732732
if (!error) {
733733
lines = stdout.toString().split('\n');
734734
}
@@ -777,7 +777,7 @@ function getCpu() {
777777

778778
// Test Raspberry
779779
if (result.vendor === 'ARM') {
780-
const linesRpi = fs.readFileSync('/proc/cpuinfo').toString().split('\n');
780+
const linesRpi = (await fs.promises.readFile('/proc/cpuinfo')).toString().split('\n');
781781
const rPIRevision = util.decodePiCpuinfo(linesRpi);
782782
if (rPIRevision.model.toLowerCase().indexOf('raspberry') >= 0) {
783783
result.family = result.manufacturer;
@@ -1037,7 +1037,7 @@ exports.cpuCurrentSpeed = cpuCurrentSpeed;
10371037
function cpuTemperature(callback) {
10381038

10391039
return new Promise((resolve) => {
1040-
process.nextTick(() => {
1040+
process.nextTick(async () => {
10411041
let result = {
10421042
main: null,
10431043
cores: [],
@@ -1049,7 +1049,7 @@ function cpuTemperature(callback) {
10491049
// CPU Chipset, Socket
10501050
try {
10511051
const cmd = 'cat /sys/class/thermal/thermal_zone*/type 2>/dev/null; echo "-----"; cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null;';
1052-
const parts = execSync(cmd, util.execOptsLinux).toString().split('-----\n');
1052+
const parts = (await execAsync(cmd, util.execOptsLinux)).toString().split('-----\n');
10531053
if (parts.length === 2) {
10541054
const lines = parts[0].split('\n');
10551055
const lines2 = parts[1].split('\n');
@@ -1579,7 +1579,7 @@ exports.cpuCache = cpuCache;
15791579
function getLoad() {
15801580

15811581
return new Promise((resolve) => {
1582-
process.nextTick(() => {
1582+
process.nextTick(async () => {
15831583
let loads = os.loadavg().map(function (x) { return x / util.cores(); });
15841584
let avgLoad = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
15851585
let result = {};
@@ -1605,7 +1605,7 @@ function getLoad() {
16051605
// linux: try to get other cpu stats
16061606
if (_linux) {
16071607
try {
1608-
const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu', util.execOptsLinux).toString().split('\n');
1608+
const lines = (await execAsync('cat /proc/stat 2>/dev/null | grep cpu', util.execOptsLinux)).toString().split('\n');
16091609
if (lines.length > 1) {
16101610
lines.shift();
16111611
if (lines.length === cpus.length) {

lib/filesystem.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const util = require('./util');
1717
const fs = require('fs');
1818

1919
const exec = require('child_process').exec;
20-
const execSync = require('child_process').execSync;
21-
const execPromiseSave = util.promisifySave(require('child_process').exec);
20+
const execAsync = util.execAsync;
2221

2322
let _platform = process.platform;
2423

@@ -115,7 +114,7 @@ function fsSize(drive, callback) {
115114
}
116115

117116
return new Promise((resolve) => {
118-
process.nextTick(() => {
117+
process.nextTick(async () => {
119118
let data = [];
120119
if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
121120
let cmd = '';
@@ -124,10 +123,10 @@ function fsSize(drive, callback) {
124123
if (_darwin) {
125124
cmd = 'df -kP';
126125
try {
127-
macOsDisks = execSync('diskutil list').toString().split('\n').filter(line => {
126+
macOsDisks = (await execAsync('diskutil list')).toString().split('\n').filter(line => {
128127
return !line.startsWith('/') && line.indexOf(':') > 0;
129128
});
130-
execSync('mount').toString().split('\n').filter(line => {
129+
(await execAsync('mount')).toString().split('\n').filter(line => {
131130
return line.startsWith('/');
132131
}).forEach((line) => {
133132
osMounts[line.split(' ')[0]] = line.toLowerCase().indexOf('read-only') === -1;
@@ -139,7 +138,7 @@ function fsSize(drive, callback) {
139138
if (_linux) {
140139
try {
141140
cmd = 'export LC_ALL=C; df -lkPTx squashfs; unset LC_ALL';
142-
execSync('cat /proc/mounts 2>/dev/null', util.execOptsLinux).toString().split('\n').filter(line => {
141+
(await execAsync('cat /proc/mounts 2>/dev/null', util.execOptsLinux)).toString().split('\n').filter(line => {
143142
return line.startsWith('/');
144143
}).forEach((line) => {
145144
osMounts[line.split(' ')[0]] = osMounts[line.split(' ')[0]] || false;
@@ -154,7 +153,7 @@ function fsSize(drive, callback) {
154153
if (_freebsd || _openbsd || _netbsd) {
155154
try {
156155
cmd = 'df -lkPT';
157-
execSync('mount').toString().split('\n').forEach((line) => {
156+
(await execAsync('mount')).toString().split('\n').forEach((line) => {
158157
osMounts[line.split(' ')[0]] = line.toLowerCase().indexOf('read-only') === -1;
159158
});
160159
} catch (e) {
@@ -420,13 +419,13 @@ function decodeMdabmData(lines) {
420419
};
421420
}
422421

423-
function raidMatchLinux(data) {
422+
async function raidMatchLinux(data) {
424423
// for all block devices of type "raid%"
425424
let result = data;
426425
try {
427-
data.forEach(element => {
426+
for (let element of data) {
428427
if (element.type.startsWith('raid')) {
429-
const lines = execSync(`mdadm --export --detail /dev/${element.name}`, util.execOptsLinux).toString().split('\n');
428+
const lines = (await execAsync(`mdadm --export --detail /dev/${element.name}`, util.execOptsLinux)).toString().split('\n');
430429
const mdData = decodeMdabmData(lines);
431430

432431
element.label = mdData.label; // <- assign label info
@@ -441,7 +440,7 @@ function raidMatchLinux(data) {
441440
});
442441
}
443442
}
444-
});
443+
}
445444
} catch (e) {
446445
util.noop();
447446
}
@@ -570,27 +569,27 @@ function blkStdoutToObject(stdout) {
570569
function blockDevices(callback) {
571570

572571
return new Promise((resolve) => {
573-
process.nextTick(() => {
572+
process.nextTick(async () => {
574573
let data = [];
575574
if (_linux) {
576575
// see https://wiki.ubuntuusers.de/lsblk/
577576
// exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,TRAN,SERIAL,LABEL,MODEL,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,SCHED,RQ-SIZE,RA,WSAME", function (error, stdout) {
578-
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
577+
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, async function (error, stdout) {
579578
if (!error) {
580579
let lines = blkStdoutToObject(stdout).split('\n');
581580
data = parseBlk(lines);
582-
data = raidMatchLinux(data);
581+
data = await raidMatchLinux(data);
583582
data = matchDevicesLinux(data);
584583
if (callback) {
585584
callback(data);
586585
}
587586
resolve(data);
588587
} else {
589-
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
588+
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, async function (error, stdout) {
590589
if (!error) {
591590
let lines = blkStdoutToObject(stdout).split('\n');
592591
data = parseBlk(lines);
593-
data = raidMatchLinux(data);
592+
data = await raidMatchLinux(data);
594593
}
595594
if (callback) {
596595
callback(data);
@@ -1056,7 +1055,7 @@ function diskLayout(callback) {
10561055
}
10571056

10581057
return new Promise((resolve) => {
1059-
process.nextTick(() => {
1058+
process.nextTick(async () => {
10601059

10611060
const commitResult = res => {
10621061
for (let i = 0; i < res.length; i++) {
@@ -1074,7 +1073,7 @@ function diskLayout(callback) {
10741073
if (_linux) {
10751074
let cmdFullSmart = '';
10761075

1077-
exec('export LC_ALL=C; lsblk -ablJO 2>/dev/null; unset LC_ALL', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
1076+
exec('export LC_ALL=C; lsblk -ablJO 2>/dev/null; unset LC_ALL', { maxBuffer: 1024 * 1024 }, async function (error, stdout) {
10781077
if (!error) {
10791078
try {
10801079
const out = stdout.toString().trim();
@@ -1087,20 +1086,20 @@ function diskLayout(callback) {
10871086
} catch (e) {
10881087
// fallback to older version of lsblk
10891088
try {
1090-
const out2 = execSync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL', util.execOptsLinux).toString();
1089+
const out2 = (await execAsync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL', util.execOptsLinux)).toString();
10911090
let lines = blkStdoutToObject(out2).split('\n');
10921091
const data = parseBlk(lines);
10931092
devices = data.filter(item => { return (item.type === 'disk') && item.size > 0 && ((item.model !== null && item.model !== '') || (item.mount === '' && item.label === '' && item.fsType === '')); });
10941093
} catch (e) {
10951094
util.noop();
10961095
}
10971096
}
1098-
devices.forEach((device) => {
1097+
for (let device of devices) {
10991098
let mediumType = '';
11001099
const BSDName = '/dev/' + device.name;
11011100
const logical = device.name;
11021101
try {
1103-
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null', util.execOptsLinux).toString().split('\n')[0];
1102+
mediumType = (await execAsync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null', util.execOptsLinux)).toString().split('\n')[0];
11041103
} catch (e) {
11051104
util.noop();
11061105
}
@@ -1131,7 +1130,7 @@ function diskLayout(callback) {
11311130
});
11321131
cmd += `printf "\n${BSDName}|"; smartctl -H ${BSDName} | grep overall;`;
11331132
cmdFullSmart += `${cmdFullSmart ? 'printf ",";' : ''}smartctl -a -j ${BSDName};`;
1134-
});
1133+
}
11351134
} catch (e) {
11361135
util.noop();
11371136
}
@@ -1406,10 +1405,10 @@ function diskLayout(callback) {
14061405
workload.push(util.powerShell('Get-PhysicalDisk | select BusType,MediaType,FriendlyName,Model,SerialNumber,Size | fl'));
14071406
if (util.smartMonToolsInstalled()) {
14081407
try {
1409-
const smartDev = JSON.parse(execSync('smartctl --scan -j').toString());
1408+
const smartDev = JSON.parse((await execAsync('smartctl --scan -j')).toString());
14101409
if (smartDev && smartDev.devices && smartDev.devices.length > 0) {
14111410
smartDev.devices.forEach((dev) => {
1412-
workload.push(execPromiseSave(`smartctl -j -a ${dev.name}`, util.execOptsWin));
1411+
workload.push(execAsync(`smartctl -j -a ${dev.name}`, util.execOptsWin).catch(() => ''));
14131412
});
14141413
}
14151414
} catch (e) {

0 commit comments

Comments
 (0)