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."));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user