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 pnpm dev
``` ```
## Prettier
```sh
pnpm run -r format
```
## Dist ## Dist
```sh ```sh

View file

@ -111,14 +111,19 @@ function newInstallProgressNotifier(full_name: string, notifyMainWindow: MainWin
}; };
} }
export async function openTerminal(cmd: string) { export async function openPackageEntrypointInTerminal(pkg: string) {
let scriptPath = ""; 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 { try {
// TODO SECURITY: escape the cmd if possible or create whitelist of acceptable commands let stdout = "";
scriptPath = await createCommandScriptFile(cmd); let stderr = "";
if (!scriptPath) throw new Error("unable to create Applse Script");
let stdout = ``;
let stderr = ``;
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
const child = spawn("/usr/bin/osascript", [scriptPath]); const child = spawn("/usr/bin/osascript", [scriptPath]);
@ -129,50 +134,37 @@ export async function openTerminal(cmd: string) {
stderr += data.toString().trim(); stderr += data.toString().trim();
}); });
child.on("exit", () => { child.on("exit", (code) => {
console.log("exit:", stdout); console.info("exit:", code, `\`${stdout}\``);
resolve(stdout); if (code == 0) {
resolve(stdout);
} else {
reject(new Error("failed to open terminal and run tea sh"));
}
}); });
child.on("error", () => { child.on("error", () => {
reject(new Error(stderr)); reject(new Error(stderr));
}); });
}); });
} catch (error) {
log.error("openTerminal:", error);
} finally { } finally {
if (scriptPath) await fs.unlinkSync(scriptPath); if (scriptPath) await fs.unlinkSync(scriptPath);
} }
} }
const createCommandScriptFile = async (cmd: string): Promise<string> => { const createCommandScriptFile = async (cmd: string): Promise<string> => {
try { const guiFolder = getGuiPath();
const guiFolder = getGuiPath(); const tmpFilePath = path.join(guiFolder, `${+new Date()}.scpt`);
const tmpFilePath = path.join(guiFolder, `${+new Date()}.scpt`); const command = `${cmd.replace(/"/g, '\\"')}`;
const command = `"${cmd.replace(/"/g, '\\"')}"`; const script = `
const script = ` tell application "Terminal"
tell application "iTerm" activate
activate do script "${command}"
if application "iTerm" is running then end tell
try `.trim();
tell the first window to create tab with default profile
on error
create window with default profile
end try
end if
delay 0.1 await fs.writeFileSync(tmpFilePath, script, "utf-8");
return tmpFilePath;
tell the first window to tell current session to write text ${command}
end tell
`.trim();
await fs.writeFileSync(tmpFilePath, script, "utf-8");
return tmpFilePath;
} catch (error) {
log.error(error);
return "";
}
}; };
export async function asyncExec(cmd: string): Promise<string> { 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 type { Packages, Session } from "../../src/libs/types";
import log from "./logger"; import log from "./logger";
import { syncLogsAt } from "./v1-client"; import { syncLogsAt } from "./v1-client";
import { installPackage, openTerminal, syncPantry } from "./cli"; import { installPackage, openPackageEntrypointInTerminal, syncPantry } from "./cli";
import initializeTeaCli from "./initialize"; import initializeTeaCli from "./initialize";
@ -77,12 +77,12 @@ export default function initializeHandlers({ notifyMainWindow }: HandlerOptions)
}); });
ipcMain.handle("open-terminal", async (_, data) => { ipcMain.handle("open-terminal", async (_, data) => {
const { cmd } = data as { cmd: string }; const { pkg } = data as { pkg: string };
try { try {
// TODO: detect if mac or linux // TODO: detect if mac or linux
// current openTerminal is only design for Mac // current openTerminal is only design for Mac
log.info("open terminal w/ cmd:", cmd); log.info("open tea entrypoint in terminal for pkg:", pkg);
await openTerminal(cmd); await openPackageEntrypointInTerminal(pkg);
} catch (error) { } catch (error) {
log.error(error); log.error(error);
} }

View file

@ -10,11 +10,13 @@
import type { GUIPackage } from "$libs/types"; import type { GUIPackage } from "$libs/types";
import { packagesStore } from "$libs/stores"; import { packagesStore } from "$libs/stores";
import { shellOpenExternal } from "@native"; import { openPackageEntrypointInTerminal, shellOpenExternal } from "@native";
import { findAvailableVersions, findRecentInstalledVersion } from "$libs/packages/pkg-utils"; import { findAvailableVersions, findRecentInstalledVersion } from "$libs/packages/pkg-utils";
import { trimGithubSlug } from "$libs/github"; import { trimGithubSlug } from "$libs/github";
import PackageImage from "../package-card/bg-image.svelte"; import PackageImage from "../package-card/bg-image.svelte";
import PackageVersionSelector from "$components/package-install-button/package-version-selector.svelte"; import PackageVersionSelector from "$components/package-install-button/package-version-selector.svelte";
import { isPackageInstalled } from "$libs/native-mock";
export let pkg: GUIPackage; export let pkg: GUIPackage;
let installing = false; let installing = false;
@ -151,6 +153,22 @@
<div class="icon-github text-gray flex text-xl group-hover:text-black" /> <div class="icon-github text-gray flex text-xl group-hover:text-black" />
</button> </button>
{/if} {/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> </menu>
</article> </article>
</header> </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 { try {
ipcRenderer.invoke("open-terminal", { cmd }); ipcRenderer.invoke("open-terminal", { pkg });
} catch (error) { } catch (error) {
log.error(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> => { export const getAutoUpdateStatus = async (): Promise<AutoUpdateStatus> => {
return { status: "up-to-date" }; return { status: "up-to-date" };
}; };
export async function openPackageEntrypointInTerminal(pkg: string) {
//noop
}