Skip to content

Commit

Permalink
fix: allow the user to exit init if there is already a toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Dec 17, 2021
1 parent 70fa8d1 commit 23543fe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-humans-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Allow the developer to exit `init` if there is already a toml file
40 changes: 39 additions & 1 deletion packages/wrangler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
}
34 changes: 19 additions & 15 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,29 @@ export async function main(argv: string[]): Promise<void> {

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");

Expand Down

0 comments on commit 23543fe

Please sign in to comment.