1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

执行命令行代码逻辑优化;解决中文乱码问题

This commit is contained in:
程序员小墨 2022-07-24 00:35:31 +08:00
parent 766658ea11
commit 63c5491101
5 changed files with 61 additions and 18 deletions

View File

20
package-lock.json generated
View File

@ -10,6 +10,7 @@
"dependencies": {
"dotenv": "^16.0.1",
"fs": "^0.0.1-security",
"iconv-lite": "^0.6.3",
"node-schedule": "^2.1.0",
"request": "^2.88.2"
}
@ -291,6 +292,17 @@
"npm": ">=1.3.7"
}
},
"node_modules/iconv-lite": {
"version": "0.6.3",
"resolved": "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-nan": {
"version": "1.3.2",
"resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz",
@ -791,6 +803,14 @@
"sshpk": "^1.7.0"
}
},
"iconv-lite": {
"version": "0.6.3",
"resolved": "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
}
},
"is-nan": {
"version": "1.3.2",
"resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz",

View File

@ -9,6 +9,7 @@
"dependencies": {
"dotenv": "^16.0.1",
"fs": "^0.0.1-security",
"iconv-lite": "^0.6.3",
"node-schedule": "^2.1.0",
"request": "^2.88.2"
}

View File

@ -1,21 +1,44 @@
// refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
var child_process = require('child_process');
'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');
var cmds = [
'git status',
'dir',
'git status',
];
cmds.forEach(async function (cmd) {
let result = await new Promise(function (resolve) {
child_process.exec(cmd, function (error, stdout, stderr) {
// 获取命令执行的输出
resolve({
cmd: cmd,
error: error,
stdout: stdout,
stderr: stderr,
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),
});
}
});
});
});
console.log(result);
});
console.log(result);
}
}
main();

View File

@ -2,10 +2,9 @@
const request = require('request');
const fs = require('fs');
const { dirname } = require('path');
const path = require('path');
const DATA_FOLDER = path.join(dirname(__dirname), process.env.DATA_FOLDER ?? 'data');
const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data');
console.log("DATA_FOLDER", DATA_FOLDER);
// 请求微博热搜 APi 接口