mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-28 20:02:44 +08:00
重构 runCode 方法:将参数由 program 改为 options,支持更多配置选项,包括编程语言、脚本参数和编码设置,同时更新 API 文档
This commit is contained in:
parent
e574471689
commit
dcd392ba47
@ -266,12 +266,55 @@ export default {
|
||||
// 获取用户数据
|
||||
window.quickcommand.userData = this.$root.utools.userData;
|
||||
|
||||
// 添加runCode方法,不在preload中加是因为programs写在了src中-_-
|
||||
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))
|
||||
);
|
||||
|
71
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
71
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
@ -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-8或是gbk
|
||||
* @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>;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user