2022-07-24 00:35:31 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const iconv = require("iconv-lite");
|
|
|
|
|
|
|
|
const encoding = "cp936";
|
|
|
|
const bufferEncoding = "binary";
|
|
|
|
|
2022-07-24 01:01:23 +08:00
|
|
|
async function execute(rootPath, cmds) {
|
|
|
|
let outputs = [];
|
2022-07-24 00:35:31 +08:00
|
|
|
for (let cmd of cmds) {
|
|
|
|
let result = await new Promise(function (resolve) {
|
|
|
|
// refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
|
|
|
|
child_process.exec(cmd, {
|
2022-07-24 01:01:23 +08:00
|
|
|
cwd: rootPath, // 脚本执行目录
|
2022-07-24 00:35:31 +08:00
|
|
|
encoding: bufferEncoding
|
|
|
|
}, function (err, stdout, stderr) {
|
|
|
|
if (err) {
|
|
|
|
resolve({
|
|
|
|
cmd: cmd,
|
|
|
|
err: err,
|
|
|
|
// err_stack: iconv.decode(Buffer.from(err.stack, bufferEncoding), encoding),
|
|
|
|
// err_message: iconv.decode(Buffer.from(err.message, bufferEncoding), encoding),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// 获取命令执行的输出
|
|
|
|
resolve({
|
|
|
|
cmd: cmd,
|
|
|
|
stdout: iconv.decode(Buffer.from(stdout, bufferEncoding), encoding),
|
|
|
|
stderr: iconv.decode(Buffer.from(stderr, bufferEncoding), encoding),
|
|
|
|
});
|
|
|
|
}
|
2022-07-23 23:19:13 +08:00
|
|
|
});
|
|
|
|
});
|
2022-07-24 01:01:23 +08:00
|
|
|
outputs.push(result);
|
2022-07-24 00:35:31 +08:00
|
|
|
}
|
2022-07-24 01:01:23 +08:00
|
|
|
return outputs;
|
2022-07-24 00:35:31 +08:00
|
|
|
}
|
2022-07-24 01:01:23 +08:00
|
|
|
|
|
|
|
exports.execute = execute;
|