Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/compact-preview-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@cloudflare/deploy-helpers": patch
"wrangler": patch
---

Compact `wrangler preview` deployment success output

`wrangler preview` now prints a concise success summary with the Preview name, Preview URL, deployment ID, and Deployment URL instead of the previous box-art settings summary.
349 changes: 20 additions & 329 deletions packages/deploy-helpers/src/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
getPreviewDeployment,
getWorkerPreviewDefaults,
} from "./api";
import { drawBox, drawConnectedChildBox } from "./box";
import { formatAlignedRows, formatBindings } from "./format";
import {
assemblePreviewScriptSettings,
extractConfigBindings,
Expand All @@ -45,55 +43,6 @@ type PreviewDeploymentModule = {
content_base64: string;
};

type MergedBinding = Binding & { fromConfig: boolean };

type MergedScriptLevel = {
observability?: {
enabled?: boolean;
head_sampling_rate?: number;
fromConfig: boolean;
};
logpush?: {
value: boolean;
fromConfig: boolean;
};
tail_consumers?: Array<{ name: string }>;
};

type MergedVersionLevel = {
compatibility_date?: {
value: string;
fromConfig: boolean;
};
compatibility_flags?: {
value: string[];
fromConfig: boolean;
};
limits?: {
value: Config["limits"];
fromConfig: boolean;
};
placement?: {
value: { mode: string };
fromConfig: boolean;
};
cache?: {
value: Config["cache"];
fromConfig: boolean;
};
assets?: {
value: {
directory?: string;
binding?: string;
html_handling?: string;
not_found_handling?: string;
run_worker_first?: string[] | boolean;
};
fromConfig: boolean;
};
env: Record<string, MergedBinding>;
};

export type PreviewArgs = {
script?: string;
name?: string;
Expand Down Expand Up @@ -336,285 +285,36 @@ async function assemblePreviewDeploymentSettings(
return request;
}

function buildMergedScriptLevel(
config: Config,
previewResource: PreviewResource
): MergedScriptLevel {
const previews = config.previews as PreviewsConfig | undefined;
const result: MergedScriptLevel = {};
const configHasObservability =
previews?.observability !== undefined || config.observability !== undefined;
const configHasLogpush =
previews?.logpush !== undefined || config.logpush !== undefined;

if (previewResource.observability !== undefined) {
result.observability = {
enabled: previewResource.observability.enabled,
head_sampling_rate: previewResource.observability.head_sampling_rate,
fromConfig: configHasObservability,
};
}

if (previewResource.logpush !== undefined) {
result.logpush = {
value: previewResource.logpush,
fromConfig: configHasLogpush,
};
}

if (
previewResource.tail_consumers &&
previewResource.tail_consumers.length > 0
) {
result.tail_consumers = previewResource.tail_consumers;
function formatUrlLines(label: string, urls: string[] | undefined): string[] {
if (urls === undefined || urls.length === 0) {
return [];
}

return result;
}

function buildMergedVersionLevel(
config: Config,
deployment: DeploymentResource
): MergedVersionLevel {
const previews = config.previews as PreviewsConfig | undefined;
const configBindingNames = new Set(
Object.keys(extractConfigBindings(config))
);
const result: MergedVersionLevel = { env: {} };

if (deployment.compatibility_date) {
result.compatibility_date = {
value: deployment.compatibility_date,
fromConfig: !!config.compatibility_date,
};
}
if (
deployment.compatibility_flags &&
deployment.compatibility_flags.length > 0
) {
result.compatibility_flags = {
value: deployment.compatibility_flags,
fromConfig: !!(
config.compatibility_flags && config.compatibility_flags.length > 0
),
};
}
if (
deployment.limits?.cpu_ms !== undefined ||
deployment.limits?.subrequests !== undefined
) {
result.limits = {
value: {
...(deployment.limits?.cpu_ms !== undefined && {
cpu_ms: deployment.limits.cpu_ms,
}),
...(deployment.limits?.subrequests !== undefined && {
subrequests: deployment.limits.subrequests,
}),
},
fromConfig: !!(
previews?.limits !== undefined || config.limits !== undefined
),
};
}
if (deployment.placement?.mode) {
result.placement = {
value: { mode: deployment.placement.mode },
fromConfig: !!config.placement?.mode,
};
}
if (deployment.cache !== undefined) {
result.cache = {
value: deployment.cache,
fromConfig: previews?.cache !== undefined || config.cache !== undefined,
};
}
if (config.assets) {
result.assets = {
value: {
directory: config.assets.directory,
binding: config.assets.binding,
html_handling: config.assets.html_handling,
not_found_handling: config.assets.not_found_handling,
run_worker_first: config.assets.run_worker_first,
},
fromConfig: true,
};
}
for (const [name, binding] of Object.entries(deployment.env ?? {})) {
result.env[name] = { ...binding, fromConfig: configBindingNames.has(name) };
const firstUrl = urls[0];
if (urls.length === 1 && firstUrl !== undefined) {
return [`${chalk.bold(`${label} URL:`)} ${chalk.underline(firstUrl)}`];
}

return result;
return [
chalk.bold(`${label} URLs:`),
...urls.map((url) => ` ${chalk.underline(url)}`),
];
}

function formatPreviewResource(
function formatPreviewDeploymentSummary(
previewResource: PreviewResource,
scriptLevel: MergedScriptLevel,
isNew: boolean,
configName: string
deployment: DeploymentResource,
isNew: boolean
): string {
const statusLabel = isNew ? chalk.green("(new)") : chalk.dim("(updated)");
const obsEnabled = scriptLevel.observability?.enabled ?? false;
const obsRate = scriptLevel.observability?.head_sampling_rate;
const formattedRate = obsRate !== undefined ? obsRate.toFixed(1) : undefined;
const obsValue = obsEnabled
? `enabled${
formattedRate !== undefined ? `, ${formattedRate} sampling` : ""
}`
: "disabled";

const lines: string[] = [
`${chalk.bold("Preview:")} ${previewResource.name} ${statusLabel}`,
"",
...(previewResource.urls ?? []).map(
(url) => ` ${chalk.bold.underline(url)}`
),
];

const settingsRows: Array<[string, string, boolean]> = [];
if (scriptLevel.observability !== undefined) {
settingsRows.push([
"observability",
obsValue,
scriptLevel.observability.fromConfig,
]);
}
if (scriptLevel.logpush !== undefined) {
settingsRows.push([
"logpush",
scriptLevel.logpush.value ? "enabled" : "disabled",
scriptLevel.logpush.fromConfig,
]);
}
if (scriptLevel.tail_consumers && scriptLevel.tail_consumers.length > 0) {
settingsRows.push([
"tail_consumers",
scriptLevel.tail_consumers.map((tc) => tc.name).join(", "),
false,
]);
}
if (settingsRows.length > 0) {
lines.push("");
lines.push(...formatAlignedRows(settingsRows));
}

const hasConfigValues = settingsRows.some(([, , fromConfig]) => fromConfig);
const footerLines = hasConfigValues
? ["", chalk.hex("#FFA500")(`◆ from ${configName}`)]
: undefined;

return drawBox(lines, { footerLines, connectToChild: true });
}

function formatDeploymentResource(
deployment: DeploymentResource,
versionLevel: MergedVersionLevel,
configName: string
): string {
const lines: string[] = [
`${chalk.bold("Deployment:")} ${deployment.id}`,
return [
`${chalk.bold("Preview:")} ${previewResource.name} ${statusLabel}`,
...formatUrlLines("Preview", previewResource.urls),
"",
...(deployment.urls ?? []).map((url) => ` ${chalk.bold.underline(url)}`),
];

const settingsRows: Array<[string, string, boolean]> = [];
if (versionLevel.compatibility_date) {
settingsRows.push([
"compatibility_date",
versionLevel.compatibility_date.value,
versionLevel.compatibility_date.fromConfig,
]);
}
if (versionLevel.compatibility_flags) {
settingsRows.push([
"compatibility_flags",
versionLevel.compatibility_flags.value.join(", "),
versionLevel.compatibility_flags.fromConfig,
]);
}
if (
versionLevel.limits?.value?.cpu_ms !== undefined ||
versionLevel.limits?.value?.subrequests !== undefined
) {
const limitParts = [
versionLevel.limits?.value?.cpu_ms !== undefined
? `cpu_ms: ${versionLevel.limits.value.cpu_ms}`
: undefined,
versionLevel.limits?.value?.subrequests !== undefined
? `subrequests: ${versionLevel.limits.value.subrequests}`
: undefined,
].filter((value): value is string => value !== undefined);
settingsRows.push([
"limits",
limitParts.join(", "),
versionLevel.limits.fromConfig,
]);
}
if (versionLevel.placement) {
settingsRows.push([
"placement",
versionLevel.placement.value.mode,
versionLevel.placement.fromConfig,
]);
}
if (versionLevel.cache !== undefined) {
settingsRows.push([
"cache",
versionLevel.cache.value?.enabled ? "enabled" : "disabled",
versionLevel.cache.fromConfig,
]);
}
if (settingsRows.length > 0) {
lines.push("");
lines.push(...formatAlignedRows(settingsRows));
}

if (versionLevel.assets) {
lines.push("");
lines.push(chalk.bold(" Assets"));
const assetsRows: Array<[string, string, boolean]> = [];
const assets = versionLevel.assets.value;
const fromConfig = versionLevel.assets.fromConfig;
if (assets.directory) {
assetsRows.push(["directory", assets.directory, fromConfig]);
}
if (assets.binding) {
assetsRows.push(["binding", assets.binding, fromConfig]);
}
if (assets.html_handling) {
assetsRows.push(["html_handling", assets.html_handling, fromConfig]);
}
if (assets.not_found_handling) {
assetsRows.push([
"not_found_handling",
assets.not_found_handling,
fromConfig,
]);
}
if (assets.run_worker_first !== undefined) {
const value =
typeof assets.run_worker_first === "boolean"
? String(assets.run_worker_first)
: assets.run_worker_first.join(", ");
assetsRows.push(["run_worker_first", value, fromConfig]);
}
lines.push(...formatAlignedRows(assetsRows, " "));
}

lines.push("");
lines.push(chalk.bold(" Bindings"));
lines.push(...formatBindings(versionLevel.env));

const hasConfigValues =
settingsRows.some(([, , fromConfig]) => fromConfig) ||
versionLevel.assets?.fromConfig ||
Object.values(versionLevel.env).some((binding) => binding.fromConfig);
const footerLines = hasConfigValues
? ["", chalk.hex("#FFA500")(`◆ from ${configName}`)]
: undefined;

return drawConnectedChildBox(lines, { footerLines, indent: " " });
`${chalk.bold("Deployment ID:")} ${deployment.id}`,
...formatUrlLines("Deployment", deployment.urls),
].join("\n");
}

function logMissingPreviewsBindingsWarning(
Expand Down Expand Up @@ -749,18 +449,9 @@ export async function preview(
JSON.stringify({ preview: previewResource, deployment }, null, 2)
);
} else {
const scriptLevel = buildMergedScriptLevel(config, previewResource);
const versionLevel = buildMergedVersionLevel(config, deployment);
const configName = configFileName(config.configPath);
logger.log(
formatPreviewResource(
previewResource,
scriptLevel,
isNewPreview,
configName
)
formatPreviewDeploymentSummary(previewResource, deployment, isNewPreview)
);
logger.log(formatDeploymentResource(deployment, versionLevel, configName));

const topLevelBindings = getBindings(config);
if (Object.keys(topLevelBindings).length > 0) {
Expand Down
Loading
Loading