Skip to content

Commit

Permalink
fix: ensure logged in when choosing account during pre-existing worke…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
andyjessop authored Oct 22, 2024
1 parent f9d5fdb commit 0e91d42
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-pants-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": patch
---

fix: ensure that user is logged in before trying to choose account during pre-existing Worker C3 flow
41 changes: 41 additions & 0 deletions packages/create-cloudflare/src/__tests__/pre-existing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it, vi } from "vitest";
import { buildConfigure } from "../../templates/pre-existing/c3";
import type { ConfigureParams } from "../../templates/pre-existing/c3";
import type { C3Context } from "types";

describe("configure function", () => {
const ctx = {
args: { deploy: true },
} as C3Context;

it("should successfully configure when login is successful", async () => {
const params: ConfigureParams = {
login: vi.fn().mockResolvedValue(true),
chooseAccount: vi.fn().mockResolvedValue(undefined),
copyFiles: vi.fn().mockResolvedValue(undefined),
};

const configure = buildConfigure(params);
await configure(ctx);

expect(params.login).toHaveBeenCalledWith(ctx);
expect(params.chooseAccount).toHaveBeenCalledWith(ctx);
expect(params.copyFiles).toHaveBeenCalledWith(ctx);
expect(ctx.args.deploy).toBe(false);
});

it("should throw an error when login fails", async () => {
const params: ConfigureParams = {
login: vi.fn().mockResolvedValue(false),
chooseAccount: vi.fn(),
copyFiles: vi.fn(),
};

const configure = buildConfigure(params);
await expect(configure(ctx)).rejects.toThrow(
"Failed to login to Cloudflare",
);
expect(params.chooseAccount).not.toHaveBeenCalled();
expect(params.copyFiles).not.toHaveBeenCalled();
});
});
33 changes: 26 additions & 7 deletions packages/create-cloudflare/templates/pre-existing/c3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { brandColor, dim } from "@cloudflare/cli/colors";
import { processArgument } from "helpers/args";
import { runCommand } from "helpers/command";
import { detectPackageManager } from "helpers/packageManagers";
import { chooseAccount } from "../../src/wrangler/accounts";
import { chooseAccount, wranglerLogin } from "../../src/wrangler/accounts";
import type { C3Context } from "types";

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

await chooseAccount(ctx);

if (ctx.args.existingScript === undefined) {
ctx.args.existingScript = await processArgument(
ctx.args,
Expand Down Expand Up @@ -74,10 +72,31 @@ export default {
copyFiles: {
path: "./js",
},
configure: async (ctx: C3Context) => {
await copyExistingWorkerFiles(ctx);
configure: buildConfigure({
login: wranglerLogin,
chooseAccount,
copyFiles: copyExistingWorkerFiles,
}),
};

export interface ConfigureParams {
login: (ctx: C3Context) => Promise<boolean>;
chooseAccount: (ctx: C3Context) => Promise<void>;
copyFiles: (ctx: C3Context) => Promise<void>;
}

export function buildConfigure(params: ConfigureParams) {
return async function configure(ctx: C3Context) {
const loginSuccess = await params.login(ctx);

if (!loginSuccess) {
throw new Error("Failed to login to Cloudflare");
}

await params.chooseAccount(ctx);
await params.copyFiles(ctx);

// Force no-deploy since the worker is already deployed
ctx.args.deploy = false;
},
};
};
}

0 comments on commit 0e91d42

Please sign in to comment.