diff --git a/.changeset/triggers-deploy-show-diff.md b/.changeset/triggers-deploy-show-diff.md new file mode 100644 index 0000000000..dd149783fa --- /dev/null +++ b/.changeset/triggers-deploy-show-diff.md @@ -0,0 +1,9 @@ +--- +"wrangler": minor +--- + +Add `--show-diff` flag to `wrangler triggers deploy` + +When `--show-diff` is passed, the command fetches the currently deployed trigger configuration from the remote worker and displays a colored diff against the local configuration. This covers routes, custom domains, cron schedules, and the `workers_dev`/`preview_urls` subdomain settings. + +The flag also works with `--dry-run` to preview what trigger changes would be applied without actually deploying. diff --git a/packages/deploy-helpers/src/shared/types.ts b/packages/deploy-helpers/src/shared/types.ts index 229cf88c7a..917607432f 100644 --- a/packages/deploy-helpers/src/shared/types.ts +++ b/packages/deploy-helpers/src/shared/types.ts @@ -205,6 +205,7 @@ export type TriggerProps = { routes: Route[]; firstDeploy: boolean; dryRun: boolean; + showDiff?: boolean; validated: boolean; queueCreateCommand: (queueName: string) => string; workersDevConfigFieldName: string; diff --git a/packages/deploy-helpers/src/triggers/deploy.ts b/packages/deploy-helpers/src/triggers/deploy.ts index 72df05c0b0..cfb3b05c98 100644 --- a/packages/deploy-helpers/src/triggers/deploy.ts +++ b/packages/deploy-helpers/src/triggers/deploy.ts @@ -10,6 +10,8 @@ import { } from "@cloudflare/workers-utils"; import chalk from "chalk"; import PQueue from "p-queue"; +import { diffJsonObjects } from "../deploy/helpers/diff-json"; +import { fetchWorkerConfig } from "../deploy/helpers/download-worker-config"; import { WORKFLOW_CRON_REQUIRES_PAID_PLAN_CODE, WORKFLOW_JURISDICTION_IMMUTABLE_CODE, @@ -33,8 +35,13 @@ import { } from "./queue-consumers"; import { getWorkerSubdomain, getWorkersDevSubdomain } from "./subdomain"; import { getZoneForRoute } from "./zones"; +import type { JsonLike } from "../deploy/helpers/diff-json"; import type { TriggerDeployment, TriggerProps } from "../shared/types"; -import type { Config, Route } from "@cloudflare/workers-utils"; +import type { + Config, + CustomDomainRoute, + Route, +} from "@cloudflare/workers-utils"; export const PREVIEW_DOMAIN_PROVISIONING_NOTE = "Note: DNS and TLS certificate provisioning for Preview domains may continue after this deploy. If a new Preview URL does not work immediately, wait a few minutes and retry."; @@ -49,7 +56,7 @@ export async function triggersDeploy( validateOwnedWorkflowDeclarations(config, scriptName); } - if (props.dryRun) { + if (props.dryRun && !props.showDiff) { logger.log(`--dry-run: exiting now.`); return; } @@ -57,6 +64,20 @@ export async function triggersDeploy( const { accountId } = props; assert(accountId); + const { routesOnly, customDomainsOnly } = partitionRoutes(routes); + const remoteTriggersSnapshot = props.showDiff + ? await fetchRemoteTriggers(config, accountId, scriptName) + : undefined; + + if (props.dryRun) { + displayTriggerDiff( + remoteTriggersSnapshot, + buildLocalTriggers(config, routes, routesOnly, customDomainsOnly, crons) + ); + logger.log(`--dry-run: exiting now.`); + return; + } + if (props.validated !== true) { await ensureQueuesExistByConfig( config, @@ -67,8 +88,6 @@ export async function triggersDeploy( ); } - const { routesOnly, customDomainsOnly } = partitionRoutes(routes); - const start = Date.now(); const workerUrl = `/accounts/${accountId}/workers/scripts/${scriptName}`; @@ -614,9 +633,171 @@ export async function triggersDeploy( ); } + if (props.showDiff) { + displayTriggerDiff( + remoteTriggersSnapshot, + buildLocalTriggers(config, routes, routesOnly, customDomainsOnly, crons) + ); + } + return targets; } +/** + * Normalizes a route for comparison, excluding API-only fields. + */ +function normalizeRoute(route: Route): Record { + if (typeof route === "string") { + return { pattern: route }; + } + const normalized: Record = { pattern: route.pattern }; + if ("zone_name" in route && route.zone_name) { + normalized.zone_name = route.zone_name; + } + if ("zone_id" in route && route.zone_id) { + normalized.zone_id = route.zone_id; + } + if (route.custom_domain) { + normalized.custom_domain = true; + } + return normalized; +} + +/** + * Builds the normalized local trigger configuration to compare with the remote. + */ +function buildLocalTriggers( + config: Config, + routes: Route[], + routesOnly: Route[], + customDomainsOnly: CustomDomainRoute[], + crons: string[] | undefined +): Record { + const { workers_dev, preview_urls } = getSubdomainValuesAPIMock( + config.workers_dev, + config.preview_urls, + routes + ); + const triggers: Record = { workers_dev, preview_urls }; + + const normalizedRoutes = routesOnly + .map(normalizeRoute) + .sort((firstRoute, secondRoute) => + String(firstRoute.pattern ?? "").localeCompare( + String(secondRoute.pattern ?? "") + ) + ); + if (normalizedRoutes.length > 0) { + triggers.routes = normalizedRoutes; + } + + const normalizedCustomDomains = customDomainsOnly + .map(normalizeRoute) + .sort((firstRoute, secondRoute) => + String(firstRoute.pattern ?? "").localeCompare( + String(secondRoute.pattern ?? "") + ) + ); + if (normalizedCustomDomains.length > 0) { + triggers.custom_domains = normalizedCustomDomains; + } + + if (crons && crons.length > 0) { + triggers.crons = [...crons].sort(); + } + + return triggers; +} + +/** + * Fetches and normalizes the deployed routes, domains, crons and subdomain settings. + */ +async function fetchRemoteTriggers( + config: Config, + accountId: string, + workerName: string +): Promise> { + const { routes, customDomains, subdomainStatus, cronTriggers } = + await fetchWorkerConfig( + config, + accountId, + workerName, + "production", + true, + true + ); + + const triggers: Record = { + workers_dev: subdomainStatus.enabled, + preview_urls: subdomainStatus.previews_enabled, + }; + + const normalizedRoutes = routes + .map((route) => { + const normalized: Record = { pattern: route.pattern }; + if (route.zone_name) { + normalized.zone_name = route.zone_name; + } + return normalized; + }) + .sort((firstRoute, secondRoute) => + String(firstRoute.pattern ?? "").localeCompare( + String(secondRoute.pattern ?? "") + ) + ); + if (normalizedRoutes.length > 0) { + triggers.routes = normalizedRoutes; + } + + const normalizedCustomDomains = customDomains + .map((domain) => { + const normalized: Record = { pattern: domain.hostname }; + if (domain.zone_name) { + normalized.zone_name = domain.zone_name; + } + normalized.custom_domain = true; + return normalized; + }) + .sort((firstRoute, secondRoute) => + String(firstRoute.pattern ?? "").localeCompare( + String(secondRoute.pattern ?? "") + ) + ); + if (normalizedCustomDomains.length > 0) { + triggers.custom_domains = normalizedCustomDomains; + } + + const crons = cronTriggers.schedules.map((schedule) => schedule.cron).sort(); + if (crons.length > 0) { + triggers.crons = crons; + } + + return triggers; +} + +/** + * Displays the differences between the deployed and local trigger configurations. + */ +function displayTriggerDiff( + remoteTriggers: Record | undefined, + localTriggers: Record +): void { + if (!remoteTriggers) { + return; + } + + const diff = diffJsonObjects(remoteTriggers, localTriggers); + + logger.log(""); + if (diff) { + logger.log( + `Trigger changes (remote ${chalk.red("\u2192")} local):\n${diff}` + ); + } else { + logger.log("No trigger changes detected."); + } +} + /** * Collapse the telemetry labels of a set of inner errors into a single sorted, * deduplicated string. Each `UserError` contributes its own `telemetryMessage`; diff --git a/packages/wrangler/src/triggers/index.ts b/packages/wrangler/src/triggers/index.ts index 8d5a867942..54cc7a4c75 100644 --- a/packages/wrangler/src/triggers/index.ts +++ b/packages/wrangler/src/triggers/index.ts @@ -49,6 +49,11 @@ export const triggersDeployCommand = createCommand({ type: "boolean", default: false, }, + "show-diff": { + describe: "Show the diff between remote and local trigger configuration", + type: "boolean", + default: false, + }, "experimental-deploy-helpers": { describe: "Experimental: Gates refactored deploy/upload path", type: "boolean", @@ -71,13 +76,15 @@ export const triggersDeployCommand = createCommand({ sendMetrics: config.send_metrics, }); const props = resolveTriggersInput(args, config); - const accountId = args.dryRun ? undefined : await requireAuth(config); + const accountId = + args.dryRun && !args.showDiff ? undefined : await requireAuth(config); await triggersDeploy({ config, accountId, firstDeploy: false, dryRun: args.dryRun, + showDiff: args.showDiff, validated: false, queueCreateCommand: (queueName) => `wrangler queues create ${queueName}`, workersDevConfigFieldName: "workers_dev",