Skip to content

Commit

Permalink
fix: wrangler deploy prompts warning with deployment styling
Browse files Browse the repository at this point in the history
Styling of the warning about multiple deployments during
`wrangler deploy` should be consistent with `wrangler versions`
styling.
  • Loading branch information
Daniel Rivas committed Jun 12, 2024
1 parent aa18a35 commit cc0fd7c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 72 deletions.
3 changes: 2 additions & 1 deletion packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ export const warn = (
shape = shapes.corners.bl,
// current default for backcompat -- TODO: change default to true once all callees have been updated
multiline = false,
newlineBefore = true,
} = {}
) => {
logRaw(
format(msg, {
firstLinePrefix: gray(shape) + space() + status.warning,
linePrefix: gray(shapes.bar),
newlineBefore: true,
newlineBefore: newlineBefore,
formatLine: (line) => dim(line), // for backcompat but it's not ideal for this to be "dim"
multiline,
})
Expand Down
33 changes: 1 addition & 32 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ import {
mswSuccessOauthHandlers,
mswSuccessUserHandlers,
} from "./helpers/msw";
import {
mswListNewDeploymentsLatestFiftyFifty,
mswListNewDeploymentsLatestFull,
} from "./helpers/msw/handlers/versions";
import { mswListNewDeploymentsLatestFull } from "./helpers/msw/handlers/versions";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import { writeWorkerSource } from "./helpers/write-worker-source";
Expand Down Expand Up @@ -428,34 +425,6 @@ describe("deploy", () => {
`);
});

it("should warn user when worker has deployment with multiple versions", async () => {
msw.use(...mswListNewDeploymentsLatestFiftyFifty);
writeWranglerToml();
writeWorkerSource();
mockSubDomainRequest();
mockUploadWorkerRequest();
mockConfirm({
text: `"wrangler deploy" will upload a new version and deploy it globally immediately.\nAre you sure you want to continue?`,
result: false,
});

await runWrangler("deploy ./index");

expect(std.warn).toMatchInlineSnapshot(`
"▲ [WARNING] Your last deployment has multiple versions. To progress this deployment use \\"wrangler versions deploy\\" instead.
Currently deployed versions:
Version(s): (50%) test-name:version:0
Created: 2021-01-01T00:00:00.000Z
(50%) test-name:version:1
Created: 2021-01-01T00:00:00.000Z
"
`);
});

it("should warn user when additional properties are passed to a services config", async () => {
writeWranglerToml({
d1_databases: [
Expand Down
41 changes: 41 additions & 0 deletions packages/wrangler/src/__tests__/versions/versions.deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ import { collectCLIOutput } from "../helpers/collect-cli-output";
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
import { mockConsoleMethods } from "../helpers/mock-console";
import { useMockIsTTY } from "../helpers/mock-istty";
import { mockUploadWorkerRequest } from "../helpers/mock-upload-worker";
import { mockSubDomainRequest } from "../helpers/mock-workers-subdomain";
import {
msw,
mswGetVersion,
mswListNewDeployments,
mswListVersions,
mswPatchNonVersionedScriptSettings,
mswPostNewDeployment,
mswSuccessDeploymentScriptMetadata,
} from "../helpers/msw";
import { mswListNewDeploymentsLatestFiftyFifty } from "../helpers/msw/handlers/versions";
import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import { writeWorkerSource } from "../helpers/write-worker-source";
import writeWranglerToml from "../helpers/write-wrangler-toml";
import type { VersionsDeployArgs } from "../../versions/deploy";

Expand All @@ -43,6 +48,42 @@ describe("versions deploy", () => {
);
});

describe("legacy deploy", () => {
test("should warn user when worker has deployment with multiple versions", async () => {
msw.use(
...mswSuccessDeploymentScriptMetadata,
...mswListNewDeploymentsLatestFiftyFifty
);
writeWranglerToml();
writeWorkerSource();
mockSubDomainRequest();
mockUploadWorkerRequest();

await runWrangler("deploy ./index");

expect(normalizeOutput(std.out)).toMatchInlineSnapshot(`
"╭ WARNING Your last deployment has multiple versions. To progress this deployment use \\"wrangler versions deploy\\" instead.
├ Your last deployment has 2 version(s):
│ (50%) test-name:version:0
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
│ (50%) test-name:version:1
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
├ \\"wrangler deploy\\" will upload a new version and deploy it globally immediately.
Are you sure you want to continue?
│ yes
│"
`);
});
});

describe("without wrangler.toml", () => {
test("succeeds with --name arg", async () => {
const result = runWrangler(
Expand Down
78 changes: 39 additions & 39 deletions packages/wrangler/src/versions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { findWranglerToml, readConfig } from "../config";
import { confirm } from "../dialogs";

Check failure on line 12 in packages/wrangler/src/versions/deploy.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'confirm' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 12 in packages/wrangler/src/versions/deploy.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'confirm' is defined but never used
import { UserError } from "../errors";
import { CI } from "../is-ci";
import isInteractive from "../is-interactive";
import { logger } from "../logger";

Check failure on line 16 in packages/wrangler/src/versions/deploy.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'logger' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 16 in packages/wrangler/src/versions/deploy.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'logger' is defined but never used
import * as metrics from "../metrics";
import { APIError } from "../parse";
Expand Down Expand Up @@ -225,42 +227,35 @@ export async function confirmLatestDeploymentOverwrite(
const versionIds = latest.versions.map((v) => v.version_id);
await fetchVersions(accountId, scriptName, versionCache, ...versionIds);

// Format each version.

const formattedVersions = latest.versions.map((traffic) => {
const version = versionCache.get(traffic.version_id);
const versions = latest.versions.map((v) => {
const version = versionCache.get(v.version_id);
assert(version);
const percentage = brandColor(`(${traffic.percentage}%)`);
const details = formatLabelledValues(
{ Created: new Date(version.metadata["created_on"]).toISOString() },
{
indentationCount: 4,
labelJustification: "right",
formatLabel: (label) => gray(label + ":"),
formatValue: (value) => gray(value),
}
);
return `${percentage} ${version.id}\n${details}`;
return version;
});

// Format deployment.

const formattedDeployment = formatLabelledValues({
"Version(s)": formattedVersions.join("\n\n"),
});
const traffic = new Map(
latest.versions.map((v) => [v.version_id, v.percentage])
);

// Print message and confirmation.

logger.warn(
`Your last deployment has multiple versions. To progress this deployment use "wrangler versions deploy" instead.
Currently deployed versions:
${formattedDeployment}`
cli.warn(
`Your last deployment has multiple versions. To progress this deployment use "wrangler versions deploy" instead.`,
{ shape: cli.shapes.corners.tl, newlineBefore: false }
);

return await confirm(
`"wrangler deploy" will upload a new version and deploy it globally immediately.\nAre you sure you want to continue?`
cli.newline();
cli.logRaw(
`${leftT} Your last deployment has ${versions.length} version(s):`
);
printVersions(versions, traffic);

return inputPrompt<boolean>({
type: "confirm",
question: `"wrangler deploy" will upload a new version and deploy it globally immediately.\nAre you sure you want to continue?`,
label: "",
defaultValue: true, // defaults to true to keep current behavior.
acceptDefault: !isInteractive() || CI.isCI(),
});
}
} catch (e) {
const isNotFound = e instanceof APIError && e.code == 10007;
Expand Down Expand Up @@ -294,20 +289,25 @@ export function printVersions(
versions: ApiVersion[],
traffic: Map<VersionId, Percentage>
) {
for (const version of versions) {
const trafficString = brandColor(`(${traffic.get(version.id)}%)`);
const versionIdString = white(version.id);
cli.newline();
cli.log(formatVersions(versions, traffic));
cli.newline();
}

cli.log(
gray(`
${trafficString} ${versionIdString}
export function formatVersions(
versions: ApiVersion[],
traffic: Map<VersionId, Percentage>
) {
return versions
.map((version) => {
const trafficString = brandColor(`(${traffic.get(version.id)}%)`);
const versionIdString = white(version.id);
return gray(`${trafficString} ${versionIdString}
Created: ${version.metadata.created_on}
Tag: ${version.annotations?.["workers/tag"] ?? BLANK_INPUT}
Message: ${version.annotations?.["workers/message"] ?? BLANK_INPUT}`)
);
}

cli.newline();
Message: ${version.annotations?.["workers/message"] ?? BLANK_INPUT}`);
})
.join("\n\n");
}

/**
Expand Down

0 comments on commit cc0fd7c

Please sign in to comment.