新增quickcommand.runCode,quickcommand.runInTerminal支持warp

This commit is contained in:
fofolee
2025-01-13 01:15:42 +08:00
parent 4053f7a9c2
commit 0a8c24374a
9 changed files with 366 additions and 105 deletions

View File

@@ -159,7 +159,7 @@ export default {
this.childProcess = window.runCodeFile(
currentCommand.cmd,
this.getCommandOpt(currentCommand),
currentCommand.output === "terminal",
currentCommand.output === "terminal" ? {} : false,
(stdout, stderr) => this.handleResult(stdout, stderr, resultOpts)
);
this.listenStopSign();

View File

@@ -36,6 +36,7 @@ import ButtonBox from "components/quickcommandUI/ButtonBox";
import ConfirmBox from "components/quickcommandUI/ConfirmBox";
import TextArea from "components/quickcommandUI/TextArea";
import SelectList from "components/quickcommandUI/SelectList";
import programs from "js/options/programs";
export default {
components: {
@@ -256,8 +257,26 @@ export default {
});
},
};
// 将quickcommandUI添加到quickcommand
Object.assign(window.quickcommand, quickcommandUI);
// 获取用户数据
window.quickcommand.userData = this.$root.utools.userData;
// 添加runCode方法不在preload中加是因为programs写在了src中-_-
quickcommand.runCode = (code, program, runInTerminal = false) => {
return new Promise((reslove, reject) => {
window.runCodeFile(
code,
{ ...programs[program], charset: {}, scptarg: "" },
runInTerminal,
(result, err) => (err ? reject(err) : reslove(result))
);
false;
});
};
// 冻结quickcommand
Object.freeze(quickcommand);
},
methods: {

View File

@@ -17,7 +17,7 @@ import { imageCommands } from "./imageCommands";
import { windowsCommands } from "./windowsCommands";
import { statusCommands } from "./statusCommands";
import { macosCommands } from "./macosCommands";
import { scriptCommands } from "./scriptCommands";
let commands = [
fileCommands,
networkCommands,
@@ -30,6 +30,7 @@ let commands = [
dataCommands,
codingCommands,
controlCommands,
scriptCommands,
uiCommands,
simulateCommands,
mathCommands,

View File

@@ -0,0 +1,53 @@
export const scriptCommands = {
label: "编程相关",
icon: "integration_instructions",
commands: [
{
value: "",
label: "赋值",
icon: "script",
outputVariable: "value",
saveOutput: true,
config: [
{
label: "值或表达式",
component: "VariableInput",
width: 12,
},
],
},
{
value: "(function(code){new Function(code)()})",
label: "注入JS脚本",
icon: "script",
config: [
{
label: "JS脚本",
component: "CodeEditor",
width: 12,
},
],
},
{
value: "quickcommand.runAppleScript",
label: "执行 AppleScript",
icon: "script",
outputVariable: "result",
saveOutput: true,
config: [
{
label: "脚本",
component: "CodeEditor",
width: 12,
},
],
},
{
value: "quickcommand.runCsharp",
label: "执行C#脚本",
icon: "script",
outputVariable: "result",
saveOutput: true,
},
],
};

View File

@@ -378,11 +378,23 @@ interface quickcommandApi {
* 在终端运行,不支持 Linux
*
* @param command 要在终端运行的命令
* @param options 终端运行参数
* @param options.dir 运行目录
* @param options.windows 终端类型默认wt
* @param options.macos 终端类型默认warp
*
* ```js
* quickcommand.runInTerminal(`whoami`)
* ```
*/
runInTerminal(command: string);
runInTerminal(
command: string,
options?: {
dir?: string;
windows?: "wt" | "cmd";
macos?: "warp" | "iterm" | "terminal";
}
);
/**
* 对应 utools.onPluginEnter 的 `code` `type` 和 `payload`
@@ -597,6 +609,44 @@ interface quickcommandApi {
defaultText?: string,
title?: string
): Promise<string | null>;
/**
* 运行代码
* @param code 代码
* @param program 编程语言
* @param runInTerminal 终端运行参数,不传则不在终端运行
* @param runInTerminal.dir 运行目录
* @param runInTerminal.windows windows使用的终端默认wt
* @param runInTerminal.macos macos使用的终端默认warp
*
* 支持的编程语言:
* shell, applescript, cmd, python, powershell, javascript, ruby, php, lua, perl, csharp, c
*
* ```js
* quickcommand.runCode("print('Hello, World!');", "python");
* ```
*/
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";
}
): Promise<string>;
}
declare var quickcommand: quickcommandApi;