Skip to content

Commit 7c70628

Browse files
committed
feat: extract subdomain deploy into its own function
Should make the logic easier to understand.
1 parent fd73b69 commit 7c70628

File tree

1 file changed

+81
-60
lines changed

1 file changed

+81
-60
lines changed

packages/wrangler/src/triggers/deploy.ts

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ export default async function triggersDeploy(
6060
}
6161
}
6262

63-
// deployToWorkersDev defaults to true only if there aren't any routes defined
64-
const deployToWorkersDev = getResolvedWorkersDev(config.workers_dev, routes);
65-
6663
if (!scriptName) {
6764
throw new UserError(
6865
'You need to provide a name when uploading a Worker Version. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`',
@@ -79,14 +76,6 @@ export default async function triggersDeploy(
7976
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
8077
: `/accounts/${accountId}/workers/scripts/${scriptName}`;
8178

82-
const {
83-
enabled: available_on_subdomain,
84-
previews_enabled: previews_available_on_subdomain,
85-
} = await fetchResult<{
86-
enabled: boolean;
87-
previews_enabled: boolean;
88-
}>(config, `${workerUrl}/subdomain`);
89-
9079
if (!props.dryRun) {
9180
await ensureQueuesExistByConfig(config);
9281
}
@@ -103,55 +92,17 @@ export default async function triggersDeploy(
10392
const uploadMs = Date.now() - start;
10493
const deployments: Promise<string[]>[] = [];
10594

106-
const deploymentInSync = deployToWorkersDev === available_on_subdomain;
107-
const previewsInSync =
108-
config.preview_urls === previews_available_on_subdomain;
109-
110-
if (deployToWorkersDev) {
111-
// Deploy to a subdomain of `workers.dev`
112-
const userSubdomain = await getWorkersDevSubdomain(
113-
config,
114-
accountId,
115-
config.configPath
116-
);
117-
118-
const deploymentURL =
119-
props.legacyEnv || !props.env
120-
? `${scriptName}.${userSubdomain}`
121-
: `${envName}.${scriptName}.${userSubdomain}`;
122-
123-
if (deploymentInSync && previewsInSync) {
124-
deployments.push(Promise.resolve([deploymentURL]));
125-
} else {
126-
// Enable the `workers.dev` subdomain.
127-
deployments.push(
128-
fetchResult(config, `${workerUrl}/subdomain`, {
129-
method: "POST",
130-
body: JSON.stringify({
131-
enabled: true,
132-
previews_enabled: config.preview_urls,
133-
}),
134-
headers: {
135-
"Content-Type": "application/json",
136-
},
137-
}).then(() => [deploymentURL])
138-
);
139-
}
140-
}
141-
if (!deployToWorkersDev && (!deploymentInSync || !previewsInSync)) {
142-
// Disable the workers.dev deployment
143-
await fetchResult(config, `${workerUrl}/subdomain`, {
144-
method: "POST",
145-
body: JSON.stringify({
146-
enabled: false,
147-
previews_enabled: config.preview_urls,
148-
}),
149-
headers: {
150-
"Content-Type": "application/json",
151-
},
152-
});
153-
}
154-
if (!deployToWorkersDev && deploymentInSync && routes.length !== 0) {
95+
const { wantWorkersDev, workersDevInSync } = await subdomainDeploy(
96+
props,
97+
accountId,
98+
scriptName,
99+
envName,
100+
workerUrl,
101+
routes,
102+
deployments
103+
);
104+
105+
if (!wantWorkersDev && workersDevInSync && routes.length !== 0) {
155106
// TODO is this true? How does last subdomain status affect route confict??
156107
// Why would we only need to validate route conflicts if didn't need to
157108
// disable the subdomain deployment?
@@ -349,3 +300,73 @@ export default async function triggersDeploy(
349300
logger.log("No deploy targets for", workerName, formatTime(deployMs));
350301
}
351302
}
303+
304+
async function subdomainDeploy(
305+
props: Props,
306+
accountId: string,
307+
scriptName: string,
308+
envName: string,
309+
workerUrl: string,
310+
routes: Route[],
311+
deployments: Array<Promise<string[]>>
312+
) {
313+
const { config } = props;
314+
315+
// Get desired subdomain enablement status.
316+
317+
const defaultWorkersDev = routes.length === 0;
318+
const wantWorkersDev = config.workers_dev ?? defaultWorkersDev;
319+
const wantPreviews = config.preview_urls ?? false;
320+
321+
// Get current subdomain enablement status.
322+
323+
const { enabled: currWorkersDev, previews_enabled: currPreviews } =
324+
await fetchResult<{
325+
enabled: boolean;
326+
previews_enabled: boolean;
327+
}>(config, `${workerUrl}/subdomain`);
328+
329+
const workersDevInSync = wantWorkersDev === currWorkersDev;
330+
const previewsInSync = wantPreviews === currPreviews;
331+
const allInSync = [workersDevInSync, previewsInSync].every((v) => v);
332+
333+
// workers.dev URL is only set if we want to deploy to workers.dev.
334+
335+
let workersDevURL: string | undefined;
336+
if (wantWorkersDev) {
337+
const userSubdomain = await getWorkersDevSubdomain(
338+
config,
339+
accountId,
340+
config.configPath
341+
);
342+
workersDevURL =
343+
props.legacyEnv || !props.env
344+
? `${scriptName}.${userSubdomain}`
345+
: `${envName}.${scriptName}.${userSubdomain}`;
346+
}
347+
348+
// Update subdomain enablement status if needed.
349+
350+
if (!allInSync) {
351+
await fetchResult(config, `${workerUrl}/subdomain`, {
352+
method: "POST",
353+
body: JSON.stringify({
354+
enabled: wantWorkersDev,
355+
previews_enabled: wantPreviews,
356+
}),
357+
headers: {
358+
"Content-Type": "application/json",
359+
},
360+
});
361+
}
362+
363+
if (workersDevURL) {
364+
deployments.push(Promise.resolve([workersDevURL]));
365+
}
366+
return {
367+
wantWorkersDev,
368+
wantPreviews,
369+
workersDevInSync,
370+
previewsInSync,
371+
};
372+
}

0 commit comments

Comments
 (0)