From f2b06bc37622040e8f861f58da43fc11b08db608 Mon Sep 17 00:00:00 2001 From: lifeir <132877455+lifeir@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:10:59 +0800 Subject: [PATCH] feat(run): npm tool window with the project's package.json scripts (#5217) A new npm tool window on the right lists the scripts of every package.json in the project (root, workspace members, detected module roots and any other package.json the index knows outside node_modules), grouped by package. Double-click runs a script; the context menu offers Run / Debug through the same path as the package.json gutter, reusing a matching run configuration or creating a temporary one. The window appears only while the project has scripts and refreshes when a package.json changes or indexing finishes. The editor context menu on a package.json script line also offers Run / Debug for that script. Co-authored-by: lifeir <132877455+lifeir@users.noreply.github.com> --- e2e/npmScripts.spec.ts | 44 +++++++ src/features/run/NpmScriptsView.tsx | 77 ++++++++++++ src/features/run/Run.module.css | 17 +++ src/features/run/contextRun.test.ts | 12 ++ src/features/run/contextRun.ts | 19 +++ src/features/run/index.ts | 2 + src/features/run/npmScripts.ts | 186 ++++++++++++++++++++++++++++ src/platform/locales/text/en.json | 1 + src/platform/locales/text/zh.json | 1 + 9 files changed, 359 insertions(+) create mode 100644 e2e/npmScripts.spec.ts create mode 100644 src/features/run/NpmScriptsView.tsx create mode 100644 src/features/run/npmScripts.ts 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 '