diff --git a/.changeset/rare-laws-sing.md b/.changeset/rare-laws-sing.md new file mode 100644 index 000000000000..4cf6e5170bbb --- /dev/null +++ b/.changeset/rare-laws-sing.md @@ -0,0 +1,47 @@ +--- +"wrangler": patch +--- + +fix: implement `d1 list --json` with clean output for piping into other commands + +Before: + +```bash +rozenmd@cflaptop test % npx wrangler d1 list +-------------------- +🚧 D1 is currently in open alpha and is not recommended for production data and traffic +🚧 Please report any bugs to https://github.com/cloudflare/wrangler2/issues/new/choose +🚧 To request features, visit https://community.cloudflare.com/c/developers/d1 +🚧 To give feedback, visit https://discord.gg/cloudflaredev +-------------------- + +┌──────────────────────────────┬─────────────────┐ +│ uuid │ name │ +├──────────────────────────────┼─────────────────┤ +│ xxxxxx-xxxx-xxxx-xxxx-xxxxxx │ test │ +├──────────────────────────────┼─────────────────┤ +│ xxxxxx-xxxx-xxxx-xxxx-xxxxxx │ test2 │ +├──────────────────────────────┼─────────────────┤ +│ xxxxxx-xxxx-xxxx-xxxx-xxxxxx │ test3 │ +└──────────────────────────────┴─────────────────┘ +``` + +After: + +```bash +rozenmd@cflaptop test % npx wrangler d1 list --json +[ + { + "uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx", + "name": "test" + }, + { + "uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx", + "name": "test2" + }, + { + "uuid": "xxxxxx-xxxx-xxxx-xxxx-xxxxxx", + "name": "test3" + }, +] +``` diff --git a/packages/wrangler/src/__tests__/d1/d1.test.ts b/packages/wrangler/src/__tests__/d1/d1.test.ts index 0745ae423bce..7557520e5651 100644 --- a/packages/wrangler/src/__tests__/d1/d1.test.ts +++ b/packages/wrangler/src/__tests__/d1/d1.test.ts @@ -1,9 +1,6 @@ -import { cwd } from "process"; import { mockConsoleMethods } from "../helpers/mock-console"; -import { useMockIsTTY } from "../helpers/mock-istty"; import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; -import writeWranglerToml from "../helpers/write-wrangler-toml"; function endEventLoop() { return new Promise((resolve) => setImmediate(resolve)); @@ -12,7 +9,6 @@ function endEventLoop() { describe("d1", () => { const std = mockConsoleMethods(); runInTempDir(); - const { setIsTTY } = useMockIsTTY(); it("should show help when no argument is passed", async () => { await runWrangler("d1"); @@ -86,44 +82,4 @@ describe("d1", () => { --------------------" `); }); - - describe("migrate", () => { - describe("apply", () => { - it("should not attempt to login in local mode", async () => { - setIsTTY(false); - writeWranglerToml({ - d1_databases: [ - { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, - ], - }); - // If we get to the point where we are checking for migrations then we have not been asked to log in. - await expect( - runWrangler("d1 migrations apply --local DATABASE") - ).rejects.toThrowError( - `No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.` - ); - }); - - it("should try to read D1 config from wrangler.toml", async () => { - setIsTTY(false); - writeWranglerToml(); - await expect( - runWrangler("d1 migrations apply DATABASE") - ).rejects.toThrowError( - "Can't find a DB with name/binding 'DATABASE' in local config. Check info in wrangler.toml..." - ); - }); - - it("should not try to read wrangler.toml in local mode", async () => { - setIsTTY(false); - writeWranglerToml(); - // If we get to the point where we are checking for migrations then we have not checked wrangler.toml. - await expect( - runWrangler("d1 migrations apply --local DATABASE") - ).rejects.toThrowError( - `No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.` - ); - }); - }); - }); }); diff --git a/packages/wrangler/src/__tests__/d1/migrate.test.ts b/packages/wrangler/src/__tests__/d1/migrate.test.ts new file mode 100644 index 000000000000..92ec78b6fbf2 --- /dev/null +++ b/packages/wrangler/src/__tests__/d1/migrate.test.ts @@ -0,0 +1,48 @@ +import { cwd } from "process"; +import { useMockIsTTY } from "../helpers/mock-istty"; +import { runInTempDir } from "../helpers/run-in-tmp"; +import { runWrangler } from "../helpers/run-wrangler"; +import writeWranglerToml from "../helpers/write-wrangler-toml"; + +describe("migrate", () => { + runInTempDir(); + const { setIsTTY } = useMockIsTTY(); + + describe("apply", () => { + it("should not attempt to login in local mode", async () => { + setIsTTY(false); + writeWranglerToml({ + d1_databases: [ + { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, + ], + }); + // If we get to the point where we are checking for migrations then we have not been asked to log in. + await expect( + runWrangler("d1 migrations apply --local DATABASE") + ).rejects.toThrowError( + `No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.` + ); + }); + + it("should try to read D1 config from wrangler.toml", async () => { + setIsTTY(false); + writeWranglerToml(); + await expect( + runWrangler("d1 migrations apply DATABASE") + ).rejects.toThrowError( + "Can't find a DB with name/binding 'DATABASE' in local config. Check info in wrangler.toml..." + ); + }); + + it("should not try to read wrangler.toml in local mode", async () => { + setIsTTY(false); + writeWranglerToml(); + // If we get to the point where we are checking for migrations then we have not checked wrangler.toml. + await expect( + runWrangler("d1 migrations apply --local DATABASE") + ).rejects.toThrowError( + `No migrations present at ${cwd().replaceAll("\\", "/")}/migrations.` + ); + }); + }); +}); diff --git a/packages/wrangler/src/d1/list.tsx b/packages/wrangler/src/d1/list.tsx index 46763d4ada2c..404e1d4b1952 100644 --- a/packages/wrangler/src/d1/list.tsx +++ b/packages/wrangler/src/d1/list.tsx @@ -12,18 +12,27 @@ import type { import type { Database } from "./types"; export function Options(d1ListYargs: CommonYargsArgv) { - return d1ListYargs.epilogue(d1BetaWarning); + return d1ListYargs + .option("json", { + describe: "return output as clean JSON", + type: "boolean", + default: false, + }) + .epilogue(d1BetaWarning); } -export async function Handler( - _: StrictYargsOptionsToInterface -): Promise { +export async function Handler({ + json, +}: StrictYargsOptionsToInterface): Promise { const accountId = await requireAuth({}); - logger.log(d1BetaWarning); - const dbs: Array = await listDatabases(accountId); - render(
); + if (json) { + logger.log(JSON.stringify(dbs, null, 2)); + } else { + logger.log(d1BetaWarning); + render(
); + } } export const listDatabases = async (