From 63c54911014c059eb99cb0647969126258e21e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sun, 24 Jul 2022 00:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96=EF=BC=9B?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/.gitkeep | 0 package-lock.json | 20 ++++++++++++++++ package.json | 3 ++- src/execute_command.js | 53 ++++++++++++++++++++++++++++++------------ src/get_hotband.js | 3 +-- 5 files changed, 61 insertions(+), 18 deletions(-) delete mode 100644 data/.gitkeep diff --git a/data/.gitkeep b/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/package-lock.json b/package-lock.json index e509250..44ef4cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index bcf7de7..af61168 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "dependencies": { "dotenv": "^16.0.1", "fs": "^0.0.1-security", + "iconv-lite": "^0.6.3", "node-schedule": "^2.1.0", "request": "^2.88.2" } -} \ No newline at end of file +} diff --git a/src/execute_command.js b/src/execute_command.js index 557016f..23fec0e 100644 --- a/src/execute_command.js +++ b/src/execute_command.js @@ -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); -}); \ No newline at end of file + console.log(result); + } +} +main(); \ No newline at end of file diff --git a/src/get_hotband.js b/src/get_hotband.js index abceba1..8b5d31d 100644 --- a/src/get_hotband.js +++ b/src/get_hotband.js @@ -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 接口