增加使用 jschardet 对输出进行编码检测

This commit is contained in:
fofolee
2019-05-16 11:16:43 +08:00
parent 26e5e7f877
commit 6f672b22fb
59 changed files with 18286 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
const os = require('os')
const iconv = require('iconv-lite')
const { spawn, exec } = require("child_process")
const jschardet = require("jschardet")
//-------checkUpdate------
const fs = require('fs')
@@ -30,17 +31,20 @@ totalMem = os.totalmem();
powershell = (cmd, callback) => {
const ps = spawn('powershell', ['-NoProfile', '-Command', cmd], { encoding: 'buffer' })
let chunks = [];
let err_chunks = [];
let chunks = [], err_chunks = [], size = 0, err_size = 0;
ps.stdout.on('data', chunk => {
chunks.push(iconv.decode(chunk, 'cp936'))
chunks.push(chunk);
size += chunk.length;
})
ps.stderr.on('data', err_chunk => {
err_chunks.push(iconv.decode(err_chunk, 'cp936'))
err_chunks.push(err_chunk);
err_size += err_chunk.length;
})
ps.on('close', code => {
let stdout = chunks.join("");
let stderr = err_chunks.join("");
let stdout = Buffer.concat(chunks, size);
stdout = stdout.length ? iconv.decode(stdout, jschardet.detect(stdout).encoding) : '';
let stderr = Buffer.concat(err_chunks, err_size);
stderr = stderr.length ? iconv.decode(stderr, jschardet.detect(stderr).encoding) : '';
callback(stdout, stderr)
})
}
@@ -94,8 +98,4 @@ taskkill = (task, path, callback) => {
callback(stderr);
});
}
}
tasklist(l => {
console.log(l);
})
}