-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
37 lines (31 loc) · 1.16 KB
/
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
35
36
37
const { execFile, execFileSync } = require('child_process');
const iconv = require('iconv-lite');
const path = require('path');
const wmic = path.join(process.env.SystemRoot, 'System32', 'wbem', 'WMIC.exe');
const aliasList = execFileSync(wmic, ['ALIAS', 'LIST', 'BRIEF']);
const aliasLines = iconv.decode(aliasList, 'GB2312').split(/\r\n/);
const aliasItems = aliasLines.slice(1, aliasLines.length - 2).map(line => line.split(/\s{2,}/)[0]);
const data = {};
for (let aliasItem of aliasItems) {
data[aliasItem] = () =>
new Promise((resolve, reject) => {
execFile(wmic, [aliasItem, 'get', '/VALUE'], (err, stdout, stderr) => {
if (err || stderr) {
reject(err || stderr);
}
const group = stdout.trim().split(/[\r\r\n]{5,}/);
const jsonGroup = [];
for (const item of group) {
const list = item.split(/\r\r\n/);
const jsonItem = {};
for (const d of list) {
const eqPos = d.indexOf('=');
jsonItem[d.slice(0, eqPos)] = d.slice(eqPos + 1);
}
jsonGroup.push(jsonItem);
}
resolve(jsonGroup);
});
});
}
module.exports = data;