mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-29 12:22: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;
|
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) => {
|
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(
|
window.runCodeFile(
|
||||||
code,
|
code,
|
||||||
{ ...programs[program], charset: {}, scptarg: "" },
|
{
|
||||||
|
...programs[language],
|
||||||
|
charset: { scriptCode, outputCode },
|
||||||
|
scptarg: argsStr,
|
||||||
|
},
|
||||||
runInTerminal,
|
runInTerminal,
|
||||||
(result, err) => (err ? reject(err) : reslove(result))
|
(result, err) => (err ? reject(err) : reslove(result))
|
||||||
);
|
);
|
||||||
|
41
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
41
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
@ -652,22 +652,41 @@ interface quickcommandApi {
|
|||||||
/**
|
/**
|
||||||
* 运行代码
|
* 运行代码
|
||||||
* @param code 代码
|
* @param code 代码
|
||||||
* @param program 编程语言
|
* @param options 选项
|
||||||
* @param runInTerminal 终端运行参数,不传则不在终端运行
|
* @param options.language 编程语言,不传时则根据操作系统选择cmd或是shell
|
||||||
* @param runInTerminal.dir 运行目录
|
* @param options.args 脚本参数
|
||||||
* @param runInTerminal.windows windows使用的终端,默认wt
|
* @param options.charset 编码,不传时则根据操作系统及语言选择utf-8或是gbk
|
||||||
* @param runInTerminal.macos macos使用的终端,默认warp
|
* @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
|
* shell, applescript, cmd, python, powershell, javascript, ruby, php, lua, perl, csharp, c
|
||||||
*
|
*
|
||||||
* ```js
|
* ```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(
|
runCode(
|
||||||
code: string,
|
code: string,
|
||||||
program:
|
options: {
|
||||||
|
language:
|
||||||
| "shell"
|
| "shell"
|
||||||
| "applescript"
|
| "applescript"
|
||||||
| "cmd"
|
| "cmd"
|
||||||
@ -679,11 +698,17 @@ interface quickcommandApi {
|
|||||||
| "lua"
|
| "lua"
|
||||||
| "perl"
|
| "perl"
|
||||||
| "csharp"
|
| "csharp"
|
||||||
| "c",
|
| "c";
|
||||||
|
args?: string[];
|
||||||
|
charset?: {
|
||||||
|
scriptCode?: string;
|
||||||
|
outputCode?: string;
|
||||||
|
};
|
||||||
runInTerminal?: {
|
runInTerminal?: {
|
||||||
dir?: string;
|
dir?: string;
|
||||||
windows?: "wt" | "cmd";
|
windows?: "wt" | "cmd";
|
||||||
macos?: "warp" | "iterm" | "terminal";
|
macos?: "warp" | "iterm" | "terminal";
|
||||||
|
};
|
||||||
}
|
}
|
||||||
): Promise<string>;
|
): Promise<string>;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user