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": { "dependencies": {
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"iconv-lite": "^0.6.3",
"node-schedule": "^2.1.0", "node-schedule": "^2.1.0",
"request": "^2.88.2" "request": "^2.88.2"
} }
@ -291,6 +292,17 @@
"npm": ">=1.3.7" "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": { "node_modules/is-nan": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz", "resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz",
@ -791,6 +803,14 @@
"sshpk": "^1.7.0" "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": { "is-nan": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz", "resolved": "https://registry.npmmirror.com/is-nan/-/is-nan-1.3.2.tgz",

View File

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

View File

@ -1,21 +1,44 @@
// refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/ 'use strict';
var child_process = require('child_process');
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 = [ var cmds = [
'git status',
'dir', 'dir',
'git status',
]; ];
cmds.forEach(async function (cmd) { async function main() {
let result = await new Promise(function (resolve) { for (let cmd of cmds) {
child_process.exec(cmd, function (error, stdout, stderr) { let result = await new Promise(function (resolve) {
// 获取命令执行的输出 // refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
resolve({ child_process.exec(cmd, {
cmd: cmd, cwd: DATA_FOLDER, // 脚本执行目录
error: error, encoding: bufferEncoding
stdout: stdout, }, function (err, stdout, stderr) {
stderr: 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 request = require('request');
const fs = require('fs'); const fs = require('fs');
const { dirname } = require('path');
const path = 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); console.log("DATA_FOLDER", DATA_FOLDER);
// 请求微博热搜 APi 接口 // 请求微博热搜 APi 接口