From 7e9525aad68de26eb0f2558d822272e815871cf9 Mon Sep 17 00:00:00 2001 From: Phil Rzewski Date: Wed, 7 Feb 2024 17:55:35 +0000 Subject: [PATCH] Use Electron's shell.openpath() for pcap slices (#2992) --- apps/zui/src/domain/env/plugin-api.ts | 5 - apps/zui/src/electron/ops/open-link-op.ts | 4 +- apps/zui/src/js/components/Login.test.tsx | 12 +- apps/zui/src/js/lib/open.ts | 139 ------------------ .../src/plugins/brimcap/packets/download.ts | 5 +- 5 files changed, 10 insertions(+), 155 deletions(-) delete mode 100644 apps/zui/src/js/lib/open.ts diff --git a/apps/zui/src/domain/env/plugin-api.ts b/apps/zui/src/domain/env/plugin-api.ts index dd6b7df918..b0edb7217e 100644 --- a/apps/zui/src/domain/env/plugin-api.ts +++ b/apps/zui/src/domain/env/plugin-api.ts @@ -1,6 +1,5 @@ import env from "src/app/core/env" import {getPath} from "src/js/api/core/get-path" -import open from "src/js/lib/open" import {join} from "path" export class EnvApi { @@ -20,10 +19,6 @@ export class EnvApi { return env.isTest } - openExternal(uri: string, opts: {newWindow?: boolean} = {}) { - return open(uri, opts) - } - getExePath(relPath: string) { return join(getPath("zdeps"), relPath) + (env.isWindows ? ".exe" : "") } diff --git a/apps/zui/src/electron/ops/open-link-op.ts b/apps/zui/src/electron/ops/open-link-op.ts index 76c9111d3b..1d9c836d1a 100644 --- a/apps/zui/src/electron/ops/open-link-op.ts +++ b/apps/zui/src/electron/ops/open-link-op.ts @@ -1,6 +1,6 @@ import {createOperation} from "../../core/operations" -import open from "src/js/lib/open" +import {shell} from "electron" export const openLinkOp = createOperation("openLinkOp", (_ctx, url: string) => { - open(url) + shell.openExternal(url) }) diff --git a/apps/zui/src/js/components/Login.test.tsx b/apps/zui/src/js/components/Login.test.tsx index cee3714f4e..ccdd209cbc 100644 --- a/apps/zui/src/js/components/Login.test.tsx +++ b/apps/zui/src/js/components/Login.test.tsx @@ -10,9 +10,7 @@ import Login from "./Login" import {rest} from "msw" import LakeStatuses from "../state/LakeStatuses" import lake from "../models/lake" -import open from "src/js/lib/open" - -jest.mock("src/js/lib/open", () => jest.fn()) +import {shell} from "electron" const system = new SystemTest("Login.test.ts") @@ -76,12 +74,12 @@ function getLake() { } async function expectBrowserToOpen() { - await waitFor(() => - expect(open).toHaveBeenCalledWith( + await waitFor(() => { + expect(shell.openExternal).toHaveBeenCalledWith( expect.stringContaining("http://test.com/authorize?") ) - ) - const auth0Url = (open as jest.Mock).mock.calls[0][0] + }) + const auth0Url = (shell.openExternal as jest.Mock).mock.calls[0][0] const urlParts = url.parse(auth0Url, true) return urlParts.query.state } diff --git a/apps/zui/src/js/lib/open.ts b/apps/zui/src/js/lib/open.ts deleted file mode 100644 index b8db084e1e..0000000000 --- a/apps/zui/src/js/lib/open.ts +++ /dev/null @@ -1,139 +0,0 @@ -"use strict" - -import env from "src/app/core/env" - -const isWsl = require("is-wsl") - -const {promisify} = require("util") -const childProcess = require("child_process") -const path = require("path") - -const pExecFile = promisify(childProcess.execFile) - -// Convert a path from WSL format to Windows format: -// `/mnt/c/Program Files/Example/MyApp.exe` → `C:\Program Files\Example\MyApp.exe`` -const wslToWindowsPath = async (path) => { - const {stdout} = await pExecFile("wslpath", ["-w", path]) - return stdout.trim() -} - -type Opts = { - app?: string | string[] - newWindow?: boolean - wait?: boolean -} - -type ChildProcessOpts = { - stdio?: string - detached?: boolean -} - -export default async function open(target: string, options?: Opts) { - if (typeof target !== "string") { - throw new TypeError("Expected a `target`") - } - - options = { - wait: false, - newWindow: false, - ...options, - } - - let command - let appArguments = [] - const cliArguments = [] - const childProcessOptions: ChildProcessOpts = {} - - if (Array.isArray(options.app)) { - appArguments = options.app.slice(1) - - options.app = options.app[0] - } - - if (env.isMac) { - command = "open" - - if (options.newWindow) { - cliArguments.push("-n") - } - - if (options.wait) { - cliArguments.push("-W") - } - - if (options.app) { - cliArguments.push("-a", options.app) - } - } else if (env.isWindows || isWsl) { - command = "cmd" + (isWsl ? ".exe" : "") - cliArguments.push("/c", "start", '""', "/b") - target = target.replace(/&/g, "^&") - - if (options.wait) { - cliArguments.push("/wait") - } - - if (options.app) { - if (isWsl && options.app.startsWith("/mnt/")) { - const windowsPath = await wslToWindowsPath(options.app) - options.app = windowsPath - } - - cliArguments.push(options.app) - } - - if (appArguments.length > 0) { - cliArguments.push(...appArguments) - } - } else { - if (options.app) { - command = options.app - } else { - const useSystemXdgOpen = - process.versions.electron || process.platform === "android" - command = useSystemXdgOpen ? "xdg-open" : path.join(__dirname, "xdg-open") - } - - if (appArguments.length > 0) { - cliArguments.push(...appArguments) - } - - if (!options.wait) { - // `xdg-open` will block the process unless stdio is ignored - // and it's detached from the parent even if it's unref'd. - childProcessOptions.stdio = "ignore" - childProcessOptions.detached = true - } - } - - cliArguments.push(target) - - if (env.isMac && appArguments.length > 0) { - cliArguments.push("--args", ...appArguments) - } - - const subprocess = childProcess.spawn( - command, - cliArguments, - childProcessOptions - ) - - if (options.wait) { - return new Promise((resolve, reject) => { - subprocess.once("error", reject) - - subprocess.once("close", (exitCode) => { - if (exitCode > 0) { - reject(new Error(`Exited with code ${exitCode}`)) - return - } - - resolve(subprocess) - }) - }) - } - - subprocess.unref() - - return subprocess -} diff --git a/apps/zui/src/plugins/brimcap/packets/download.ts b/apps/zui/src/plugins/brimcap/packets/download.ts index 86d19357d9..678684d5da 100644 --- a/apps/zui/src/plugins/brimcap/packets/download.ts +++ b/apps/zui/src/plugins/brimcap/packets/download.ts @@ -2,9 +2,10 @@ import * as zed from "@brimdata/zed-js" import {join} from "path" import {createCli} from "../cli" import os from "os" -import {window, env, commands} from "src/zui" +import {window, commands} from "src/zui" import {queryForConnLog} from "./query-conn-log" import {DOWNLOAD} from "./types" +import {shell} from "electron" function getSearchArgsFromConn(conn: zed.Record) { const dur = conn.try("duration") as zed.Duration @@ -38,7 +39,7 @@ export async function downloadPackets(root: string, pool: string, uid: string) { const msg = JSON.parse(err)?.error || `brimcap search failed: ${err}` window.showErrorMessage(msg) } else { - env.openExternal(dest) + shell.openPath(dest) window.showSuccessMessage("Packets extracted. Opening...") } }