Skip to content

Commit 8cf47f9

Browse files
feat: ✨ introduce json output for cf pages deployment list (#8318)
* feat: ✨ introduce json output for cf pages deployment list, as it enables other tools like jq to parse the data * test: 🧪 add test for --json flag and changeset * Update .changeset/young-mammals-spend.md --------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent 151b3ce commit 8cf47f9

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

.changeset/young-mammals-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Introduce json output flag for wrangler pages deployment list

packages/wrangler/src/__tests__/pages/deployment-list.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,51 @@ describe("pages deployment list", () => {
5555
`);
5656
});
5757

58+
it("should make request to list deployments and return result as json", async () => {
59+
const deployments: Deployment[] = [
60+
{
61+
id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
62+
url: "https://87bbc8fe.images.pages.dev",
63+
environment: "preview",
64+
created_on: "2021-11-17T14:52:26.133835Z",
65+
latest_stage: {
66+
ended_on: "2021-11-17T14:52:26.133835Z",
67+
status: "success",
68+
},
69+
deployment_trigger: {
70+
metadata: {
71+
branch: "main",
72+
commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
73+
},
74+
},
75+
project_name: "images",
76+
},
77+
];
78+
79+
const requests = mockDeploymentListRequest(deployments);
80+
await runWrangler("pages deployment list --project-name=images --json");
81+
82+
expect(requests.count).toBe(1);
83+
const output = JSON.parse(std.out);
84+
85+
expect(output[0].Status).toBeTypeOf("string");
86+
output[0].Status = "SNAPSHOT_VALUE"; // This value would drift from snapshot if not hardcoded as is
87+
88+
expect(JSON.stringify(output, null, 2)).toMatchInlineSnapshot(`
89+
"[
90+
{
91+
\\"Id\\": \\"87bbc8fe-16be-45cd-81e0-63d722e82cdf\\",
92+
\\"Environment\\": \\"Preview\\",
93+
\\"Branch\\": \\"main\\",
94+
\\"Source\\": \\"c764936\\",
95+
\\"Deployment\\": \\"https://87bbc8fe.images.pages.dev\\",
96+
\\"Status\\": \\"SNAPSHOT_VALUE\\",
97+
\\"Build\\": \\"https://dash.cloudflare.com/some-account-id/pages/view/images/87bbc8fe-16be-45cd-81e0-63d722e82cdf\\"
98+
}
99+
]"
100+
`);
101+
});
102+
58103
it("should pass no environment", async () => {
59104
const deployments: Deployment[] = [
60105
{

packages/wrangler/src/pages/deployments.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ export const pagesDeploymentListCommand = createCommand({
3333
choices: ["production", "preview"],
3434
description: "Environment type to list deployments for",
3535
},
36+
json: {
37+
type: "boolean",
38+
description: "Return output as clean JSON",
39+
default: false,
40+
},
3641
},
37-
async handler({ projectName, environment }) {
42+
async handler({ projectName, environment, json }) {
3843
const config = getConfigCache<PagesConfigCache>(
3944
PAGES_CONFIG_CACHE_FILENAME
4045
);
@@ -93,7 +98,11 @@ export const pagesDeploymentListCommand = createCommand({
9398
account_id: accountId,
9499
});
95100

96-
logger.table(data);
101+
if (json) {
102+
logger.log(JSON.stringify(data, null, 2));
103+
} else {
104+
logger.table(data);
105+
}
97106
metrics.sendMetricsEvent("list pages deployments");
98107
},
99108
});

0 commit comments

Comments
 (0)