2022-07-24 00:35:31 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const iconv = require("iconv-lite");
|
|
|
|
|
|
|
|
const encoding = "cp936";
|
|
|
|
const bufferEncoding = "binary";
|
|
|
|
const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data');
|
|
|
|
|
2022-07-23 23:19:13 +08:00
|
|
|
var cmds = [
|
|
|
|
'dir',
|
2022-07-24 00:35:31 +08:00
|
|
|
'git status',
|
2022-07-23 23:19:13 +08:00
|
|
|
];
|
|
|
|
|
2022-07-24 00:35:31 +08:00
|
|
|
async function main() {
|
|
|
|
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: DATA_FOLDER, // 脚本执行目录
|
|
|
|
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 00:35:31 +08:00
|
|
|
console.log(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
main();
|