open button for projects (#497)

* open button for projects
* hardcoding needs fixing, I want this logic from the pantry. Currently that cannot be done in the gui
* styling needs some fixes
* needs the rebuilt sd-webui pkg (building as we speak)
This commit is contained in:
Max Howell 2023-04-25 15:41:31 -04:00 committed by GitHub
parent 4237766daa
commit c1c626cc80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 45 deletions

View file

@ -76,6 +76,12 @@ pnpm install
pnpm dev
```
## Prettier
```sh
pnpm run -r format
```
## Dist
```sh

View file

@ -111,14 +111,19 @@ function newInstallProgressNotifier(full_name: string, notifyMainWindow: MainWin
};
}
export async function openTerminal(cmd: string) {
let scriptPath = "";
export async function openPackageEntrypointInTerminal(pkg: string) {
let sh = `${cliBinPath} --sync --env=false +${pkg} `;
if (pkg == "github.com/AUTOMATIC1111/stable-diffusion-webui") {
sh += `~/.tea/${pkg}/v*/entrypoint.sh`;
} else {
sh += "sh";
}
const scriptPath = await createCommandScriptFile(sh);
try {
// TODO SECURITY: escape the cmd if possible or create whitelist of acceptable commands
scriptPath = await createCommandScriptFile(cmd);
if (!scriptPath) throw new Error("unable to create Applse Script");
let stdout = ``;
let stderr = ``;
let stdout = "";
let stderr = "";
await new Promise((resolve, reject) => {
const child = spawn("/usr/bin/osascript", [scriptPath]);
@ -129,50 +134,37 @@ export async function openTerminal(cmd: string) {
stderr += data.toString().trim();
});
child.on("exit", () => {
console.log("exit:", stdout);
resolve(stdout);
child.on("exit", (code) => {
console.info("exit:", code, `\`${stdout}\``);
if (code == 0) {
resolve(stdout);
} else {
reject(new Error("failed to open terminal and run tea sh"));
}
});
child.on("error", () => {
reject(new Error(stderr));
});
});
} catch (error) {
log.error("openTerminal:", error);
} finally {
if (scriptPath) await fs.unlinkSync(scriptPath);
}
}
const createCommandScriptFile = async (cmd: string): Promise<string> => {
try {
const guiFolder = getGuiPath();
const tmpFilePath = path.join(guiFolder, `${+new Date()}.scpt`);
const command = `"${cmd.replace(/"/g, '\\"')}"`;
const script = `
tell application "iTerm"
activate
if application "iTerm" is running then
try
tell the first window to create tab with default profile
on error
create window with default profile
end try
end if
delay 0.1
tell the first window to tell current session to write text ${command}
end tell
`.trim();
const guiFolder = getGuiPath();
const tmpFilePath = path.join(guiFolder, `${+new Date()}.scpt`);
const command = `${cmd.replace(/"/g, '\\"')}`;
const script = `
tell application "Terminal"
activate
do script "${command}"
end tell
`.trim();
await fs.writeFileSync(tmpFilePath, script, "utf-8");
return tmpFilePath;
} catch (error) {
log.error(error);
return "";
}
await fs.writeFileSync(tmpFilePath, script, "utf-8");
return tmpFilePath;
};
export async function asyncExec(cmd: string): Promise<string> {

View file

@ -4,7 +4,7 @@ import { readSessionData, writeSessionData } from "./auth";
import type { Packages, Session } from "../../src/libs/types";
import log from "./logger";
import { syncLogsAt } from "./v1-client";
import { installPackage, openTerminal, syncPantry } from "./cli";
import { installPackage, openPackageEntrypointInTerminal, syncPantry } from "./cli";
import initializeTeaCli from "./initialize";
@ -77,12 +77,12 @@ export default function initializeHandlers({ notifyMainWindow }: HandlerOptions)
});
ipcMain.handle("open-terminal", async (_, data) => {
const { cmd } = data as { cmd: string };
const { pkg } = data as { pkg: string };
try {
// TODO: detect if mac or linux
// current openTerminal is only design for Mac
log.info("open terminal w/ cmd:", cmd);
await openTerminal(cmd);
log.info("open tea entrypoint in terminal for pkg:", pkg);
await openPackageEntrypointInTerminal(pkg);
} catch (error) {
log.error(error);
}

View file

@ -10,11 +10,13 @@
import type { GUIPackage } from "$libs/types";
import { packagesStore } from "$libs/stores";
import { shellOpenExternal } from "@native";
import { openPackageEntrypointInTerminal, shellOpenExternal } from "@native";
import { findAvailableVersions, findRecentInstalledVersion } from "$libs/packages/pkg-utils";
import { trimGithubSlug } from "$libs/github";
import PackageImage from "../package-card/bg-image.svelte";
import PackageVersionSelector from "$components/package-install-button/package-version-selector.svelte";
import { isPackageInstalled } from "$libs/native-mock";
export let pkg: GUIPackage;
let installing = false;
@ -151,6 +153,22 @@
<div class="icon-github text-gray flex text-xl group-hover:text-black" />
</button>
{/if}
{#if pkg.installed_versions?.length}
<Button
class="h-10"
type="plain"
color="black"
onClick={() => {
openPackageEntrypointInTerminal(pkg.full_name);
}}>
{#if pkg.full_name == "github.com/AUTOMATIC1111/stable-diffusion-webui"}
OPEN
{:else}
OPEN IN TERMINAL
{/if}
</Button
>
{/if}
</menu>
</article>
</header>

View file

@ -194,9 +194,9 @@ export const updateSession = async (session: Partial<Session>) => {
}
};
export const openTerminal = (cmd: string) => {
export const openPackageEntrypointInTerminal = (pkg: string) => {
try {
ipcRenderer.invoke("open-terminal", { cmd });
ipcRenderer.invoke("open-terminal", { pkg });
} catch (error) {
log.error(error);
}

View file

@ -387,3 +387,7 @@ export const cacheImageURL = async (url: string): Promise<string | undefined> =>
export const getAutoUpdateStatus = async (): Promise<AutoUpdateStatus> => {
return { status: "up-to-date" };
};
export async function openPackageEntrypointInTerminal(pkg: string) {
//noop
}