Skip to content

Commit 7be8f76

Browse files
authored
Run deploy interactively in C3 (#6569)
1 parent 9524be5 commit 7be8f76

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

.changeset/smart-drinks-joke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
Make sure `wrangler deploy` and `wrangler pages deploy` are run in an interactive context in C3, so that users are able to respond to interactive prompts

packages/create-cloudflare/src/__tests__/deploy.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ describe("deploy helpers", async () => {
130130
ctx.template.platform = "pages";
131131
ctx.commitMessage = commitMsg;
132132
mockInsideGitRepo(false);
133-
vi.mocked(runCommand).mockResolvedValueOnce(deployedUrl);
134-
133+
vi.mocked(runCommand).mockResolvedValueOnce("");
134+
vi.mocked(readFile).mockImplementationOnce(
135+
() => `{"type":"deploy", "targets":["${deployedUrl}"]}`,
136+
);
135137
await runDeploy(ctx);
136138
expect(runCommand).toHaveBeenCalledWith(
137139
["npm", "run", "deploy", "--", "--commit-message", `"${commitMsg}"`],

packages/create-cloudflare/src/deploy.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { mkdtemp } from "node:fs/promises";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
14
import { startSection, updateStatus } from "@cloudflare/cli";
25
import { blue, brandColor, dim } from "@cloudflare/cli/colors";
36
import TOML from "@iarna/toml";
47
import { processArgument } from "helpers/args";
58
import { C3_DEFAULTS, openInBrowser } from "helpers/cli";
69
import { quoteShellArgs, runCommand } from "helpers/command";
10+
import { readFile } from "helpers/files";
711
import { detectPackageManager } from "helpers/packageManagers";
812
import { poll } from "helpers/poll";
913
import { isInsideGitRepo } from "./git";
@@ -99,24 +103,42 @@ export const runDeploy = async (ctx: C3Context) => {
99103
: []),
100104
];
101105

102-
const result = await runCommand(deployCmd, {
103-
silent: true,
106+
const outputFile = join(
107+
await mkdtemp(join(tmpdir(), "c3-wrangler-deploy-")),
108+
"output.json",
109+
);
110+
111+
await runCommand(deployCmd, {
104112
cwd: ctx.project.path,
105113
env: {
106114
CLOUDFLARE_ACCOUNT_ID: ctx.account.id,
107115
NODE_ENV: "production",
116+
WRANGLER_OUTPUT_FILE_PATH: outputFile,
108117
},
109118
startText: "Deploying your application",
110119
doneText: `${brandColor("deployed")} ${dim(
111120
`via \`${quoteShellArgs(baseDeployCmd)}\``,
112121
)}`,
113122
});
114123

115-
const deployedUrlRegex = /https:\/\/.+\.(pages|workers)\.dev/;
116-
const deployedUrlMatch = result.match(deployedUrlRegex);
117-
if (deployedUrlMatch) {
118-
ctx.deployment.url = deployedUrlMatch[0];
119-
} else {
124+
try {
125+
const contents = readFile(outputFile);
126+
127+
const entries = contents
128+
.split("\n")
129+
.filter(Boolean)
130+
.map((entry) => JSON.parse(entry));
131+
const url: string | undefined =
132+
entries.find((entry) => entry.type === "deploy")?.targets?.[0] ??
133+
entries.find((entry) => entry.type === "pages-deploy")?.url;
134+
const deployedUrlRegex = /https:\/\/.+\.(pages|workers)\.dev/;
135+
const deployedUrlMatch = url?.match(deployedUrlRegex);
136+
if (deployedUrlMatch) {
137+
ctx.deployment.url = deployedUrlMatch[0];
138+
} else {
139+
throw new Error("Failed to find deployment url.");
140+
}
141+
} catch {
120142
throw new Error("Failed to find deployment url.");
121143
}
122144

0 commit comments

Comments
 (0)