From a55bc367e4cc81c906f8b82b37e4637c5c0bef6b Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Tue, 17 Dec 2024 10:34:37 +0100 Subject: [PATCH] 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 --- .../deployments/deployments.list.test.ts | 2 +- .../deployments/deployments.status.test.ts | 2 +- packages/wrangler/src/index.ts | 107 +++++++----- .../src/versions/deployments/index.ts | 45 +---- .../wrangler/src/versions/deployments/list.ts | 164 +++++++++--------- .../src/versions/deployments/status.ts | 157 ++++++++--------- .../wrangler/src/versions/deployments/view.ts | 61 +++---- 7 files changed, 254 insertions(+), 284 deletions(-) diff --git a/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts b/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts index bdcdfcea53cf..d79c96ecec91 100644 --- a/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts +++ b/packages/wrangler/src/__tests__/versions/deployments/deployments.list.test.ts @@ -23,7 +23,7 @@ describe("deployments list", () => { const result = runWrangler("deployments list --experimental-versions"); await expect(result).rejects.toMatchInlineSnapshot( - `[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name \` or in your config file as \`name = ""\`]` + `[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name \` or in your configuration file as \`name = ""\`]` ); expect(std.out).toMatchInlineSnapshot(`""`); diff --git a/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts b/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts index ef088d4401b9..ec4564fb93fd 100644 --- a/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts +++ b/packages/wrangler/src/__tests__/versions/deployments/deployments.status.test.ts @@ -25,7 +25,7 @@ describe("deployments list", () => { ); await expect(result).rejects.toMatchInlineSnapshot( - `[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name \` or in your config file as \`name = ""\`]` + `[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name \` or in your configuration file as \`name = ""\`]` ); expect(std.out).toMatchInlineSnapshot(`""`); diff --git a/packages/wrangler/src/index.ts b/packages/wrangler/src/index.ts index 2e0e4d076b72..4bc46fb077b4 100644 --- a/packages/wrangler/src/index.ts +++ b/packages/wrangler/src/index.ts @@ -163,7 +163,10 @@ import { debugLogFilepath } from "./utils/log-file"; import { vectorize } from "./vectorize/index"; import { versionsNamespace } from "./versions"; import { versionsDeployCommand } from "./versions/deploy"; -import registerVersionsDeploymentsSubcommands from "./versions/deployments"; +import { deploymentsNamespace } from "./versions/deployments"; +import { deploymentsListCommand } from "./versions/deployments/list"; +import { deploymentsStatusCommand } from "./versions/deployments/status"; +import { deploymentsViewCommand } from "./versions/deployments/view"; import { versionsListCommand } from "./versions/list"; import registerVersionsRollbackCommand from "./versions/rollback"; import { versionsSecretNamespace } from "./versions/secrets"; @@ -477,55 +480,65 @@ export function createCLIParser(argv: string[]) { deployHandler ); - // deployments - const deploymentsDescription = - "🚢 List and view the current and past deployments for your Worker"; - if (experimentalGradualRollouts) { + registry.define([ + { command: "wrangler deployments", definition: deploymentsNamespace }, + { + command: "wrangler deployments list", + definition: deploymentsListCommand, + }, + { + command: "wrangler deployments status", + definition: deploymentsStatusCommand, + }, + { + command: "wrangler deployments view", + definition: deploymentsViewCommand, + }, + ]); + registry.registerNamespace("deployments"); + } else { wrangler.command( "deployments", - deploymentsDescription, - registerVersionsDeploymentsSubcommands - ); - } else { - wrangler.command("deployments", deploymentsDescription, (yargs) => - yargs - .option("name", { - describe: "The name of your Worker", - type: "string", - }) - .command( - "list", - "Displays the 10 most recent deployments for a Worker", - async (listYargs) => listYargs, - async (listYargs) => { - const { accountId, scriptName, config } = - await commonDeploymentCMDSetup(listYargs); - await deployments(accountId, scriptName, config); - } - ) - .command( - "view [deployment-id]", - "View a deployment", - async (viewYargs) => - viewYargs.positional("deployment-id", { - describe: "The ID of the deployment you want to inspect", - type: "string", - demandOption: false, - }), - async (viewYargs) => { - const { accountId, scriptName, config } = - await commonDeploymentCMDSetup(viewYargs); - - await viewDeployment( - accountId, - scriptName, - config, - viewYargs.deploymentId - ); - } - ) - .command(subHelp) + "🚢 List and view the current and past deployments for your Worker", + (yargs) => + yargs + .option("name", { + describe: "The name of your Worker", + type: "string", + }) + .command( + "list", + "Displays the 10 most recent deployments for a Worker", + async (listYargs) => listYargs, + async (listYargs) => { + const { accountId, scriptName, config } = + await commonDeploymentCMDSetup(listYargs); + await deployments(accountId, scriptName, config); + } + ) + .command( + "view [deployment-id]", + "View a deployment", + async (viewYargs) => + viewYargs.positional("deployment-id", { + describe: "The ID of the deployment you want to inspect", + type: "string", + demandOption: false, + }), + async (viewYargs) => { + const { accountId, scriptName, config } = + await commonDeploymentCMDSetup(viewYargs); + + await viewDeployment( + accountId, + scriptName, + config, + viewYargs.deploymentId + ); + } + ) + .command(subHelp) ); } diff --git a/packages/wrangler/src/versions/deployments/index.ts b/packages/wrangler/src/versions/deployments/index.ts index 1707f444e40d..85e56b2d0d3a 100644 --- a/packages/wrangler/src/versions/deployments/index.ts +++ b/packages/wrangler/src/versions/deployments/index.ts @@ -1,37 +1,10 @@ -import { - versionsDeploymentsListHandler, - versionsDeploymentsListOptions, -} from "./list"; -import { - versionsDeploymentsStatusHandler, - versionsDeploymentsStatusOptions, -} from "./status"; -import { - versionsDeploymentsViewHandler, - versionsDeploymentsViewOptions, -} from "./view"; -import type { CommonYargsArgv } from "../../yargs-types"; +import { createNamespace } from "../../core/create-command"; -export default function registerVersionsDeploymentsSubcommands( - versionDeploymentsYargs: CommonYargsArgv -) { - versionDeploymentsYargs - .command( - "list", - "Displays the 10 most recent deployments of your Worker", - versionsDeploymentsListOptions, - versionsDeploymentsListHandler - ) - .command( - "status", - "View the current state of your production", - versionsDeploymentsStatusOptions, - versionsDeploymentsStatusHandler - ) - .command( - "view [deployment-id]", - false, - versionsDeploymentsViewOptions, - versionsDeploymentsViewHandler - ); -} +export const deploymentsNamespace = createNamespace({ + metadata: { + description: + "🚢 List and view the current and past deployments for your Worker", + owner: "Workers: Authoring and Testing", + status: "stable", + }, +}); diff --git a/packages/wrangler/src/versions/deployments/list.ts b/packages/wrangler/src/versions/deployments/list.ts index 80e3aa4581d3..8753cde5a910 100644 --- a/packages/wrangler/src/versions/deployments/list.ts +++ b/packages/wrangler/src/versions/deployments/list.ts @@ -1,115 +1,107 @@ import assert from "assert"; import { logRaw } from "@cloudflare/cli"; import { brandColor, gray } from "@cloudflare/cli/colors"; -import { readConfig } from "../../config"; +import { createCommand } from "../../core/create-command"; import { UserError } from "../../errors"; import * as metrics from "../../metrics"; import { requireAuth } from "../../user"; import formatLabelledValues from "../../utils/render-labelled-values"; -import { printWranglerBanner } from "../../wrangler-banner"; import { fetchLatestDeployments, fetchVersions } from "../api"; import { getVersionSource } from "../list"; -import type { - CommonYargsArgv, - StrictYargsOptionsToInterface, -} from "../../yargs-types"; import type { ApiDeployment, VersionCache } from "../types"; const BLANK_INPUT = "-"; // To be used where optional user-input is displayed and the value is nullish -export type VersionsDeloymentsListArgs = StrictYargsOptionsToInterface< - typeof versionsDeploymentsListOptions ->; - -export function versionsDeploymentsListOptions(yargs: CommonYargsArgv) { - return yargs - .option("name", { - describe: "Name of the worker", +export const deploymentsListCommand = createCommand({ + metadata: { + description: "Displays the 10 most recent deployments of your Worker", + owner: "Workers: Authoring and Testing", + status: "stable", + }, + args: { + name: { + describe: "Name of the Worker", type: "string", requiresArg: true, - }) - .option("json", { + }, + json: { describe: "Display output as clean JSON", type: "boolean", default: false, - }); -} - -export async function versionsDeploymentsListHandler( - args: VersionsDeloymentsListArgs -) { - if (!args.json) { - await printWranglerBanner(); - } - - const config = readConfig(args); - metrics.sendMetricsEvent( - "list versioned deployments", - { json: args.json }, - { - sendMetrics: config.send_metrics, - } - ); - - const accountId = await requireAuth(config); - const workerName = args.name ?? config.name; - - if (workerName === undefined) { - throw new UserError( - 'You need to provide a name of your worker. Either pass it as a cli arg with `--name ` or in your config file as `name = ""`' + }, + }, + behaviour: { + printBanner: false, + }, + handler: async function versionsDeploymentsListHandler(args, { config }) { + metrics.sendMetricsEvent( + "list versioned deployments", + { json: args.json }, + { + sendMetrics: config.send_metrics, + } ); - } - - const deployments = ( - await fetchLatestDeployments(accountId, workerName) - ).sort((a, b) => a.created_on.localeCompare(b.created_on)); - if (args.json) { - logRaw(JSON.stringify(deployments, null, 2)); - return; - } + const accountId = await requireAuth(config); + const workerName = args.name ?? config.name; - const versionCache: VersionCache = new Map(); - const versionIds = deployments.flatMap((d) => - d.versions.map((v) => v.version_id) - ); - await fetchVersions(accountId, workerName, versionCache, ...versionIds); - - const formattedDeployments = deployments.map((deployment) => { - const formattedVersions = deployment.versions.map((traffic) => { - const version = versionCache.get(traffic.version_id); - assert(version); - - const percentage = brandColor(`(${traffic.percentage}%)`); - const details = formatLabelledValues( - { - Created: new Date(version.metadata["created_on"]).toISOString(), - Tag: version.annotations?.["workers/tag"] || BLANK_INPUT, - Message: version.annotations?.["workers/message"] || BLANK_INPUT, - }, - { - indentationCount: 4, - labelJustification: "right", - formatLabel: (label) => gray(label + ":"), - formatValue: (value) => gray(value), - } + if (workerName === undefined) { + throw new UserError( + 'You need to provide a name for your Worker. Either pass it as a cli arg with `--name ` or in your configuration file as `name = ""`' ); + } - return `${percentage} ${version.id}\n${details}`; - }); + const deployments = ( + await fetchLatestDeployments(accountId, workerName) + ).sort((a, b) => a.created_on.localeCompare(b.created_on)); - return formatLabelledValues({ - // explicitly not outputting Deployment ID - Created: new Date(deployment.created_on).toISOString(), - Author: deployment.author_email, - Source: getDeploymentSource(deployment), - Message: deployment.annotations?.["workers/message"] || BLANK_INPUT, - "Version(s)": formattedVersions.join("\n\n"), + if (args.json) { + logRaw(JSON.stringify(deployments, null, 2)); + return; + } + + const versionCache: VersionCache = new Map(); + const versionIds = deployments.flatMap((d) => + d.versions.map((v) => v.version_id) + ); + await fetchVersions(accountId, workerName, versionCache, ...versionIds); + + const formattedDeployments = deployments.map((deployment) => { + const formattedVersions = deployment.versions.map((traffic) => { + const version = versionCache.get(traffic.version_id); + assert(version); + + const percentage = brandColor(`(${traffic.percentage}%)`); + const details = formatLabelledValues( + { + Created: new Date(version.metadata["created_on"]).toISOString(), + Tag: version.annotations?.["workers/tag"] || BLANK_INPUT, + Message: version.annotations?.["workers/message"] || BLANK_INPUT, + }, + { + indentationCount: 4, + labelJustification: "right", + formatLabel: (label) => gray(label + ":"), + formatValue: (value) => gray(value), + } + ); + + return `${percentage} ${version.id}\n${details}`; + }); + + return formatLabelledValues({ + // explicitly not outputting Deployment ID + Created: new Date(deployment.created_on).toISOString(), + Author: deployment.author_email, + Source: getDeploymentSource(deployment), + Message: deployment.annotations?.["workers/message"] || BLANK_INPUT, + "Version(s)": formattedVersions.join("\n\n"), + }); }); - }); - logRaw(formattedDeployments.join("\n\n")); -} + logRaw(formattedDeployments.join("\n\n")); + }, +}); export function getDeploymentSource(deployment: ApiDeployment) { return getVersionSource({ diff --git a/packages/wrangler/src/versions/deployments/status.ts b/packages/wrangler/src/versions/deployments/status.ts index a7d974b66163..3afb075b9f40 100644 --- a/packages/wrangler/src/versions/deployments/status.ts +++ b/packages/wrangler/src/versions/deployments/status.ts @@ -1,7 +1,7 @@ import assert from "assert"; import { logRaw } from "@cloudflare/cli"; import { brandColor, gray } from "@cloudflare/cli/colors"; -import { readConfig } from "../../config"; +import { createCommand } from "../../core/create-command"; import { UserError } from "../../errors"; import * as metrics from "../../metrics"; import { requireAuth } from "../../user"; @@ -9,102 +9,99 @@ import formatLabelledValues from "../../utils/render-labelled-values"; import { printWranglerBanner } from "../../wrangler-banner"; import { fetchLatestDeployment, fetchVersions } from "../api"; import { getDeploymentSource } from "./list"; -import type { - CommonYargsArgv, - StrictYargsOptionsToInterface, -} from "../../yargs-types"; import type { VersionCache } from "../types"; const BLANK_INPUT = "-"; // To be used where optional user-input is displayed and the value is nullish -export type VersionsDeploymentsStatusArgs = StrictYargsOptionsToInterface< - typeof versionsDeploymentsStatusOptions ->; - -export function versionsDeploymentsStatusOptions(yargs: CommonYargsArgv) { - return yargs - .option("name", { - describe: "Name of the worker", +export const deploymentsStatusCommand = createCommand({ + metadata: { + description: "View the current state of your production", + owner: "Workers: Authoring and Testing", + status: "stable", + }, + args: { + name: { + describe: "Name of the Worker", type: "string", requiresArg: true, - }) - .option("json", { + }, + json: { describe: "Display output as clean JSON", type: "boolean", default: false, - }); -} - -export async function versionsDeploymentsStatusHandler( - args: VersionsDeploymentsStatusArgs -) { - if (!args.json) { - await printWranglerBanner(); - } - - const config = readConfig(args); - metrics.sendMetricsEvent( - "view latest versioned deployment", - {}, - { - sendMetrics: config.send_metrics, + }, + }, + behaviour: { + printBanner: false, + }, + handler: async function versionsDeploymentsStatusHandler(args, { config }) { + if (!args.json) { + await printWranglerBanner(); } - ); - - const accountId = await requireAuth(config); - const workerName = args.name ?? config.name; - if (workerName === undefined) { - throw new UserError( - 'You need to provide a name of your worker. Either pass it as a cli arg with `--name ` or in your config file as `name = ""`' + metrics.sendMetricsEvent( + "view latest versioned deployment", + {}, + { + sendMetrics: config.send_metrics, + } ); - } - const latestDeployment = await fetchLatestDeployment(accountId, workerName); + const accountId = await requireAuth(config); + const workerName = args.name ?? config.name; - if (!latestDeployment) { - throw new UserError(`The worker ${workerName} has no deployments.`); - } - - if (args.json) { - logRaw(JSON.stringify(latestDeployment, null, 2)); - return; - } + if (workerName === undefined) { + throw new UserError( + 'You need to provide a name for your Worker. Either pass it as a cli arg with `--name ` or in your configuration file as `name = ""`' + ); + } - const versionCache: VersionCache = new Map(); - const versionIds = latestDeployment.versions.map((v) => v.version_id); - await fetchVersions(accountId, workerName, versionCache, ...versionIds); + const latestDeployment = await fetchLatestDeployment(accountId, workerName); - const formattedVersions = latestDeployment.versions.map((traffic) => { - const version = versionCache.get(traffic.version_id); - assert(version); + if (!latestDeployment) { + throw new UserError(`The Worker ${workerName} has no deployments.`); + } - const percentage = brandColor(`(${traffic.percentage}%)`); - const details = formatLabelledValues( - { - Created: new Date(version.metadata["created_on"]).toISOString(), - Tag: version.annotations?.["workers/tag"] || BLANK_INPUT, - Message: version.annotations?.["workers/message"] || BLANK_INPUT, - }, - { - indentationCount: 4, - labelJustification: "right", - formatLabel: (label) => gray(label + ":"), - formatValue: (value) => gray(value), - } - ); + if (args.json) { + logRaw(JSON.stringify(latestDeployment, null, 2)); + return; + } - return `${percentage} ${version.id}\n${details}`; - }); + const versionCache: VersionCache = new Map(); + const versionIds = latestDeployment.versions.map((v) => v.version_id); + await fetchVersions(accountId, workerName, versionCache, ...versionIds); + + const formattedVersions = latestDeployment.versions.map((traffic) => { + const version = versionCache.get(traffic.version_id); + assert(version); + + const percentage = brandColor(`(${traffic.percentage}%)`); + const details = formatLabelledValues( + { + Created: new Date(version.metadata["created_on"]).toISOString(), + Tag: version.annotations?.["workers/tag"] || BLANK_INPUT, + Message: version.annotations?.["workers/message"] || BLANK_INPUT, + }, + { + indentationCount: 4, + labelJustification: "right", + formatLabel: (label) => gray(label + ":"), + formatValue: (value) => gray(value), + } + ); + + return `${percentage} ${version.id}\n${details}`; + }); - const formattedDeployment = formatLabelledValues({ - // explicitly not outputting Deployment ID - Created: new Date(latestDeployment.created_on).toISOString(), - Author: latestDeployment.author_email, - Source: getDeploymentSource(latestDeployment), - Message: latestDeployment.annotations?.["workers/message"] || BLANK_INPUT, - "Version(s)": formattedVersions.join("\n\n"), - }); + const formattedDeployment = formatLabelledValues({ + // explicitly not outputting Deployment ID + Created: new Date(latestDeployment.created_on).toISOString(), + Author: latestDeployment.author_email, + Source: getDeploymentSource(latestDeployment), + Message: latestDeployment.annotations?.["workers/message"] || BLANK_INPUT, + "Version(s)": formattedVersions.join("\n\n"), + }); - logRaw(formattedDeployment); -} + logRaw(formattedDeployment); + }, +}); diff --git a/packages/wrangler/src/versions/deployments/view.ts b/packages/wrangler/src/versions/deployments/view.ts index 79e401685827..b12e1e6eef57 100644 --- a/packages/wrangler/src/versions/deployments/view.ts +++ b/packages/wrangler/src/versions/deployments/view.ts @@ -1,41 +1,36 @@ +import { createCommand } from "../../core/create-command"; import { UserError } from "../../errors"; -import { printWranglerBanner } from "../../wrangler-banner"; -import type { - CommonYargsArgv, - StrictYargsOptionsToInterface, -} from "../../yargs-types"; -export type VersionsDeploymentsViewArgs = StrictYargsOptionsToInterface< - typeof versionsDeploymentsViewOptions ->; - -export function versionsDeploymentsViewOptions(yargs: CommonYargsArgv) { - return yargs - .option("name", { - describe: "Name of the worker", +export const deploymentsViewCommand = createCommand({ + metadata: { + description: "View a deployment of a Worker", + owner: "Workers: Authoring and Testing", + status: "stable", + hidden: true, + }, + args: { + name: { + describe: "Name of the Worker", type: "string", requiresArg: true, - }) - .positional("deployment-id", { + }, + ["deployment-id"]: { describe: "Deprecated. Deployment ID is now referred to as Version ID. Please use `wrangler versions view [version-id]` instead.", type: "string", requiresArg: true, - }); -} - -export async function versionsDeploymentsViewHandler( - args: VersionsDeploymentsViewArgs -) { - await printWranglerBanner(); - - if (args.deploymentId === undefined) { - throw new UserError( - "`wrangler deployments view` has been renamed `wrangler deployments status`. Please use that command instead." - ); - } else { - throw new UserError( - "`wrangler deployments view ` has been renamed `wrangler versions view [version-id]`. Please use that command instead." - ); - } -} + }, + }, + positionalArgs: ["deployment-id"], + handler: async function versionsDeploymentsViewHandler(args) { + if (args.deploymentId === undefined) { + throw new UserError( + "`wrangler deployments view` has been renamed `wrangler deployments status`. Please use that command instead." + ); + } else { + throw new UserError( + "`wrangler deployments view ` has been renamed `wrangler versions view [version-id]`. Please use that command instead." + ); + } + }, +});