Skip to content

Commit 080f33f

Browse files
committed
feat: extract subdomain deploy into its own function
Should make the logic easier to understand.
1 parent 76d9aa2 commit 080f33f

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
@@ -51,9 +51,6 @@ export default async function triggersDeploy(
5151
}
5252
}
5353

54-
// deployToWorkersDev defaults to true only if there aren't any routes defined
55-
const deployToWorkersDev = config.workers_dev ?? routes.length === 0;
56-
5754
if (!scriptName) {
5855
throw new UserError(
5956
'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>"`',
@@ -70,14 +67,6 @@ export default async function triggersDeploy(
7067
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
7168
: `/accounts/${accountId}/workers/scripts/${scriptName}`;
7269

73-
const {
74-
enabled: available_on_subdomain,
75-
previews_enabled: previews_available_on_subdomain,
76-
} = await fetchResult<{
77-
enabled: boolean;
78-
previews_enabled: boolean;
79-
}>(config, `${workerUrl}/subdomain`);
80-
8170
if (!props.dryRun) {
8271
await ensureQueuesExistByConfig(config);
8372
}
@@ -94,55 +83,17 @@ export default async function triggersDeploy(
9483
const uploadMs = Date.now() - start;
9584
const deployments: Promise<string[]>[] = [];
9685

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

0 commit comments

Comments
 (0)