Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: withwrangler init, test for existence of package.json/ tsconfig.json / .git in the right locations #924

Merged
merged 1 commit into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fresh-dots-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: with`wrangler init`, test for existence of `package.json`/ `tsconfig.json` / `.git` in the right locations

When running `wrangler.init`, we look for the existence of `package.json`, / `tsconfig.json` / `.git` when deciding whether we should create them ourselves or not. Because `name` can be a relative path, we had a bug where we don't starting look from the right directory. We also had a bug where we weren't even testing for the existence of the `.git` directory correctly. This patch fixes that initial starting location, tests for `.git` as a directory, and correctly decides when to create those files.
156 changes: 155 additions & 1 deletion packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ describe("init", () => {
expect(parsed.compatibility_date).toBe("something-else");
});

it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
fs.mkdirSync("path/to/worker", { recursive: true });
fs.writeFileSync(
"path/to/worker/wrangler.toml",
'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
"utf-8"
);
mockConfirm({
text: "Do you want to continue initializing this project?",
result: false,
});
await runWrangler("init path/to/worker");
expect(std.warn).toContain("wrangler.toml file already exists!");
const parsed = TOML.parse(
await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
);
expect(parsed.compatibility_date).toBe("something-else");
});

it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
fs.writeFileSync(
"./wrangler.toml",
Expand Down Expand Up @@ -305,7 +324,6 @@ describe("init", () => {
"debug": "",
"err": "",
"out": "✨ Successfully created wrangler.toml
✨ Initialized git repository
✨ Created package.json
✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
✨ Created src/index.ts
Expand All @@ -317,6 +335,30 @@ describe("init", () => {
`);
});

it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
fs.mkdirSync("path/to/worker", { recursive: true });
await execa("git", ["init"], { cwd: "path/to/worker" });
expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);

await runWrangler("init path/to/worker/my-worker -y");

// Note the lack of "✨ Initialized git repository" in the log
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "✨ Successfully created wrangler.toml
✨ Created package.json
✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
✨ Created src/index.ts

To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
To publish your Worker to the Internet, run \`npm run publish\`",
"warn": "",
}
`);
});

it.todo(
"should not offer to initialize a git repo if git is not installed"
);
Expand Down Expand Up @@ -417,6 +459,41 @@ describe("init", () => {
expect(packageJson.version).toEqual("1.0.0");
});

it("should not touch an existing package.json in a target directory", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to install wrangler into your package.json?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.js?",
result: false,
}
);

fs.mkdirSync("path/to/worker", { recursive: true });
fs.writeFileSync(
"path/to/worker/package.json",
JSON.stringify({ name: "test", version: "1.0.0" }),
"utf-8"
);

await runWrangler("init path/to/worker/my-worker");
const packageJson = JSON.parse(
fs.readFileSync("path/to/worker/package.json", "utf-8")
);
expect(packageJson.name).toEqual("test");
expect(packageJson.version).toEqual("1.0.0");
});

it("should offer to install wrangler into an existing package.json", async () => {
mockConfirm(
{
Expand Down Expand Up @@ -454,6 +531,45 @@ describe("init", () => {
);
});

it("should offer to install wrangler into a package.json relative to the target directory", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to install wrangler into your package.json?",
result: true,
},
{
text: "Would you like to use TypeScript?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.js?",
result: false,
}
);

fs.mkdirSync("path/to/worker", { recursive: true });
fs.writeFileSync(
"path/to/worker/package.json",
JSON.stringify({ name: "test", version: "1.0.0" }),
"utf-8"
);

await runWrangler("init path/to/worker/my-worker");
const packageJson = JSON.parse(
fs.readFileSync("path/to/worker/package.json", "utf-8")
);
expect(packageJson.name).toEqual("test");
expect(packageJson.version).toEqual("1.0.0");
expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
`wrangler@${wranglerVersion}`
);
expect(mockPackageManager.cwd).toBe(process.cwd());
});

it("should not touch an existing package.json in an ancestor directory", async () => {
mockConfirm(
{
Expand Down Expand Up @@ -761,6 +877,44 @@ describe("init", () => {
expect(tsconfigJson.compilerOptions).toEqual({});
});

it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
fs.mkdirSync("path/to/worker", { recursive: true });
fs.writeFileSync(
"path/to/worker/package.json",
JSON.stringify({
name: "test",
version: "1.0.0",
devDependencies: {
wrangler: "0.0.0",
"@cloudflare/workers-types": "0.0.0",
},
}),
"utf-8"
);
fs.writeFileSync(
"path/to/worker/tsconfig.json",
JSON.stringify({ compilerOptions: {} }),
"utf-8"
);

mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to create a Worker at src/index.ts?",
result: true,
}
);

await runWrangler("init path/to/worker/my-worker");
const tsconfigJson = JSON.parse(
fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
);
expect(tsconfigJson.compilerOptions).toEqual({});
});

it("should offer to install type definitions in an existing typescript project", async () => {
mockConfirm(
{
Expand Down
12 changes: 9 additions & 3 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ export async function main(argv: string[]): Promise<void> {

const yesFlag = args.yes ?? false;

const isInsideGitProject = Boolean(await findUp(".git"));
const isInsideGitProject = Boolean(
await findUp(".git", { cwd: creationDirectory, type: "directory" })
);
const isGitInstalled = (await execa("git", ["--version"])).exitCode === 0;
if (!isInsideGitProject && isGitInstalled) {
const shouldInitGit =
Expand All @@ -406,7 +408,9 @@ export async function main(argv: string[]): Promise<void> {
}
}

let pathToPackageJson = await findUp("package.json");
let pathToPackageJson = await findUp("package.json", {
cwd: creationDirectory,
});
let shouldCreatePackageJson = false;

if (!pathToPackageJson) {
Expand Down Expand Up @@ -466,7 +470,9 @@ export async function main(argv: string[]): Promise<void> {
}

let isTypescriptProject = false;
let pathToTSConfig = await findUp("tsconfig.json");
let pathToTSConfig = await findUp("tsconfig.json", {
cwd: creationDirectory,
});
if (!pathToTSConfig) {
// If there's no tsconfig, offer to create one
// and install @cloudflare/workers-types
Expand Down