Skip to content

Commit e846512

Browse files
andyjessopCarmenPopoviciu
authored andcommitted
chore: convert deployments to createCommand (#7499)
* chore: convert deployments to createCommand kebab case for positional remove print banner chore: register namesapce * chore: add hidden to deployments view * remove unused imports * Update packages/wrangler/src/versions/deployments/list.ts * Update packages/wrangler/src/versions/deployments/list.ts * Update packages/wrangler/src/versions/deployments/status.ts * Update packages/wrangler/src/versions/deployments/status.ts * Update packages/wrangler/src/versions/deployments/status.ts * Update packages/wrangler/src/versions/deployments/view.ts * chore: update snapshots --------- Co-authored-by: Carmen Popoviciu <[email protected]>
1 parent 67a0579 commit e846512

File tree

7 files changed

+254
-284
lines changed

7 files changed

+254
-284
lines changed

packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("deployments list", () => {
2323
const result = runWrangler("deployments list --experimental-versions");
2424

2525
await expect(result).rejects.toMatchInlineSnapshot(
26-
`[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name <name>\` or in your config file as \`name = "<name>"\`]`
26+
`[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name <name>\` or in your configuration file as \`name = "<name>"\`]`
2727
);
2828

2929
expect(std.out).toMatchInlineSnapshot(`""`);

packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("deployments list", () => {
2525
);
2626

2727
await expect(result).rejects.toMatchInlineSnapshot(
28-
`[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name <name>\` or in your config file as \`name = "<name>"\`]`
28+
`[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name <name>\` or in your configuration file as \`name = "<name>"\`]`
2929
);
3030

3131
expect(std.out).toMatchInlineSnapshot(`""`);

packages/wrangler/src/index.ts

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ import { debugLogFilepath } from "./utils/log-file";
163163
import { vectorize } from "./vectorize/index";
164164
import { versionsNamespace } from "./versions";
165165
import { versionsDeployCommand } from "./versions/deploy";
166-
import registerVersionsDeploymentsSubcommands from "./versions/deployments";
166+
import { deploymentsNamespace } from "./versions/deployments";
167+
import { deploymentsListCommand } from "./versions/deployments/list";
168+
import { deploymentsStatusCommand } from "./versions/deployments/status";
169+
import { deploymentsViewCommand } from "./versions/deployments/view";
167170
import { versionsListCommand } from "./versions/list";
168171
import registerVersionsRollbackCommand from "./versions/rollback";
169172
import { versionsSecretNamespace } from "./versions/secrets";
@@ -477,55 +480,65 @@ export function createCLIParser(argv: string[]) {
477480
deployHandler
478481
);
479482

480-
// deployments
481-
const deploymentsDescription =
482-
"🚢 List and view the current and past deployments for your Worker";
483-
484483
if (experimentalGradualRollouts) {
484+
registry.define([
485+
{ command: "wrangler deployments", definition: deploymentsNamespace },
486+
{
487+
command: "wrangler deployments list",
488+
definition: deploymentsListCommand,
489+
},
490+
{
491+
command: "wrangler deployments status",
492+
definition: deploymentsStatusCommand,
493+
},
494+
{
495+
command: "wrangler deployments view",
496+
definition: deploymentsViewCommand,
497+
},
498+
]);
499+
registry.registerNamespace("deployments");
500+
} else {
485501
wrangler.command(
486502
"deployments",
487-
deploymentsDescription,
488-
registerVersionsDeploymentsSubcommands
489-
);
490-
} else {
491-
wrangler.command("deployments", deploymentsDescription, (yargs) =>
492-
yargs
493-
.option("name", {
494-
describe: "The name of your Worker",
495-
type: "string",
496-
})
497-
.command(
498-
"list",
499-
"Displays the 10 most recent deployments for a Worker",
500-
async (listYargs) => listYargs,
501-
async (listYargs) => {
502-
const { accountId, scriptName, config } =
503-
await commonDeploymentCMDSetup(listYargs);
504-
await deployments(accountId, scriptName, config);
505-
}
506-
)
507-
.command(
508-
"view [deployment-id]",
509-
"View a deployment",
510-
async (viewYargs) =>
511-
viewYargs.positional("deployment-id", {
512-
describe: "The ID of the deployment you want to inspect",
513-
type: "string",
514-
demandOption: false,
515-
}),
516-
async (viewYargs) => {
517-
const { accountId, scriptName, config } =
518-
await commonDeploymentCMDSetup(viewYargs);
519-
520-
await viewDeployment(
521-
accountId,
522-
scriptName,
523-
config,
524-
viewYargs.deploymentId
525-
);
526-
}
527-
)
528-
.command(subHelp)
503+
"🚢 List and view the current and past deployments for your Worker",
504+
(yargs) =>
505+
yargs
506+
.option("name", {
507+
describe: "The name of your Worker",
508+
type: "string",
509+
})
510+
.command(
511+
"list",
512+
"Displays the 10 most recent deployments for a Worker",
513+
async (listYargs) => listYargs,
514+
async (listYargs) => {
515+
const { accountId, scriptName, config } =
516+
await commonDeploymentCMDSetup(listYargs);
517+
await deployments(accountId, scriptName, config);
518+
}
519+
)
520+
.command(
521+
"view [deployment-id]",
522+
"View a deployment",
523+
async (viewYargs) =>
524+
viewYargs.positional("deployment-id", {
525+
describe: "The ID of the deployment you want to inspect",
526+
type: "string",
527+
demandOption: false,
528+
}),
529+
async (viewYargs) => {
530+
const { accountId, scriptName, config } =
531+
await commonDeploymentCMDSetup(viewYargs);
532+
533+
await viewDeployment(
534+
accountId,
535+
scriptName,
536+
config,
537+
viewYargs.deploymentId
538+
);
539+
}
540+
)
541+
.command(subHelp)
529542
);
530543
}
531544

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
1-
import {
2-
versionsDeploymentsListHandler,
3-
versionsDeploymentsListOptions,
4-
} from "./list";
5-
import {
6-
versionsDeploymentsStatusHandler,
7-
versionsDeploymentsStatusOptions,
8-
} from "./status";
9-
import {
10-
versionsDeploymentsViewHandler,
11-
versionsDeploymentsViewOptions,
12-
} from "./view";
13-
import type { CommonYargsArgv } from "../../yargs-types";
1+
import { createNamespace } from "../../core/create-command";
142

15-
export default function registerVersionsDeploymentsSubcommands(
16-
versionDeploymentsYargs: CommonYargsArgv
17-
) {
18-
versionDeploymentsYargs
19-
.command(
20-
"list",
21-
"Displays the 10 most recent deployments of your Worker",
22-
versionsDeploymentsListOptions,
23-
versionsDeploymentsListHandler
24-
)
25-
.command(
26-
"status",
27-
"View the current state of your production",
28-
versionsDeploymentsStatusOptions,
29-
versionsDeploymentsStatusHandler
30-
)
31-
.command(
32-
"view [deployment-id]",
33-
false,
34-
versionsDeploymentsViewOptions,
35-
versionsDeploymentsViewHandler
36-
);
37-
}
3+
export const deploymentsNamespace = createNamespace({
4+
metadata: {
5+
description:
6+
"🚢 List and view the current and past deployments for your Worker",
7+
owner: "Workers: Authoring and Testing",
8+
status: "stable",
9+
},
10+
});

packages/wrangler/src/versions/deployments/list.ts

Lines changed: 78 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,107 @@
11
import assert from "assert";
22
import { logRaw } from "@cloudflare/cli";
33
import { brandColor, gray } from "@cloudflare/cli/colors";
4-
import { readConfig } from "../../config";
4+
import { createCommand } from "../../core/create-command";
55
import { UserError } from "../../errors";
66
import * as metrics from "../../metrics";
77
import { requireAuth } from "../../user";
88
import formatLabelledValues from "../../utils/render-labelled-values";
9-
import { printWranglerBanner } from "../../wrangler-banner";
109
import { fetchLatestDeployments, fetchVersions } from "../api";
1110
import { getVersionSource } from "../list";
12-
import type {
13-
CommonYargsArgv,
14-
StrictYargsOptionsToInterface,
15-
} from "../../yargs-types";
1611
import type { ApiDeployment, VersionCache } from "../types";
1712

1813
const BLANK_INPUT = "-"; // To be used where optional user-input is displayed and the value is nullish
1914

20-
export type VersionsDeloymentsListArgs = StrictYargsOptionsToInterface<
21-
typeof versionsDeploymentsListOptions
22-
>;
23-
24-
export function versionsDeploymentsListOptions(yargs: CommonYargsArgv) {
25-
return yargs
26-
.option("name", {
27-
describe: "Name of the worker",
15+
export const deploymentsListCommand = createCommand({
16+
metadata: {
17+
description: "Displays the 10 most recent deployments of your Worker",
18+
owner: "Workers: Authoring and Testing",
19+
status: "stable",
20+
},
21+
args: {
22+
name: {
23+
describe: "Name of the Worker",
2824
type: "string",
2925
requiresArg: true,
30-
})
31-
.option("json", {
26+
},
27+
json: {
3228
describe: "Display output as clean JSON",
3329
type: "boolean",
3430
default: false,
35-
});
36-
}
37-
38-
export async function versionsDeploymentsListHandler(
39-
args: VersionsDeloymentsListArgs
40-
) {
41-
if (!args.json) {
42-
await printWranglerBanner();
43-
}
44-
45-
const config = readConfig(args);
46-
metrics.sendMetricsEvent(
47-
"list versioned deployments",
48-
{ json: args.json },
49-
{
50-
sendMetrics: config.send_metrics,
51-
}
52-
);
53-
54-
const accountId = await requireAuth(config);
55-
const workerName = args.name ?? config.name;
56-
57-
if (workerName === undefined) {
58-
throw new UserError(
59-
'You need to provide a name of your worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
31+
},
32+
},
33+
behaviour: {
34+
printBanner: false,
35+
},
36+
handler: async function versionsDeploymentsListHandler(args, { config }) {
37+
metrics.sendMetricsEvent(
38+
"list versioned deployments",
39+
{ json: args.json },
40+
{
41+
sendMetrics: config.send_metrics,
42+
}
6043
);
61-
}
62-
63-
const deployments = (
64-
await fetchLatestDeployments(accountId, workerName)
65-
).sort((a, b) => a.created_on.localeCompare(b.created_on));
6644

67-
if (args.json) {
68-
logRaw(JSON.stringify(deployments, null, 2));
69-
return;
70-
}
45+
const accountId = await requireAuth(config);
46+
const workerName = args.name ?? config.name;
7147

72-
const versionCache: VersionCache = new Map();
73-
const versionIds = deployments.flatMap((d) =>
74-
d.versions.map((v) => v.version_id)
75-
);
76-
await fetchVersions(accountId, workerName, versionCache, ...versionIds);
77-
78-
const formattedDeployments = deployments.map((deployment) => {
79-
const formattedVersions = deployment.versions.map((traffic) => {
80-
const version = versionCache.get(traffic.version_id);
81-
assert(version);
82-
83-
const percentage = brandColor(`(${traffic.percentage}%)`);
84-
const details = formatLabelledValues(
85-
{
86-
Created: new Date(version.metadata["created_on"]).toISOString(),
87-
Tag: version.annotations?.["workers/tag"] || BLANK_INPUT,
88-
Message: version.annotations?.["workers/message"] || BLANK_INPUT,
89-
},
90-
{
91-
indentationCount: 4,
92-
labelJustification: "right",
93-
formatLabel: (label) => gray(label + ":"),
94-
formatValue: (value) => gray(value),
95-
}
48+
if (workerName === undefined) {
49+
throw new UserError(
50+
'You need to provide a name for your Worker. Either pass it as a cli arg with `--name <name>` or in your configuration file as `name = "<name>"`'
9651
);
52+
}
9753

98-
return `${percentage} ${version.id}\n${details}`;
99-
});
54+
const deployments = (
55+
await fetchLatestDeployments(accountId, workerName)
56+
).sort((a, b) => a.created_on.localeCompare(b.created_on));
10057

101-
return formatLabelledValues({
102-
// explicitly not outputting Deployment ID
103-
Created: new Date(deployment.created_on).toISOString(),
104-
Author: deployment.author_email,
105-
Source: getDeploymentSource(deployment),
106-
Message: deployment.annotations?.["workers/message"] || BLANK_INPUT,
107-
"Version(s)": formattedVersions.join("\n\n"),
58+
if (args.json) {
59+
logRaw(JSON.stringify(deployments, null, 2));
60+
return;
61+
}
62+
63+
const versionCache: VersionCache = new Map();
64+
const versionIds = deployments.flatMap((d) =>
65+
d.versions.map((v) => v.version_id)
66+
);
67+
await fetchVersions(accountId, workerName, versionCache, ...versionIds);
68+
69+
const formattedDeployments = deployments.map((deployment) => {
70+
const formattedVersions = deployment.versions.map((traffic) => {
71+
const version = versionCache.get(traffic.version_id);
72+
assert(version);
73+
74+
const percentage = brandColor(`(${traffic.percentage}%)`);
75+
const details = formatLabelledValues(
76+
{
77+
Created: new Date(version.metadata["created_on"]).toISOString(),
78+
Tag: version.annotations?.["workers/tag"] || BLANK_INPUT,
79+
Message: version.annotations?.["workers/message"] || BLANK_INPUT,
80+
},
81+
{
82+
indentationCount: 4,
83+
labelJustification: "right",
84+
formatLabel: (label) => gray(label + ":"),
85+
formatValue: (value) => gray(value),
86+
}
87+
);
88+
89+
return `${percentage} ${version.id}\n${details}`;
90+
});
91+
92+
return formatLabelledValues({
93+
// explicitly not outputting Deployment ID
94+
Created: new Date(deployment.created_on).toISOString(),
95+
Author: deployment.author_email,
96+
Source: getDeploymentSource(deployment),
97+
Message: deployment.annotations?.["workers/message"] || BLANK_INPUT,
98+
"Version(s)": formattedVersions.join("\n\n"),
99+
});
108100
});
109-
});
110101

111-
logRaw(formattedDeployments.join("\n\n"));
112-
}
102+
logRaw(formattedDeployments.join("\n\n"));
103+
},
104+
});
113105

114106
export function getDeploymentSource(deployment: ApiDeployment) {
115107
return getVersionSource({

0 commit comments

Comments
 (0)