'use strict'; const child_process = require('child_process'); const iconv = require("iconv-lite"); const encoding = "cp936"; const bufferEncoding = "binary"; async function execute(rootPath, cmds) { let outputs = []; 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, { cwd: rootPath, // 脚本执行目录 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), }); } }); }); outputs.push(result); } return outputs; } exports.execute = execute;