Skip to content

Commit

Permalink
add helpful logging to --experimental-versions commands (#5419)
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Mar 28, 2024
1 parent 9343714 commit daac6a2
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-brooms-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: add helpful logging to --experimental-versions commands
112 changes: 93 additions & 19 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const status = {
warning: bgYellow(` WARNING `),
info: bgBlue(` INFO `),
success: bgGreen(` SUCCESS `),
cancel: white.bgRed(` X `),
};

// Returns a string containing n non-trimmable spaces
Expand All @@ -66,18 +67,46 @@ export const newline = () => {
log("");
};

type FormatOptions = {
linePrefix?: string;
firstLinePrefix?: string;
newlineBefore?: boolean;
newlineAfter?: boolean;
formatLine?: (line: string) => string;
multiline?: boolean;
};
export const format = (
msg: string,
{
linePrefix = gray(shapes.bar),
firstLinePrefix = linePrefix,
newlineBefore = false,
newlineAfter = false,
formatLine = (line: string) => white(line),
multiline = true,
}: FormatOptions = {}
) => {
const lines = multiline ? msg.split("\n") : [msg];
const formattedLines = lines.map(
(line, i) =>
(i === 0 ? firstLinePrefix : linePrefix) + space() + formatLine(line)
);

if (newlineBefore) formattedLines.unshift(linePrefix);
if (newlineAfter) formattedLines.push(linePrefix);

return formattedLines.join("\n");
};

// Log a simple status update with a style similar to the clack spinner
export const updateStatus = (msg: string, printNewLine = true) => {
const lines = msg.split("\n");
const restLines = lines
.slice(1)
.map((ln) => `${gray(shapes.bar)} ${white(ln)}`);
logRaw(`${gray(shapes.leftT)} ${lines[0]}`);
if (restLines.length) {
logRaw(restLines.join("\n"));
}

if (printNewLine) newline();
logRaw(
format(msg, {
firstLinePrefix: gray(shapes.leftT),
linePrefix: gray(shapes.bar),
newlineAfter: printNewLine,
})
);
};

export const startSection = (
Expand All @@ -101,19 +130,64 @@ export const endSection = (heading: string, subheading?: string) => {
);
};

export const cancel = (msg: string) => {
newline();
logRaw(`${gray(shapes.corners.bl)} ${white.bgRed(` X `)} ${dim(msg)}`);
export const cancel = (
msg: string,
{
// current default is backcompat and makes sense going forward too
shape = shapes.corners.bl,
// current default for backcompat -- TODO: change default to true once all callees have been updated
multiline = false,
} = {}
) => {
logRaw(
format(msg, {
firstLinePrefix: `${gray(shape)} ${status.cancel}`,
linePrefix: gray(shapes.bar),
newlineBefore: true,
formatLine: (line) => dim(line), // for backcompat but it's not ideal for this to be "dim"
multiline,
})
);
};

export const warn = (msg: string) => {
newline();
logRaw(`${gray(shapes.corners.bl)} ${status.warning} ${dim(msg)}`);
export const warn = (
msg: string,
{
// current default for backcompat -- TODO: change default to shapes.bar once all callees have been updated
shape = shapes.corners.bl,
// current default for backcompat -- TODO: change default to true once all callees have been updated
multiline = false,
} = {}
) => {
logRaw(
format(msg, {
firstLinePrefix: gray(shape) + space() + status.warning,
linePrefix: gray(shapes.bar),
newlineBefore: true,
formatLine: (line) => dim(line), // for backcompat but it's not ideal for this to be "dim"
multiline,
})
);
};

export const success = (msg: string) => {
newline();
logRaw(`${gray(shapes.corners.bl)} ${status.success} ${dim(msg)}`);
export const success = (
msg: string,
{
// current default for backcompat -- TODO: change default to shapes.bar once all callees have been updated
shape = shapes.corners.bl,
// current default for backcompat -- TODO: change default to true once all callees have been updated
multiline = false,
} = {}
) => {
logRaw(
format(msg, {
firstLinePrefix: gray(shape) + space() + status.success,
linePrefix: gray(shapes.bar),
newlineBefore: true,
formatLine: (line) => dim(line), // for backcompat but it's not ideal for this to be "dim"
multiline,
})
);
};

// Strip the ansi color characters out of the line when calculating
Expand Down
20 changes: 16 additions & 4 deletions packages/wrangler/e2e/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ describe("versions deploy", () => {
expect(normalize(upload.stdout)).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Version ID: 00000000-0000-0000-0000-000000000000
Uploaded tmp-e2e-wrangler (TIMINGS)"
Uploaded tmp-e2e-wrangler (TIMINGS)
To deploy this version to production traffic use the command wrangler versions deploy --experimental-versions
NOTE: Changes to non-versioned settings (config properties 'logpush' or 'tail_consumers') take effect after your next deployment. Use the command wrangler versions deploy --experimental-versions to deploy these changes
NOTE: Changes to triggers (routes, custom domains, cron schedules, etc) must be applied with the command wrangler triggers deploy --experimental-versions"
`);
});

Expand Down Expand Up @@ -185,7 +188,10 @@ describe("versions deploy", () => {
expect(normalize(upload.stdout)).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Version ID: 00000000-0000-0000-0000-000000000000
Uploaded tmp-e2e-wrangler (TIMINGS)"
Uploaded tmp-e2e-wrangler (TIMINGS)
To deploy this version to production traffic use the command wrangler versions deploy --experimental-versions
NOTE: Changes to non-versioned settings (config properties 'logpush' or 'tail_consumers') take effect after your next deployment. Use the command wrangler versions deploy --experimental-versions to deploy these changes
NOTE: Changes to triggers (routes, custom domains, cron schedules, etc) must be applied with the command wrangler triggers deploy --experimental-versions"
`);

const versionsList =
Expand Down Expand Up @@ -319,7 +325,10 @@ describe("versions deploy", () => {
│ Message Rollback via e2e test
╰ WARNING You are about to rollback to Worker Version 00000000-0000-0000-0000-000000000000:
├ WARNING You are about to rollback to Worker Version 00000000-0000-0000-0000-000000000000.
│ This will immediately replace the current deployment and become the active deployment across all your deployed triggers.
│ However, your local development environment will not be affected by this rollback.
│ Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).
│ (100%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
Expand Down Expand Up @@ -427,7 +436,10 @@ describe("versions deploy", () => {
│ Message Rollback to old version
╰ WARNING You are about to rollback to Worker Version 00000000-0000-0000-0000-000000000000:
├ WARNING You are about to rollback to Worker Version 00000000-0000-0000-0000-000000000000.
│ This will immediately replace the current deployment and become the active deployment across all your deployed triggers.
│ However, your local development environment will not be affected by this rollback.
│ Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).
│ (100%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
Expand Down
10 changes: 5 additions & 5 deletions packages/wrangler/src/__tests__/deployments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("deployments", () => {

it("should successfully rollback and output a success message", async () => {
mockConfirm({
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).",
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).",
result: true,
});

Expand All @@ -313,7 +313,7 @@ describe("deployments", () => {

it("should early exit from rollback if user denies continuing", async () => {
mockConfirm({
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).",
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).",
result: false,
});

Expand All @@ -335,7 +335,7 @@ describe("deployments", () => {
expect(std.out).toMatchInlineSnapshot(`
"🚧\`wrangler rollback\` is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose

? This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).
? This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).
🤖 Using fallback value in non-interactive context: yes
? Please provide a message for this rollback (120 characters max)
🤖 Using default value in non-interactive context:
Expand Down Expand Up @@ -390,7 +390,7 @@ describe("deployments", () => {

it("should automatically rollback to previous deployment when id is not specified", async () => {
mockConfirm({
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).",
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).",
result: true,
});

Expand Down Expand Up @@ -425,7 +425,7 @@ describe("deployments", () => {

it("should automatically rollback to previous deployment with specified name", async () => {
mockConfirm({
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).",
text: "This deployment 3mEgaU1T will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. Note: Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).",
result: true,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export async function rollbackDeployment(
firstHash
)} will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. ${chalk.blue.bold(
"Note:"
)} Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).`
)} Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).`
))
) {
return;
Expand Down
7 changes: 5 additions & 2 deletions packages/wrangler/src/versions/rollback/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export async function versionsRollbackHandler(args: VersionsRollbackArgs) {
});

const version = await fetchVersion(accountId, workerName, versionId);
cli.warn(`You are about to rollback to Worker Version ${versionId}:`);
cli.warn(
`You are about to rollback to Worker Version ${versionId}.\nThis will immediately replace the current deployment and become the active deployment across all your deployed triggers.\nHowever, your local development environment will not be affected by this rollback.\nRolling back to a previous deployment will not rollback any of the bound resources (Durable Object, D1, R2, KV, etc).`,
{ multiline: true, shape: cli.shapes.leftT }
);
const rollbackTraffic = new Map([[versionId, 100]]);
printVersions([version], rollbackTraffic);

Expand All @@ -102,7 +105,7 @@ export async function versionsRollbackHandler(args: VersionsRollbackArgs) {
});

if (!confirm) {
cli.log("Aborting rollback...");
cli.cancel("Aborting rollback...");
return;
}

Expand Down
17 changes: 17 additions & 0 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { URLSearchParams } from "node:url";
import { blue, gray } from "@cloudflare/cli/colors";
import { fetchResult } from "../cfetch";
import { printBindings } from "../config";
import { bundleWorker } from "../deployment-bundle/bundle";
Expand Down Expand Up @@ -500,6 +501,22 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
const uploadMs = Date.now() - start;

logger.log("Uploaded", workerName, formatTime(uploadMs));

const cmdVersionsDeploy = blue(
"wrangler versions deploy --experimental-versions"
);
const cmdTriggersDeploy = blue(
"wrangler triggers deploy --experimental-versions"
);
logger.info(
gray(`
To deploy this version to production traffic use the command ${cmdVersionsDeploy}

Changes to non-versioned settings (config properties 'logpush' or 'tail_consumers') take effect after your next deployment using the command ${cmdVersionsDeploy}

Changes to triggers (routes, custom domains, cron schedules, etc) must be applied with the command ${cmdTriggersDeploy}
`)
);
}

export function helpIfErrorIsSizeOrScriptStartup(
Expand Down

0 comments on commit daac6a2

Please sign in to comment.