重构 runCode 方法:将参数由 program 改为 options,支持更多配置选项,包括编程语言、脚本参数和编码设置,同时更新 API 文档

This commit is contained in:
fofolee 2025-01-24 23:53:57 +08:00
parent e574471689
commit dcd392ba47
2 changed files with 94 additions and 26 deletions

View File

@ -266,12 +266,55 @@ export default {
//
window.quickcommand.userData = this.$root.utools.userData;
// runCodepreloadprogramssrc-_-
quickcommand.runCode = (code, program, runInTerminal = false) => {
/**
* 执行代码
* 添加runCode方法不在preload中加是因为programs写在了src中-_-
* @param code 代码文本
* @param options 选项
* @param options.language 编程语言
* @param options.args 脚本参数
* @param options.charset 编码
* @param options.charset.scriptCode 脚本编码
* @param options.charset.outputCode 输出编码
* @param options.runInTerminal 终端运行参数不传则不在终端运行
* @param options.runInTerminal.dir 运行目录
* @param options.runInTerminal.windows windows使用的终端默认wt
* @param options.runInTerminal.macos macos使用的终端默认warp
*/
quickcommand.runCode = (code, options) => {
return new Promise((reslove, reject) => {
const isWin = window.utools.isWindows();
const {
language = isWin ? "cmd" : "shell",
charset = {},
args = [],
runInTerminal,
} = options;
const unescapeAndQuote = (str) => `"${str.replace(/\\"/g, '"')}"`;
if (!programs[language]) {
return reject(new Error(`不支持的语言: ${language}`));
}
if (!Array.isArray(args)) {
return reject(new TypeError(`args 应为 Array, 而非 ${typeof args}`));
}
const argsStr = args.map(unescapeAndQuote).join(" ");
const defaultCharset =
isWin && ["cmd", "powershell"].includes(language) ? "gbk" : "utf-8";
const { scriptCode = defaultCharset, outputCode = defaultCharset } =
charset;
window.runCodeFile(
code,
{ ...programs[program], charset: {}, scptarg: "" },
{
...programs[language],
charset: { scriptCode, outputCode },
scptarg: argsStr,
},
runInTerminal,
(result, err) => (err ? reject(err) : reslove(result))
);

View File

@ -652,38 +652,63 @@ interface quickcommandApi {
/**
*
* @param code
* @param program
* @param runInTerminal
* @param runInTerminal.dir
* @param runInTerminal.windows windows使用的终端wt
* @param runInTerminal.macos macos使用的终端warp
* @param options
* @param options.language cmd或是shell
* @param options.args
* @param options.charset utf-8gbk
* @param options.charset.scriptCode
* @param options.charset.outputCode
* @param options.runInTerminal
* @param options.runInTerminal.dir
* @param options.runInTerminal.windows windows使用的终端wt
* @param options.runInTerminal.macos macos使用的终端warp
*
*
* shell, applescript, cmd, python, powershell, javascript, ruby, php, lua, perl, csharp, c
*
* ```js
* quickcommand.runCode("print('Hello, World!');", "python");
* const script = `
* import sys
* print(sys.argv[1])
* `
* const options = {
* language: "python",
* args: ["hello world"]
* }
* quickcommand.runCode(script, options).then(result => {
* console.log(result)
* }).catch(e => {
* console.log(e)
* })
* ```
*/
runCode(
code: string,
program:
| "shell"
| "applescript"
| "cmd"
| "python"
| "powershell"
| "javascript"
| "ruby"
| "php"
| "lua"
| "perl"
| "csharp"
| "c",
runInTerminal?: {
dir?: string;
windows?: "wt" | "cmd";
macos?: "warp" | "iterm" | "terminal";
options: {
language:
| "shell"
| "applescript"
| "cmd"
| "python"
| "powershell"
| "javascript"
| "ruby"
| "php"
| "lua"
| "perl"
| "csharp"
| "c";
args?: string[];
charset?: {
scriptCode?: string;
outputCode?: string;
};
runInTerminal?: {
dir?: string;
windows?: "wt" | "cmd";
macos?: "warp" | "iterm" | "terminal";
};
}
): Promise<string>;
}