feat(browser): copy extension path to clipboard
This commit is contained in:
@@ -2,7 +2,22 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const copyToClipboard = vi.fn();
|
||||
const runtime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("../infra/clipboard.js", () => ({
|
||||
copyToClipboard,
|
||||
}));
|
||||
|
||||
vi.mock("../runtime.js", () => ({
|
||||
defaultRuntime: runtime,
|
||||
}));
|
||||
|
||||
describe("browser extension install", () => {
|
||||
it("installs into the state dir (never node_modules)", async () => {
|
||||
@@ -16,4 +31,37 @@ describe("browser extension install", () => {
|
||||
expect(fs.existsSync(path.join(result.path, "manifest.json"))).toBe(true);
|
||||
expect(result.path.includes("node_modules")).toBe(false);
|
||||
});
|
||||
|
||||
it("copies extension path to clipboard", async () => {
|
||||
const prev = process.env.CLAWDBOT_STATE_DIR;
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-ext-path-"));
|
||||
process.env.CLAWDBOT_STATE_DIR = tmp;
|
||||
|
||||
try {
|
||||
copyToClipboard.mockReset();
|
||||
copyToClipboard.mockResolvedValue(true);
|
||||
runtime.log.mockReset();
|
||||
runtime.error.mockReset();
|
||||
runtime.exit.mockReset();
|
||||
|
||||
const dir = path.join(tmp, "browser", "chrome-extension");
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, "manifest.json"), JSON.stringify({ manifest_version: 3 }));
|
||||
|
||||
vi.resetModules();
|
||||
const { Command } = await import("commander");
|
||||
const { registerBrowserExtensionCommands } = await import("./browser-cli-extension.js");
|
||||
|
||||
const program = new Command();
|
||||
const browser = program.command("browser").option("--json", false);
|
||||
registerBrowserExtensionCommands(browser, (cmd) => cmd.parent?.opts?.() as { json?: boolean });
|
||||
|
||||
await program.parseAsync(["browser", "extension", "path"], { from: "user" });
|
||||
|
||||
expect(copyToClipboard).toHaveBeenCalledWith(dir);
|
||||
} finally {
|
||||
if (prev === undefined) delete process.env.CLAWDBOT_STATE_DIR;
|
||||
else process.env.CLAWDBOT_STATE_DIR = prev;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { Command } from "commander";
|
||||
|
||||
import { STATE_DIR_CLAWDBOT } from "../config/paths.js";
|
||||
import { danger, info } from "../globals.js";
|
||||
import { copyToClipboard } from "../infra/clipboard.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
import { movePathToTrash } from "../browser/trash.js";
|
||||
import { formatDocsLink } from "../terminal/links.js";
|
||||
@@ -76,9 +77,11 @@ export function registerBrowserExtensionCommands(
|
||||
return;
|
||||
}
|
||||
defaultRuntime.log(installed.path);
|
||||
const copied = await copyToClipboard(installed.path).catch(() => false);
|
||||
defaultRuntime.error(
|
||||
info(
|
||||
[
|
||||
copied ? "Copied to clipboard." : "Copy to clipboard unavailable.",
|
||||
"Next:",
|
||||
`- Chrome → chrome://extensions → enable “Developer mode”`,
|
||||
`- “Load unpacked” → select: ${installed.path}`,
|
||||
@@ -93,7 +96,7 @@ export function registerBrowserExtensionCommands(
|
||||
ext
|
||||
.command("path")
|
||||
.description("Print the path to the installed Chrome extension (load unpacked)")
|
||||
.action((_opts, cmd) => {
|
||||
.action(async (_opts, cmd) => {
|
||||
const parent = parentOpts(cmd);
|
||||
const dir = installedExtensionRootDir();
|
||||
if (!hasManifest(dir)) {
|
||||
@@ -112,5 +115,7 @@ export function registerBrowserExtensionCommands(
|
||||
return;
|
||||
}
|
||||
defaultRuntime.log(dir);
|
||||
const copied = await copyToClipboard(dir).catch(() => false);
|
||||
if (copied) defaultRuntime.error(info("Copied to clipboard."));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@ vi.mock("./onboard-helpers.js", () => ({
|
||||
detectBrowserOpenSupport: mocks.detectBrowserOpenSupport,
|
||||
openUrl: mocks.openUrl,
|
||||
formatControlUiSshHint: mocks.formatControlUiSshHint,
|
||||
}));
|
||||
|
||||
vi.mock("../infra/clipboard.js", () => ({
|
||||
copyToClipboard: mocks.copyToClipboard,
|
||||
}));
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { readConfigFileSnapshot, resolveGatewayPort } from "../config/config.js";
|
||||
import { copyToClipboard } from "../infra/clipboard.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
import {
|
||||
copyToClipboard,
|
||||
detectBrowserOpenSupport,
|
||||
formatControlUiSshHint,
|
||||
openUrl,
|
||||
|
||||
@@ -231,28 +231,6 @@ export async function openUrl(url: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function copyToClipboard(value: string): Promise<boolean> {
|
||||
const attempts: Array<{ argv: string[] }> = [
|
||||
{ argv: ["pbcopy"] },
|
||||
{ argv: ["xclip", "-selection", "clipboard"] },
|
||||
{ argv: ["wl-copy"] },
|
||||
{ argv: ["clip.exe"] }, // WSL / Windows
|
||||
{ argv: ["powershell", "-NoProfile", "-Command", "Set-Clipboard"] },
|
||||
];
|
||||
for (const attempt of attempts) {
|
||||
try {
|
||||
const result = await runCommandWithTimeout(attempt.argv, {
|
||||
timeoutMs: 3_000,
|
||||
input: value,
|
||||
});
|
||||
if (result.code === 0 && !result.killed) return true;
|
||||
} catch {
|
||||
// keep trying the next fallback
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function ensureWorkspaceAndSessions(
|
||||
workspaceDir: string,
|
||||
runtime: RuntimeEnv,
|
||||
|
||||
23
src/infra/clipboard.ts
Normal file
23
src/infra/clipboard.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { runCommandWithTimeout } from "../process/exec.js";
|
||||
|
||||
export async function copyToClipboard(value: string): Promise<boolean> {
|
||||
const attempts: Array<{ argv: string[] }> = [
|
||||
{ argv: ["pbcopy"] },
|
||||
{ argv: ["xclip", "-selection", "clipboard"] },
|
||||
{ argv: ["wl-copy"] },
|
||||
{ argv: ["clip.exe"] }, // WSL / Windows
|
||||
{ argv: ["powershell", "-NoProfile", "-Command", "Set-Clipboard"] },
|
||||
];
|
||||
for (const attempt of attempts) {
|
||||
try {
|
||||
const result = await runCommandWithTimeout(attempt.argv, {
|
||||
timeoutMs: 3_000,
|
||||
input: value,
|
||||
});
|
||||
if (result.code === 0 && !result.killed) return true;
|
||||
} catch {
|
||||
// keep trying the next fallback
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user