diff --git a/.changeset/short-humans-repeat.md b/.changeset/short-humans-repeat.md new file mode 100644 index 000000000000..9b662e718730 --- /dev/null +++ b/.changeset/short-humans-repeat.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Allow the developer to exit `init` if there is already a toml file diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 8b95b5ddd3df..c01496824ed3 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -5,6 +5,7 @@ import * as TOML from "@iarna/toml"; import { main } from "../index"; import { setMock, unsetAllMocks } from "./mock-cfetch"; import { existsSync } from "node:fs"; +import { dialogs } from "../dialogs"; jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch")); @@ -112,10 +113,28 @@ describe("wrangler", () => { expect(typeof parsed.compatibility_date).toBe("string"); }); - it("should error when wrangler.toml already exists", async () => { + it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => { fs.closeSync(fs.openSync("./wrangler.toml", "w")); + mockConfirm({ + text: "Do you want to continue initializing this project?", + result: false, + }); const { stderr } = await w("init"); expect(stderr).toContain("wrangler.toml file already exists!"); + const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8")); + expect(typeof parsed.compatibility_date).toBe("undefined"); + }); + + it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => { + fs.closeSync(fs.openSync("./wrangler.toml", "w")); + mockConfirm({ + text: "Do you want to continue initializing this project?", + result: true, + }); + const { stderr } = await w("init"); + expect(stderr).toContain("wrangler.toml file already exists!"); + const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8")); + expect(typeof parsed.compatibility_date).toBe("string"); }); it("should error if `--type` is used", async () => { @@ -203,3 +222,22 @@ describe("wrangler", () => { }); }); }); + +/** + * Create a mock version of `confirm()` that will respond with configured results + * for configured confirmation text messages. + * + * If there is a call to `confirm()` that does not match any of the expectations + * then an error is thrown. + */ +function mockConfirm(...expectations: { text: string; result: boolean }[]) { + const mockImplementation = async (text: string) => { + for (const { text: expectedText, result } of expectations) { + if (text === expectedText) { + return result; + } + } + throw new Error(`Unexpected confirmation message: ${text}`); + }; + return jest.spyOn(dialogs, "confirm").mockImplementation(mockImplementation); +} diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 9c331d39c837..3adaf8a7e5ce 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -201,25 +201,29 @@ export async function main(argv: string[]): Promise { const destination = path.join(process.cwd(), "wrangler.toml"); if (fs.existsSync(destination)) { - console.error( - `${destination} file already exists! Please remove it before running this command again.` + console.error(`${destination} file already exists!`); + const result = await dialogs.confirm( + "Do you want to continue initializing this project?" ); - } else { - const compatibilityDate = new Date().toISOString().substring(0, 10); - try { - await writeFile( - destination, - `compatibility_date = "${compatibilityDate}"` + "\n" - ); - console.log(`✨ Succesfully created wrangler.toml`); - // TODO: suggest next steps? - } catch (err) { - console.error(`Failed to create wrangler.toml`); - console.error(err); - throw err; + if (!result) { + return; } } + const compatibilityDate = new Date().toISOString().substring(0, 10); + try { + await writeFile( + destination, + `compatibility_date = "${compatibilityDate}"` + "\n" + ); + console.log(`✨ Succesfully created wrangler.toml`); + // TODO: suggest next steps? + } catch (err) { + console.error(`Failed to create wrangler.toml`); + console.error(err); + throw err; + } + // if no package.json, ask, and if yes, create one let pathToPackageJson = await findUp("package.json");