Skip to content

Commit 2525ba9

Browse files
committed
feat(publish): derive cloudflare url from wrangler
1 parent ecc0c3a commit 2525ba9

1 file changed

Lines changed: 67 additions & 9 deletions

File tree

‎src/publish/cloudflare/cloudflare.ts‎

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { anonymousAccount } from "../common/git.ts";
2020
export const kCloudflarePages = "cloudflare-pages";
2121
const kCloudflarePagesDescription = "Cloudflare Pages";
2222

23-
const kDefaultProductionBranch = "main";
24-
2523
export const cloudflarePagesProvider: PublishProvider = {
2624
name: kCloudflarePages,
2725
description: kCloudflarePagesDescription,
@@ -63,17 +61,23 @@ async function publish(
6361
await deployWithWrangler(
6462
publishFiles.baseDir,
6563
projectName,
66-
kDefaultProductionBranch,
6764
);
6865

69-
const url = defaultProjectUrl(projectName);
66+
const deploymentUrl = await deploymentUrlFromWrangler(projectName);
67+
const recordUrl = deploymentUrl
68+
? canonicalUrlFromDeployment(deploymentUrl) || deploymentUrl
69+
: defaultProjectUrl(projectName);
7070

7171
return [
7272
{
7373
id: projectName,
74-
url,
74+
url: recordUrl,
7575
},
76-
url ? new URL(url) : undefined,
76+
deploymentUrl
77+
? new URL(deploymentUrl)
78+
: recordUrl
79+
? new URL(recordUrl)
80+
: undefined,
7781
];
7882
}
7983

@@ -121,7 +125,6 @@ async function ensureWrangler() {
121125
async function deployWithWrangler(
122126
dir: string,
123127
projectName: string,
124-
productionBranch: string,
125128
) {
126129
await ensureWrangler();
127130

@@ -133,8 +136,6 @@ async function deployWithWrangler(
133136
dir,
134137
"--project-name",
135138
projectName,
136-
"--branch",
137-
productionBranch,
138139
],
139140
stdout: "inherit",
140141
stderr: "inherit",
@@ -146,3 +147,60 @@ async function deployWithWrangler(
146147
);
147148
}
148149
}
150+
151+
type WranglerDeployment = {
152+
Deployment?: string;
153+
deployment?: string;
154+
Branch?: string;
155+
branch?: string;
156+
};
157+
158+
async function deploymentUrlFromWrangler(
159+
projectName: string,
160+
): Promise<string | undefined> {
161+
try {
162+
const result = await execProcess({
163+
cmd: "wrangler",
164+
args: [
165+
"pages",
166+
"deployment",
167+
"list",
168+
"--project-name",
169+
projectName,
170+
"--environment",
171+
"production",
172+
"--json",
173+
],
174+
stdout: "piped",
175+
stderr: "piped",
176+
});
177+
if (!result.success || !result.stdout.trim()) {
178+
return undefined;
179+
}
180+
const deployments = JSON.parse(result.stdout) as WranglerDeployment[];
181+
if (!Array.isArray(deployments) || deployments.length === 0) {
182+
return undefined;
183+
}
184+
const deployment = deployments[0];
185+
return deployment.Deployment || deployment.deployment;
186+
} catch {
187+
return undefined;
188+
}
189+
}
190+
191+
function canonicalUrlFromDeployment(
192+
deploymentUrl: string,
193+
): string | undefined {
194+
try {
195+
const url = new URL(deploymentUrl);
196+
const host = url.hostname;
197+
if (host.endsWith(".pages.dev")) {
198+
const parts = host.split(".");
199+
if (parts.length >= 3) {
200+
return `${url.protocol}//${parts.slice(1).join(".")}`;
201+
}
202+
}
203+
} catch {
204+
return undefined;
205+
}
206+
}

0 commit comments

Comments
 (0)