Skip to content

Commit e78b667

Browse files
committed
fix(publish): harden cloudflare wrangler usage
1 parent 2b533ac commit e78b667

1 file changed

Lines changed: 98 additions & 36 deletions

File tree

‎src/publish/cloudflare/cloudflare.ts‎

Lines changed: 98 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ async function publish(
5858
projectName = await promptForProjectName(projectName);
5959
}
6060

61+
if (!options.prompt) {
62+
await ensureProjectExists(projectName);
63+
}
64+
6165
await deployWithWrangler(
6266
publishFiles.baseDir,
6367
projectName,
@@ -66,7 +70,7 @@ async function publish(
6670
const deploymentUrl = await deploymentUrlFromWrangler(projectName);
6771
const recordUrl = target?.url ??
6872
(deploymentUrl
69-
? canonicalUrlFromDeployment(deploymentUrl) || deploymentUrl
73+
? canonicalUrlFromDeployment(deploymentUrl, projectName) || deploymentUrl
7074
: defaultProjectUrl(projectName));
7175
const openUrl = target?.url ?? deploymentUrl ?? recordUrl;
7276

@@ -97,27 +101,18 @@ function defaultProjectUrl(projectName: string) {
97101
}
98102

99103
let wranglerChecked = false;
104+
type WranglerCommand = {
105+
cmd: string;
106+
args: string[];
107+
};
108+
let wranglerCommand: WranglerCommand | undefined;
100109

101110
async function ensureWrangler() {
102111
if (wranglerChecked) {
103112
return;
104113
}
105-
try {
106-
const result = await execProcess({
107-
cmd: "wrangler",
108-
args: ["--version"],
109-
stdout: "null",
110-
stderr: "null",
111-
});
112-
if (!result.success) {
113-
throw new Error();
114-
}
115-
wranglerChecked = true;
116-
} catch {
117-
throw new Error(
118-
"Cloudflare Pages publishing requires the Wrangler CLI (https://developers.cloudflare.com/workers/wrangler/install/).",
119-
);
120-
}
114+
await resolveWranglerCommand();
115+
wranglerChecked = true;
121116
}
122117

123118
async function deployWithWrangler(
@@ -126,15 +121,13 @@ async function deployWithWrangler(
126121
) {
127122
await ensureWrangler();
128123

129-
const result = await execProcess({
130-
cmd: "wrangler",
131-
args: [
132-
"pages",
133-
"deploy",
134-
dir,
135-
"--project-name",
136-
projectName,
137-
],
124+
const result = await execWrangler([
125+
"pages",
126+
"deploy",
127+
dir,
128+
"--project-name",
129+
projectName,
130+
], {
138131
stdout: "inherit",
139132
stderr: "inherit",
140133
});
@@ -153,20 +146,37 @@ type WranglerDeployment = {
153146
branch?: string;
154147
};
155148

149+
async function ensureProjectExists(projectName: string) {
150+
const result = await execWrangler([
151+
"pages",
152+
"deployment",
153+
"list",
154+
"--project-name",
155+
projectName,
156+
"--json",
157+
], {
158+
stdout: "piped",
159+
stderr: "piped",
160+
});
161+
if (!result.success) {
162+
throw new Error(
163+
`Unable to access Cloudflare Pages project '${projectName}'. Create it first with 'wrangler pages project create ${projectName}' or run without --no-prompt.`,
164+
);
165+
}
166+
}
167+
156168
async function deploymentUrlFromWrangler(
157169
projectName: string,
158170
): Promise<string | undefined> {
159171
try {
160-
const result = await execProcess({
161-
cmd: "wrangler",
162-
args: [
163-
"pages",
164-
"deployment",
165-
"list",
166-
"--project-name",
167-
projectName,
168-
"--json",
169-
],
172+
const result = await execWrangler([
173+
"pages",
174+
"deployment",
175+
"list",
176+
"--project-name",
177+
projectName,
178+
"--json",
179+
], {
170180
stdout: "piped",
171181
stderr: "piped",
172182
});
@@ -191,17 +201,69 @@ async function deploymentUrlFromWrangler(
191201

192202
function canonicalUrlFromDeployment(
193203
deploymentUrl: string,
204+
projectName: string,
194205
): string | undefined {
195206
try {
196207
const url = new URL(deploymentUrl);
197208
const host = url.hostname.toLowerCase();
198209
if (host.endsWith(".pages.dev")) {
199210
const parts = host.split(".");
200211
if (parts.length >= 4) {
212+
const project = parts[1];
213+
const prefix = parts[0];
214+
if (project !== projectName.toLowerCase()) {
215+
return undefined;
216+
}
217+
if (!/^[0-9a-f]{6,}$/i.test(prefix)) {
218+
return undefined;
219+
}
201220
return `${url.protocol}//${parts.slice(1).join(".")}`;
202221
}
203222
}
204223
} catch {
205224
return undefined;
206225
}
207226
}
227+
228+
async function resolveWranglerCommand(): Promise<WranglerCommand> {
229+
if (wranglerCommand) {
230+
return wranglerCommand;
231+
}
232+
const wranglerResult = await execProcess({
233+
cmd: "wrangler",
234+
args: ["--version"],
235+
stdout: "null",
236+
stderr: "null",
237+
});
238+
if (wranglerResult.success) {
239+
wranglerCommand = { cmd: "wrangler", args: [] };
240+
return wranglerCommand;
241+
}
242+
243+
const npxResult = await execProcess({
244+
cmd: "npx",
245+
args: ["wrangler", "--version"],
246+
stdout: "null",
247+
stderr: "null",
248+
});
249+
if (npxResult.success) {
250+
wranglerCommand = { cmd: "npx", args: ["wrangler"] };
251+
return wranglerCommand;
252+
}
253+
254+
throw new Error(
255+
"Cloudflare Pages publishing requires the Wrangler CLI (https://developers.cloudflare.com/workers/wrangler/install/). You can also run via npx if Wrangler is installed locally.",
256+
);
257+
}
258+
259+
async function execWrangler(
260+
args: string[],
261+
options: Deno.CommandOptions = {},
262+
) {
263+
const command = await resolveWranglerCommand();
264+
return await execProcess({
265+
cmd: command.cmd,
266+
...options,
267+
args: [...command.args, ...args],
268+
});
269+
}

0 commit comments

Comments
 (0)