diff --git a/lib/cpu.js b/lib/cpu.js index eb405b6..4de9725 100644 --- a/lib/cpu.js +++ b/lib/cpu.js @@ -429,7 +429,7 @@ function getCpu() { } if (_windows) { try { - exec(util.getWmic() + ' cpu get /value', util.execOptsWin, function (error, stdout) { + util.wmic('cpu get /value').then((stdout, error) => { if (!error) { let lines = stdout.split('\r\n'); let name = util.getValue(lines, 'name', '=') || ''; @@ -489,7 +489,7 @@ function getCpu() { result.physicalCores = parseInt(countCores) || util.cores(); } } - exec(util.getWmic() + ' path Win32_CacheMemory get CacheType,InstalledSize,Purpose', function (error, stdout) { + util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => { if (!error) { let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0); lines.forEach(function (line) { @@ -701,7 +701,7 @@ function cpuTemperature(callback) { } if (_windows) { try { - exec(util.getWmic() + ' /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature', util.execOptsWin, function (error, stdout) { + util.wmic('/namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature').then((stdout, error) => { if (!error) { let sum = 0; let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0); @@ -929,7 +929,7 @@ function cpuCache(callback) { } if (_windows) { try { - exec(util.getWmic() + ' cpu get l2cachesize, l3cachesize /value', util.execOptsWin, function (error, stdout) { + util.wmic('cpu get l2cachesize, l3cachesize /value').then((stdout, error) => { if (!error) { let lines = stdout.split('\r\n'); result.l1d = 0; @@ -939,7 +939,7 @@ function cpuCache(callback) { if (result.l2) { result.l2 = parseInt(result.l2, 10) * 1024; } if (result.l3) { result.l3 = parseInt(result.l3, 10) * 1024; } } - exec(util.getWmic() + ' path Win32_CacheMemory get CacheType,InstalledSize,Purpose', function (error, stdout) { + util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => { if (!error) { let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0); lines.forEach(function (line) { diff --git a/lib/util.js b/lib/util.js index d43be94..3cb4494 100644 --- a/lib/util.js +++ b/lib/util.js @@ -28,7 +28,7 @@ const _openbsd = (_platform === 'openbsd'); // const _sunos = (_platform === 'sunos'); let _cores = 0; -let wmic = ''; +let wmicPath = ''; let codepage = ''; const execOptsWin = { @@ -220,16 +220,39 @@ function findObjectByKey(array, key, value) { } function getWmic() { - if (os.type() === 'Windows_NT' && !wmic) { + if (os.type() === 'Windows_NT' && !wmicPath) { try { - wmic = execSync('WHERE WMIC').toString().trim(); + wmicPath = execSync('WHERE WMIC').toString().trim(); } catch (e) { if (fs.existsSync(process.env.WINDIR + '\\system32\\wbem\\wmic.exe')) { wmic = process.env.WINDIR + '\\system32\\wbem\\wmic.exe'; - } else wmic = 'wmic'; + } else wmicPath = 'wmic'; } } - return wmic; + return wmicPath; +} + +function wmic(command, options) { + options = options || execOptsWin; + return new Promise((resolve) => { + process.nextTick(() => { + try { + exec(getWmic() + ' ' + command, options, function (error, stdout) { + resolve(stdout, error) + // resolve({ + // stdout, + // error + // }); + }).stdin.end(); + } catch (e) { + resolve('', e) + // resolve({ + // stdout: '', + // error: e + // }); + } + }) + }) } function powerShell(cmd) { @@ -384,6 +407,7 @@ exports.parseDateTime = parseDateTime; exports.parseHead = parseHead; exports.findObjectByKey = findObjectByKey; exports.getWmic = getWmic; +exports.wmic = wmic; exports.powerShell = powerShell; exports.nanoSeconds = nanoSeconds; exports.countUniqueLines = countUniqueLines;