-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Graduate c3-vendor runCommand, quoteShellArgs, installPackages and installWrangler to @cloudflare/cli
#13068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dario-piotrowicz
merged 1 commit into
main
from
dario/DEVX-2380/1/graduate-cloudflare-cli-utilities
Mar 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@cloudflare/cli": minor | ||
| --- | ||
|
|
||
| Add `runCommand` and `quoteShellArgs`, `installPackages` and `installWrangler` utilities to `@cloudflare/cli/command` | ||
|
|
||
| These utilities are now available from `@cloudflare/cli` as dedicated sub-path exports: `runCommand` and `quoteShellArgs` via `@cloudflare/cli/command`, and `installPackages` and `installWrangler` via `@cloudflare/cli/packages`. This makes them reusable across packages in the SDK without duplication. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { spawn } from "cross-spawn"; | ||
| import { afterEach, beforeEach, describe, test, vi } from "vitest"; | ||
| import { quoteShellArgs, runCommand } from "../command"; | ||
| import type { ChildProcess } from "node:child_process"; | ||
|
|
||
| // We can change how the mock spawn works by setting these variables | ||
| let spawnResultCode = 0; | ||
| let spawnStdout: string | undefined = undefined; | ||
| let spawnStderr: string | undefined = undefined; | ||
|
|
||
| vi.mock("cross-spawn"); | ||
|
|
||
| describe("Command Helpers", () => { | ||
| afterEach(() => { | ||
| spawnResultCode = 0; | ||
| spawnStdout = undefined; | ||
| spawnStderr = undefined; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| vi.mocked(spawn).mockImplementation(() => { | ||
| return { | ||
| on: vi.fn().mockImplementation((event, cb) => { | ||
| if (event === "close") { | ||
| cb(spawnResultCode); | ||
| } | ||
| }), | ||
| stdout: { | ||
| on(_event: "data", cb: (data: string) => void) { | ||
| if (spawnStdout !== undefined) { | ||
| cb(spawnStdout); | ||
| } | ||
| }, | ||
| }, | ||
| stderr: { | ||
| on(_event: "data", cb: (data: string) => void) { | ||
| if (spawnStderr !== undefined) { | ||
| cb(spawnStderr); | ||
| } | ||
| }, | ||
| }, | ||
| } as unknown as ChildProcess; | ||
| }); | ||
| }); | ||
|
|
||
| test("runCommand", async ({ expect }) => { | ||
| await runCommand(["ls", "-l"]); | ||
| expect(spawn).toHaveBeenCalledWith("ls", ["-l"], { | ||
| stdio: "inherit", | ||
| env: process.env, | ||
| signal: expect.any(AbortSignal), | ||
| }); | ||
| }); | ||
|
|
||
| describe("quoteShellArgs", () => { | ||
| test.runIf(process.platform !== "win32")("mac", async ({ expect }) => { | ||
| expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev"); | ||
| expect(quoteShellArgs([`24.02 foo-bar`])).toEqual(`'24.02 foo-bar'`); | ||
| expect(quoteShellArgs([`foo/10 bar/20-baz/`])).toEqual( | ||
| `'foo/10 bar/20-baz/'` | ||
| ); | ||
| }); | ||
|
|
||
| test.runIf(process.platform === "win32")("windows", async ({ expect }) => { | ||
| expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev"); | ||
| expect(quoteShellArgs([`24.02 foo-bar`])).toEqual(`"24.02 foo-bar"`); | ||
| expect(quoteShellArgs([`foo/10 bar/20-baz/`])).toEqual( | ||
| `"foo/10 bar/20-baz/"` | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { writeFile } from "node:fs/promises"; | ||
| import { resolve } from "node:path"; | ||
| import { parsePackageJSON, readFileSync } from "@cloudflare/workers-utils"; | ||
| import { afterEach, describe, test, vi } from "vitest"; | ||
| import { runCommand } from "../command"; | ||
| import { installPackages, installWrangler } from "../packages"; | ||
|
|
||
| vi.mock("../command"); | ||
| vi.mock("@cloudflare/workers-utils", () => ({ | ||
| readFileSync: vi.fn(), | ||
| parsePackageJSON: vi.fn(), | ||
| })); | ||
| vi.mock("node:fs/promises", () => ({ | ||
| writeFile: vi.fn(), | ||
| })); | ||
|
|
||
| describe("Package Helpers", () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("installPackages", async () => { | ||
| type TestCase = { | ||
| pm: "npm" | "pnpm" | "yarn" | "bun"; | ||
| initialArgs: string[]; | ||
| additionalArgs?: string[]; | ||
| }; | ||
|
|
||
| const cases: TestCase[] = [ | ||
| { pm: "npm", initialArgs: ["npm", "install"] }, | ||
| { pm: "pnpm", initialArgs: ["pnpm", "install"] }, | ||
| { pm: "bun", initialArgs: ["bun", "add"] }, | ||
| { pm: "yarn", initialArgs: ["yarn", "add"] }, | ||
| ]; | ||
|
|
||
| test.for(cases)( | ||
| "with $pm", | ||
| async ({ pm, initialArgs, additionalArgs }, { expect }) => { | ||
| const mockPkgJson = { | ||
| dependencies: { | ||
| foo: "^1.0.0", | ||
| bar: "^2.0.0", | ||
| baz: "^1.2.3", | ||
| }, | ||
| }; | ||
| vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson)); | ||
| vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson); | ||
|
|
||
| const packages = ["foo", "bar@latest", "baz@1.2.3"]; | ||
| await installPackages(pm, packages); | ||
|
|
||
| expect(vi.mocked(runCommand)).toHaveBeenCalledWith( | ||
| [...initialArgs, ...packages, ...(additionalArgs ?? [])], | ||
| expect.anything() | ||
| ); | ||
|
|
||
| if (pm === "npm") { | ||
| const writeFileCall = vi.mocked(writeFile).mock.calls[0]; | ||
| expect(writeFileCall[0]).toBe(resolve(process.cwd(), "package.json")); | ||
| expect(JSON.parse(writeFileCall[1] as string)).toMatchObject({ | ||
| dependencies: { | ||
| foo: "^1.0.0", | ||
| bar: "^2.0.0", | ||
| baz: "1.2.3", | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| const devCases: TestCase[] = [ | ||
| { pm: "npm", initialArgs: ["npm", "install", "--save-dev"] }, | ||
| { pm: "pnpm", initialArgs: ["pnpm", "install", "--save-dev"] }, | ||
| { pm: "bun", initialArgs: ["bun", "add", "-d"] }, | ||
| { pm: "yarn", initialArgs: ["yarn", "add", "-D"] }, | ||
| ]; | ||
|
|
||
| test.for(devCases)( | ||
| "with $pm (dev = true)", | ||
| async ({ pm, initialArgs, additionalArgs }, { expect }) => { | ||
| const mockPkgJson = { | ||
| devDependencies: { | ||
| foo: "^1.0.0", | ||
| bar: "^2.0.0", | ||
| baz: "^1.2.3", | ||
| }, | ||
| }; | ||
| vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson)); | ||
| vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson); | ||
|
|
||
| const packages = ["foo", "bar@latest", "baz@1.2.3"]; | ||
| await installPackages(pm, packages, { dev: true }); | ||
|
|
||
| expect(vi.mocked(runCommand)).toHaveBeenCalledWith( | ||
| [...initialArgs, ...packages, ...(additionalArgs ?? [])], | ||
| expect.anything() | ||
| ); | ||
|
|
||
| if (pm === "npm") { | ||
| const writeFileCall = vi.mocked(writeFile).mock.calls[0]; | ||
| expect(writeFileCall[0]).toBe(resolve(process.cwd(), "package.json")); | ||
| expect(JSON.parse(writeFileCall[1] as string)).toMatchObject({ | ||
| devDependencies: { | ||
| foo: "^1.0.0", | ||
| bar: "^2.0.0", | ||
| baz: "1.2.3", | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test("installWrangler", async ({ expect }) => { | ||
| const mockPkgJson = { | ||
| devDependencies: { | ||
| wrangler: "^4.0.0", | ||
| }, | ||
| }; | ||
| vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson)); | ||
| vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson); | ||
|
|
||
| await installWrangler("npm", false); | ||
|
|
||
| expect(vi.mocked(runCommand)).toHaveBeenCalledWith( | ||
| ["npm", "install", "--save-dev", "wrangler@latest"], | ||
| expect.anything() | ||
| ); | ||
| }); | ||
| }); |
8 changes: 4 additions & 4 deletions
8
...ngler/src/autoconfig/c3-vendor/command.ts → packages/cli/command.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.