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 接口