From 942e497025a04afc1fb9b73857b726f46bbdaa82 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Wed, 5 Sep 2018 22:51:06 +0200 Subject: [PATCH] diskLayout() optimized media type detection (HD, SSD) on Windows --- CHANGELOG.md | 1 + lib/filesystem.js | 44 +++++++++++++++++++++++++++++++++++++++----- lib/util.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d06281c..3bf0d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.45.3 | 2018-09-06 | `diskLayout()` optimized media type detection (HD, SSD) on Windows | | 3.45.2 | 2018-09-05 | updated imags shields icons | | 3.45.1 | 2018-09-05 | updated documentation | | 3.45.0 | 2018-09-04 | `diskLayout()` added smartStatus | diff --git a/lib/filesystem.js b/lib/filesystem.js index 9f74ca1..b6f6f06 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -841,7 +841,7 @@ function diskLayout(callback) { const status = util.getValue(lines, 'Status', '=').trim(); if (size) { result.push({ - type: device.indexOf('SSD') > -1 ? 'SSD' : 'HD', // not really correct(!) ... maybe this one is better: MSFT_PhysicalDisk - Media Type?? + type: device.indexOf('SSD') > -1 ? 'SSD' : 'HD', // just a starting point ... better: MSFT_PhysicalDisk - Media Type ... see below name: util.getValue(lines, 'Caption', '='), vendor: util.getValue(lines, 'Manufacturer', '='), size: parseInt(size), @@ -859,11 +859,45 @@ function diskLayout(callback) { }); } }); + util.powerShell('Get-PhysicalDisk | Format-List') + .then(data => { + let devices = data.split(/\n\s*\n/); + devices.forEach(function (device) { + let lines = device.split('\r\n'); + const serialNum = util.getValue(lines, 'SerialNumber', ':').trim(); + const name = util.getValue(lines, 'FriendlyName', ':').trim(); + const size = util.getValue(lines, 'Size', ':').trim(); + let mediaType = util.getValue(lines, 'MediaType', ':').trim(); + if (mediaType === '3' || mediaType === 'HDD') { mediaType = 'HD'; } + if (mediaType === '4') { mediaType = 'SSD'; } + if (mediaType === '5') { mediaType = 'SCM'; } + if (size) { + let i = util.findObjectByKey(result, 'serialNum', serialNum); + if (i === -1) { + i = util.findObjectByKey(result, 'name', name); + } + if (i != -1) { + result[i].type = mediaType; + } + } + }); + if (callback) { + callback(result); + } + resolve(result); + }) + .catch(() => { + if (callback) { + callback(result); + } + resolve(result); + }); + } else { + if (callback) { + callback(result); + } + resolve(result); } - if (callback) { - callback(result); - } - resolve(result); }); } catch (e) { if (callback) { callback(result); } diff --git a/lib/util.js b/lib/util.js index bd894b2..7d548a2 100644 --- a/lib/util.js +++ b/lib/util.js @@ -14,6 +14,7 @@ const os = require('os'); const fs = require('fs'); +const { spawn } = require('child_process'); let _cores = 0; let wmic = ''; @@ -124,6 +125,15 @@ function parseDateTime(dt) { return result; } +function findObjectByKey(array, key, value) { + for (let i = 0; i < array.length; i++) { + if (array[i][key] === value) { + return i; + } + } + return -1; +} + function getWmic() { if (os.type() === 'Windows_NT' && !wmic) { if (fs.existsSync(process.env.WINDIR + '\\system32\\wbem\\wmic.exe')) { @@ -133,6 +143,32 @@ function getWmic() { return wmic; } +function powerShell(cmd) { + return new Promise((resolve, reject) => { + process.nextTick(() => { + + let result = ''; + const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], { + stdio: 'pipe' + }); + + child.stdout.on('data', function (data) { + result = result + data.toString('utf8'); + }); + child.stderr.on('data', function (data) { + reject(data); + }); + child.on('close', function () { + resolve(result); + }); + + child.stdin.write(cmd + '\n'); + child.stdin.end(); + }); + }); +} + + function noop() { } exports.isFunction = isFunction; @@ -142,5 +178,7 @@ exports.cores = cores; exports.getValue = getValue; exports.decodeEscapeSequence = decodeEscapeSequence; exports.parseDateTime = parseDateTime; +exports.findObjectByKey = findObjectByKey; exports.getWmic = getWmic; +exports.powerShell = powerShell; exports.noop = noop;