mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-09-23 20:44:42 +08:00
完善编排执行命令功能
This commit is contained in:
@@ -2,6 +2,7 @@ const quickcomposer = {
|
||||
textProcessor: require("./quickcomposer/textProcessor"),
|
||||
simulate: require("./quickcomposer/simulate"),
|
||||
file: require("./quickcomposer/file"),
|
||||
system: require("./quickcomposer/system"),
|
||||
};
|
||||
|
||||
module.exports = quickcomposer;
|
||||
|
48
plugin/lib/quickcomposer/system/exec.js
Normal file
48
plugin/lib/quickcomposer/system/exec.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const { execSync } = require("child_process");
|
||||
const iconv = require("iconv-lite");
|
||||
const os = require("os");
|
||||
|
||||
function getSystemEncoding() {
|
||||
// Windows 默认使用 GBK/GB2312,其他系统默认 UTF-8
|
||||
return os.platform() === "win32" ? "gbk" : "utf8";
|
||||
}
|
||||
|
||||
function exec(command, options = {}) {
|
||||
try {
|
||||
const {
|
||||
autoEncoding = true,
|
||||
encoding = "buffer",
|
||||
windowsHide = true,
|
||||
...execOptions
|
||||
} = options;
|
||||
|
||||
// 执行命令,总是使用 buffer 获取原始输出
|
||||
const output = execSync(command, {
|
||||
...execOptions,
|
||||
encoding: "buffer",
|
||||
windowsHide,
|
||||
});
|
||||
|
||||
// 如果设置了自动编码,根据系统编码解码
|
||||
if (autoEncoding) {
|
||||
return iconv.decode(output, getSystemEncoding());
|
||||
}
|
||||
|
||||
// 如果手动设置了编码且不是 buffer,则按指定编码解码
|
||||
if (encoding !== "buffer") {
|
||||
return iconv.decode(output, encoding);
|
||||
}
|
||||
|
||||
// 返回原始 buffer
|
||||
return output;
|
||||
} catch (error) {
|
||||
// 如果是执行错误,尝试解码错误信息
|
||||
if (error.stderr) {
|
||||
const stderr = iconv.decode(error.stderr, getSystemEncoding());
|
||||
error.message = stderr;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exec;
|
5
plugin/lib/quickcomposer/system/index.js
Normal file
5
plugin/lib/quickcomposer/system/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const exec = require("./exec");
|
||||
|
||||
module.exports = {
|
||||
exec,
|
||||
};
|
Reference in New Issue
Block a user