Skip to content

Commit

Permalink
fixup! feat: support outputting ND-JSON files via an environment vari…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
petebacondarwin committed Aug 7, 2024
1 parent d281ed1 commit 916ccb4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 61 deletions.
12 changes: 8 additions & 4 deletions packages/wrangler/src/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ describe("writeOutput()", () => {
writeOutput({
type: "deployment",
version: 1,
worker_id: "Worker",
worker_name: "Worker",
worker_tag: "ABCDE12345",
deployment_id: "1234",
});

Expand All @@ -118,7 +119,8 @@ describe("writeOutput()", () => {
{
type: "deployment",
version: 1,
worker_id: "Worker",
worker_name: "Worker",
worker_tag: "ABCDE12345",
deployment_id: "1234",
},
]);
Expand Down Expand Up @@ -178,7 +180,8 @@ describe("writeOutput()", () => {
writeOutput({
type: "deployment",
version: 1,
worker_id: "Worker",
worker_name: "Worker",
worker_tag: "ABCDE12345",
deployment_id: "1234",
});

Expand All @@ -200,7 +203,8 @@ describe("writeOutput()", () => {
{
type: "deployment",
version: 1,
worker_id: "Worker",
worker_name: "Worker",
worker_tag: "ABCDE12345",
deployment_id: "1234",
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe("versions deploy", () => {
mswListVersions,
mswGetVersion(),
mswPostNewDeployment,
mswPatchNonVersionedScriptSettings
mswPatchNonVersionedScriptSettings,
...mswSuccessDeploymentScriptMetadata
);
});

Expand Down
45 changes: 26 additions & 19 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,35 +306,44 @@ Update them to point to this script instead?`;
return domains.map((domain) => renderRoute(domain));
}

export default async function deploy(
props: Props
): Promise<{ sourceMapSize?: number; deploymentId: string | null }> {
export default async function deploy(props: Props): Promise<{
sourceMapSize?: number;
deploymentId: string | null;
workerTag: string | null;
}> {
// TODO: warn if git/hg has uncommitted changes
const { config, accountId, name } = props;
let workerTag: string | null = null;
let deploymentId: string | null = null;

if (!props.dispatchNamespace && accountId && name) {
try {
const serviceMetaData = await fetchResult(
`/accounts/${accountId}/workers/services/${name}`
);
const { default_environment } = serviceMetaData as {
const serviceMetaData = await fetchResult<{
default_environment: {
script: { last_deployed_from: "dash" | "wrangler" | "api" };
script: {
tag: string;
last_deployed_from: "dash" | "wrangler" | "api";
};
};
};
}>(`/accounts/${accountId}/workers/services/${name}`);
const {
default_environment: { script },
} = serviceMetaData;
workerTag = script.tag;

if (default_environment.script.last_deployed_from === "dash") {
if (script.last_deployed_from === "dash") {
logger.warn(
`You are about to publish a Workers Service that was last published via the Cloudflare Dashboard.\nEdits that have been made via the dashboard will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { deploymentId: null };
return { deploymentId, workerTag };
}
} else if (default_environment.script.last_deployed_from === "api") {
} else if (script.last_deployed_from === "api") {
logger.warn(
`You are about to publish a Workers Service that was last updated via the script API.\nEdits that have been made via the script API will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { deploymentId: null };
return { deploymentId, workerTag };
}
}
} catch (e) {
Expand Down Expand Up @@ -436,15 +445,13 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
: `/accounts/${accountId}/workers/scripts/${scriptName}`;

let deploymentId: string | null = null;

const { format } = props.entry;

if (!props.dispatchNamespace && prod && accountId && scriptName) {
const yes = await confirmLatestDeploymentOverwrite(accountId, scriptName);
if (!yes) {
cancel("Aborting deploy...");
return { deploymentId: null };
return { deploymentId, workerTag };
}
}

Expand Down Expand Up @@ -828,7 +835,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m

if (props.dryRun) {
logger.log(`--dry-run: exiting now.`);
return { deploymentId: null };
return { deploymentId, workerTag };
}
assert(accountId, "Missing accountId");

Expand All @@ -839,7 +846,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
// Early exit for WfP since it doesn't need the below code
if (props.dispatchNamespace !== undefined) {
deployWfpUserWorker(props.dispatchNamespace, deploymentId);
return { deploymentId };
return { deploymentId, workerTag };
}

// deploy triggers
Expand All @@ -850,7 +857,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m

logVersionIdChange();

return { sourceMapSize, deploymentId };
return { sourceMapSize, deploymentId, workerTag };
}

function deployWfpUserWorker(
Expand Down
17 changes: 8 additions & 9 deletions packages/wrangler/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export async function deployHandler(

const beforeUpload = Date.now();
const name = getScriptName(args, config);
const { sourceMapSize, deploymentId } = await deploy({
const { sourceMapSize, deploymentId, workerTag } = await deploy({
config,
accountId,
name,
Expand Down Expand Up @@ -363,14 +363,13 @@ export async function deployHandler(
experimentalVersions: args.experimentalVersions,
});

if (deploymentId) {
writeOutput({
type: "deployment",
version: 1,
worker_id: name,
deployment_id: deploymentId,
});
}
writeOutput({
type: "deployment",
version: 1,
worker_name: name ?? null,
worker_tag: workerTag,
deployment_id: deploymentId,
});

await metrics.sendMetricsEvent(
"deploy worker script",
Expand Down
13 changes: 8 additions & 5 deletions packages/wrangler/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ export interface OutputEntrySession

export interface OutputEntryDeployment extends OutputEntryBase<"deployment"> {
version: 1;
worker_id: string | undefined;
deployment_id: string;
worker_name: string | null;
worker_tag: string | null;
deployment_id: string | null;
}

export interface OutputEntryVersionUpload
extends OutputEntryBase<"version-upload"> {
version: 1;
worker_id: string;
version_id: string;
worker_name: string | null;
worker_tag: string | null;
version_id: string | null;
}

export interface OutputEntryVersionDeployment
extends OutputEntryBase<"version-deploy"> {
version: 1;
worker_id: string;
worker_name: string | null;
worker_tag: string | null;
version_traffic: Map<string, number>;
}
13 changes: 12 additions & 1 deletion packages/wrangler/src/versions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
leftT,
spinnerWhile,
} from "@cloudflare/cli/interactive";
import { fetchResult } from "../cfetch";
import { findWranglerToml, readConfig } from "../config";
import { UserError } from "../errors";
import { CI } from "../is-ci";
Expand Down Expand Up @@ -205,10 +206,20 @@ export async function versionsDeployHandler(args: VersionsDeployArgs) {
`Deployed ${workerName} ${trafficSummaryString} (${elapsedString})`
);

let workerTag: string | null = null;
try {
const serviceMetaData = await fetchResult<{
default_environment: { script: { tag: string } };
}>(`/accounts/${accountId}/workers/services/${workerName}`);
workerTag = serviceMetaData.default_environment.script.tag;
} catch {
// If the fetch fails then we just output a null for the workerTag.
}
writeOutput({
type: "version-deploy",
version: 1,
worker_id: workerName,
worker_name: workerName,
worker_tag: workerTag,
version_traffic: confirmedVersionTraffic,
});
}
Expand Down
17 changes: 8 additions & 9 deletions packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export async function versionsUploadHandler(
const name = getScriptName(args, config);

await standardPricingWarning(config);
const { versionId } = await versionsUpload({
const { versionId, workerTag } = await versionsUpload({
config,
accountId,
name,
Expand Down Expand Up @@ -278,14 +278,13 @@ export async function versionsUploadHandler(
message: args.message,
});

if (versionId && name) {
writeOutput({
type: "version-upload",
version: 1,
worker_id: name,
version_id: versionId,
});
}
writeOutput({
type: "version-upload",
version: 1,
worker_name: name ?? null,
worker_tag: workerTag,
version_id: versionId,
});
}

export default function registerVersionsSubcommands(
Expand Down
39 changes: 26 additions & 13 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,48 @@ function errIsStartupErr(err: unknown): err is ParseError & { code: 10021 } {

export default async function versionsUpload(
props: Props
): Promise<{ versionId: string | null }> {
): Promise<{ versionId: string | null; workerTag: string | null }> {
// TODO: warn if git/hg has uncommitted changes
const { config, accountId, name } = props;
let versionId: string | null = null;
let workerTag: string | null = null;

if (accountId && name) {
try {
const serviceMetaData = await fetchResult(
`/accounts/${accountId}/workers/services/${name}` // TODO(consider): should this be a /versions endpoint?
);
const { default_environment } = serviceMetaData as {
const {
default_environment: { script },
} = await fetchResult<{
default_environment: {
script: { last_deployed_from: "dash" | "wrangler" | "api" };
script: {
tag: string;
last_deployed_from: "dash" | "wrangler" | "api";
};
};
};
}>(
`/accounts/${accountId}/workers/services/${name}` // TODO(consider): should this be a /versions endpoint?
);

workerTag = script.tag;

if (default_environment.script.last_deployed_from === "dash") {
if (script.last_deployed_from === "dash") {
logger.warn(
`You are about to upload a Worker Version that was last published via the Cloudflare Dashboard.\nEdits that have been made via the dashboard will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { versionId };
return {
versionId,
workerTag,
};
}
} else if (default_environment.script.last_deployed_from === "api") {
} else if (script.last_deployed_from === "api") {
logger.warn(
`You are about to upload a Workers Version that was last updated via the API.\nEdits that have been made via the API will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { versionId };
return {
versionId,
workerTag,
};
}
}
} catch (e) {
Expand Down Expand Up @@ -512,7 +525,7 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m

if (props.dryRun) {
logger.log(`--dry-run: exiting now.`);
return { versionId };
return { versionId, workerTag };
}
if (!accountId) {
throw new UserError("Missing accountId");
Expand All @@ -538,7 +551,7 @@ Changes to triggers (routes, custom domains, cron schedules, etc) must be applie
`)
);

return { versionId };
return { versionId, workerTag };
}

export function helpIfErrorIsSizeOrScriptStartup(
Expand Down

0 comments on commit 916ccb4

Please sign in to comment.