diff --git a/e2e/npmScripts.spec.ts b/e2e/npmScripts.spec.ts new file mode 100644 index 000000000..8d92c9030 --- /dev/null +++ b/e2e/npmScripts.spec.ts @@ -0,0 +1,44 @@ +/** The npm tool window lists package.json scripts without run configurations and runs them. */ +import type { Page } from "@playwright/test"; +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +const PKG = JSON.stringify({ name: "acme-api", scripts: { dev: "vite", build: "vite build" } }); + +/** The root package.json has scripts; there are no run configurations. */ +async function stubNpm(page: Page) { + await stubDesktop(page); + await page.evaluate(async (pkg) => { + const h = window.__ipc.handlers; + h.fs_read_text = (args) => (String(args.path).endsWith("/package.json") ? pkg : ""); + h.run_configs = () => []; + h.run_start = (args) => { + const config = args.config as { id: string; name: string }; + return { id: 8, configId: config.id, name: config.name, pid: 4242, running: true, pty: false }; + }; + const npmModule = "/src/features/run/npmScripts.ts"; + const { loadNpmScripts } = await import(/* @vite-ignore */ npmModule); + await loadNpmScripts(); + }, PKG); +} + +test("lists scripts without run configurations and runs one on double-click", async ({ workbench: page }) => { + await stubNpm(page); + await page.getByRole("button", { name: "npm", exact: true }).click(); + const tree = page.getByRole("tree", { name: "npm scripts" }); + await expect(tree).toBeVisible(); + await expect(tree.getByRole("treeitem", { name: /^acme-api/ })).toBeVisible(); + await expect(tree.getByRole("treeitem", { name: "dev" })).toBeVisible(); + await expect(tree.getByRole("treeitem", { name: "build" })).toBeVisible(); + await tree.getByRole("treeitem", { name: "dev" }).dblclick(); + const [started] = await ipcCalls(page, "run_start"); + expect(started).toMatchObject({ config: { args: ["run", "dev"], kind: "npm" } }); +}); + +test("offers Run / Debug in a script's context menu", async ({ workbench: page }) => { + await stubNpm(page); + await page.getByRole("button", { name: "npm", exact: true }).click(); + const tree = page.getByRole("tree", { name: "npm scripts" }); + await tree.getByRole("treeitem", { name: "build" }).click({ button: "right" }); + await expect(page.getByRole("menuitem", { name: /Run '.*run build'/ })).toBeVisible(); + await expect(page.getByRole("menuitem", { name: /Debug '.*run build'/ })).toBeVisible(); +}); diff --git a/src/features/run/NpmScriptsView.tsx b/src/features/run/NpmScriptsView.tsx new file mode 100644 index 000000000..6ac0d0dbb --- /dev/null +++ b/src/features/run/NpmScriptsView.tsx @@ -0,0 +1,77 @@ +/** The npm tool window: the project's packages and their scripts; activating a script runs it. */ +import { useMemo, useState, type ReactNode } from "react"; +import { tx } from "@/platform/i18n"; +import { useProject } from "@/platform/project"; +import { Icon, Tree, showContextMenu, type MenuItem, type TreeRowState } from "@/ui"; +import { runScriptNode, useNpmScripts, type NpmNode } from "./npmScripts"; +import { scriptMenuItems } from "./npmGutter"; +import { call, commands } from "@/platform/ipc"; +import styles from "./Run.module.css"; + +function scriptMenuItemsFor(node: NpmNode): Promise { + if (node.kind !== "script") return Promise.resolve([]); + return call(commands.fsReadText(node.path)) + .catch(() => "") + .then((content) => scriptMenuItems({ name: node.name, line: 0 }, node.path, content ?? "")); +} + +export function NpmScriptsView() { + const packages = useNpmScripts((s) => s.packages); + const loaded = useNpmScripts((s) => s.loaded); + const hasProject = useProject((s) => !!s.project); + const [collapsed, setCollapsed] = useState>(() => new Set()); + + const roots = useMemo(() => packages, [packages]); + const expanded = useMemo( + () => new Set(roots.filter((n) => n.kind === "package" && !collapsed.has(n.key)).map((n) => n.key)), + [roots, collapsed], + ); + + if (!loaded || !hasProject || roots.length === 0) return null; + + const renderNode = (n: NpmNode, state: TreeRowState): ReactNode => { + if (n.kind === "package") { + return ( + + + {n.label} + {n.scripts.length} + + ); + } + return ( + + + {n.name} + + ); + }; + + return ( + + aria-label={tx`npm scripts`} + roots={roots} + getKey={(n) => n.key} + getLabel={(n) => (n.kind === "package" ? n.label : n.name)} + getChildren={(n) => (n.kind === "package" ? n.scripts : undefined)} + isExpandable={(n) => n.kind === "package"} + expanded={expanded} + onExpandedChange={(_next, node, isOpen) => + setCollapsed((prev) => { + const s = new Set(prev); + if (isOpen) s.delete(node.key); + else s.add(node.key); + return s; + }) + } + onActivate={(n) => { + if (n.kind === "script") void runScriptNode(n, false); + }} + onContextMenu={(n, _sel, e) => { + if (n?.kind !== "script") return; + void scriptMenuItemsFor(n).then((items) => showContextMenu({ clientX: e.clientX, clientY: e.clientY }, items)); + }} + renderNode={renderNode} + /> + ); +} diff --git a/src/features/run/Run.module.css b/src/features/run/Run.module.css index 8af83fb49..853c33151 100644 --- a/src/features/run/Run.module.css +++ b/src/features/run/Run.module.css @@ -6,6 +6,23 @@ min-width: 0; } +.scriptsRow { + display: flex; + align-items: center; + gap: var(--space-1); + min-width: 0; +} + +.scriptsStrong { + font-weight: 500; +} + +.scriptsMeta { + flex: none; + color: var(--fg-subtle); + font-size: var(--fs-small); +} + .tabs { min-width: 0; border: 0; diff --git a/src/features/run/contextRun.test.ts b/src/features/run/contextRun.test.ts index 5b6b32e60..6f4466ea7 100644 --- a/src/features/run/contextRun.test.ts +++ b/src/features/run/contextRun.test.ts @@ -82,6 +82,18 @@ describe("run context menu group", () => { expect(items[0]).toMatchObject({ arg: { path: `${SCRATCHES}/hi.ts`, line: 0, dir: false } }); }); + it("editor: offers Run / Debug '