Skip to content

Commit 0e91d42

Browse files
authored
fix: ensure logged in when choosing account during pre-existing worker C3 flow (#7034)
* fix: ensure that user is logged in before trying to choose account during pre-existing Worker C3 flow * chore: add changeset * test: add unit test for configure function * chore: remove spurious files
1 parent f9d5fdb commit 0e91d42

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

.changeset/sixty-pants-attend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
fix: ensure that user is logged in before trying to choose account during pre-existing Worker C3 flow
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { buildConfigure } from "../../templates/pre-existing/c3";
3+
import type { ConfigureParams } from "../../templates/pre-existing/c3";
4+
import type { C3Context } from "types";
5+
6+
describe("configure function", () => {
7+
const ctx = {
8+
args: { deploy: true },
9+
} as C3Context;
10+
11+
it("should successfully configure when login is successful", async () => {
12+
const params: ConfigureParams = {
13+
login: vi.fn().mockResolvedValue(true),
14+
chooseAccount: vi.fn().mockResolvedValue(undefined),
15+
copyFiles: vi.fn().mockResolvedValue(undefined),
16+
};
17+
18+
const configure = buildConfigure(params);
19+
await configure(ctx);
20+
21+
expect(params.login).toHaveBeenCalledWith(ctx);
22+
expect(params.chooseAccount).toHaveBeenCalledWith(ctx);
23+
expect(params.copyFiles).toHaveBeenCalledWith(ctx);
24+
expect(ctx.args.deploy).toBe(false);
25+
});
26+
27+
it("should throw an error when login fails", async () => {
28+
const params: ConfigureParams = {
29+
login: vi.fn().mockResolvedValue(false),
30+
chooseAccount: vi.fn(),
31+
copyFiles: vi.fn(),
32+
};
33+
34+
const configure = buildConfigure(params);
35+
await expect(configure(ctx)).rejects.toThrow(
36+
"Failed to login to Cloudflare",
37+
);
38+
expect(params.chooseAccount).not.toHaveBeenCalled();
39+
expect(params.copyFiles).not.toHaveBeenCalled();
40+
});
41+
});

packages/create-cloudflare/templates/pre-existing/c3.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { brandColor, dim } from "@cloudflare/cli/colors";
55
import { processArgument } from "helpers/args";
66
import { runCommand } from "helpers/command";
77
import { detectPackageManager } from "helpers/packageManagers";
8-
import { chooseAccount } from "../../src/wrangler/accounts";
8+
import { chooseAccount, wranglerLogin } from "../../src/wrangler/accounts";
99
import type { C3Context } from "types";
1010

1111
export async function copyExistingWorkerFiles(ctx: C3Context) {
1212
const { dlx } = detectPackageManager();
1313

14-
await chooseAccount(ctx);
15-
1614
if (ctx.args.existingScript === undefined) {
1715
ctx.args.existingScript = await processArgument(
1816
ctx.args,
@@ -74,10 +72,31 @@ export default {
7472
copyFiles: {
7573
path: "./js",
7674
},
77-
configure: async (ctx: C3Context) => {
78-
await copyExistingWorkerFiles(ctx);
75+
configure: buildConfigure({
76+
login: wranglerLogin,
77+
chooseAccount,
78+
copyFiles: copyExistingWorkerFiles,
79+
}),
80+
};
81+
82+
export interface ConfigureParams {
83+
login: (ctx: C3Context) => Promise<boolean>;
84+
chooseAccount: (ctx: C3Context) => Promise<void>;
85+
copyFiles: (ctx: C3Context) => Promise<void>;
86+
}
87+
88+
export function buildConfigure(params: ConfigureParams) {
89+
return async function configure(ctx: C3Context) {
90+
const loginSuccess = await params.login(ctx);
91+
92+
if (!loginSuccess) {
93+
throw new Error("Failed to login to Cloudflare");
94+
}
95+
96+
await params.chooseAccount(ctx);
97+
await params.copyFiles(ctx);
7998

8099
// Force no-deploy since the worker is already deployed
81100
ctx.args.deploy = false;
82-
},
83-
};
101+
};
102+
}

0 commit comments

Comments
 (0)