Skip to content

Commit

Permalink
fix: do not consider ancestor files when initializing a project with …
Browse files Browse the repository at this point in the history
…a specified name

When initializing a new project (via `wrangler init`) we attempt to reuse files in the current
directory, or in an ancestor directory. In particular we look up the directory tree for
package.json and tsconfig.json and use those instead of creating new ones.

Now we only do this if you do not specify a name for the new Worker. If you do specify a name,
we now only consider files in the directory where the Worker will be initialized.

Fixes cloudflare#859
  • Loading branch information
petebacondarwin committed Jun 15, 2022
1 parent c4d71cc commit 43ec49b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 23 deletions.
14 changes: 14 additions & 0 deletions .changeset/quiet-pigs-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"wrangler": patch
---

fix: do not consider ancestor files when initializing a project with a specified name

When initializing a new project (via `wrangler init`) we attempt to reuse files in the current
directory, or in an ancestor directory. In particular we look up the directory tree for
package.json and tsconfig.json and use those instead of creating new ones.

Now we only do this if you do not specify a name for the new Worker. If you do specify a name,
we now only consider files in the directory where the Worker will be initialized.

Fixes #859
127 changes: 110 additions & 17 deletions packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,14 +757,18 @@ describe("init", () => {
`);
});

it("should not touch an existing package.json in a target directory", async () => {
it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to install wrangler into path/to/worker/package.json?",
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to install wrangler into path/to/worker/my-worker/package.json?",
result: false,
},
{
Expand Down Expand Up @@ -797,7 +801,8 @@ describe("init", () => {
Object {
"debug": "",
"err": "",
"out": "✨ Created path/to/worker/my-worker/wrangler.toml",
"out": "✨ Created path/to/worker/my-worker/wrangler.toml
✨ Created path/to/worker/my-worker/package.json",
"warn": "",
}
`);
Expand Down Expand Up @@ -853,14 +858,14 @@ describe("init", () => {
`);
});

it("should offer to install wrangler into a package.json relative to the target directory", async () => {
it("should offer to install wrangler into a package.json relative to the target directory, if no name is provided", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to install wrangler into path/to/worker/package.json?",
text: "Would you like to install wrangler into ../package.json?",
result: true,
},
{
Expand All @@ -869,7 +874,7 @@ describe("init", () => {
}
);
mockSelect({
text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
text: "Would you like to create a Worker at src/index.js?",
result: "none",
});
writeFiles({
Expand All @@ -879,9 +884,11 @@ describe("init", () => {
},
},
});
setWorkingDirectory("path/to/worker/my-worker");

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

setWorkingDirectory("../../../..");
checkFiles({
items: {
"path/to/worker/package.json": {
Expand All @@ -898,7 +905,7 @@ describe("init", () => {
Object {
"debug": "",
"err": "",
"out": "✨ Created path/to/worker/my-worker/wrangler.toml
"out": "✨ Created wrangler.toml
✨ Installed wrangler into devDependencies",
"warn": "",
}
Expand Down Expand Up @@ -1221,6 +1228,10 @@ describe("init", () => {
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to install wrangler into package.json?",
result: false,
Expand Down Expand Up @@ -1251,6 +1262,7 @@ describe("init", () => {
"debug": "",
"err": "",
"out": "✨ Created my-worker/wrangler.toml
✨ Created my-worker/package.json
✨ Created my-worker/tsconfig.json
✨ Installed @cloudflare/workers-types and typescript into devDependencies",
"warn": "",
Expand Down Expand Up @@ -1359,11 +1371,21 @@ describe("init", () => {
`);
});

it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
mockConfirm({
text: "Would you like to use git to manage this Worker?",
result: false,
});
it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to use TypeScript?",
result: true,
}
);
mockSelect({
text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
result: "fetch",
Expand Down Expand Up @@ -1398,10 +1420,13 @@ describe("init", () => {
"debug": "",
"err": "",
"out": "✨ Created path/to/worker/my-worker/wrangler.toml
✨ Created path/to/worker/my-worker/package.json
✨ Created path/to/worker/my-worker/tsconfig.json
✨ Created path/to/worker/my-worker/src/index.ts
✨ Installed @cloudflare/workers-types and typescript into devDependencies
To start developing your Worker, run \`npx wrangler dev\`
To publish your Worker to the Internet, run \`npx wrangler publish\`",
To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
To publish your Worker to the Internet, run \`npm run deploy\`",
"warn": "",
}
`);
Expand Down Expand Up @@ -1670,7 +1695,7 @@ describe("init", () => {
result: false,
},
{
text: "Would you like to install wrangler into package.json?",
text: "Would you like to install wrangler into my-worker/package.json?",
result: false,
},
{
Expand All @@ -1681,7 +1706,9 @@ describe("init", () => {
const PLACEHOLDER = "/* placeholder text */";
writeFiles({
items: {
"package.json": { contents: { name: "test", version: "1.0.0" } },
"my-worker/package.json": {
contents: { name: "test", version: "1.0.0" },
},
"my-worker/src/index.js": { contents: PLACEHOLDER },
},
});
Expand Down Expand Up @@ -1806,6 +1833,72 @@ describe("init", () => {
}
`);
});

it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
mockConfirm(
{
text: "Would you like to use git to manage this Worker?",
result: false,
},
{
text: "Would you like to use TypeScript?",
result: true,
},
{
text: "No package.json found. Would you like to create one?",
result: true,
},
{
text: "Would you like to install the type definitions for Workers into your package.json?",
result: true,
}
);
mockSelect({
text: "Would you like to create a Worker at sub/folder/worker/src/index.ts?",
result: "fetch",
});
writeFiles({
items: {
"package.json": { contents: { name: "top-level" } },
"tsconfig.json": { contents: { compilerOptions: {} } },
"wrangler.toml": wranglerToml({
name: "top-level",
compatibility_date: "some-date",
}),
},
});

await runWrangler("init sub/folder/worker");

// Ancestor files are untouched.
checkFiles({
items: {
"package.json": { contents: { name: "top-level" } },
"tsconfig.json": {
contents: { config: { compilerOptions: {} }, error: undefined },
},
"wrangler.toml": wranglerToml({
name: "top-level",
compatibility_date: "some-date",
}),
},
});
// New initialized Worker has its own files.
checkFiles({
items: {
"sub/folder/worker/package.json": {
contents: expect.objectContaining({
name: "worker",
}),
},
"sub/folder/worker/tsconfig.json": true,
"sub/folder/worker/wrangler.toml": wranglerToml({
...MINIMAL_WRANGLER_TOML,
name: "worker",
}),
},
});
});
});
});

Expand Down
39 changes: 33 additions & 6 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,12 @@ function createCLIParser(argv: string[]) {
}
}

let pathToPackageJson = await findUp("package.json", {
cwd: creationDirectory,
});
const isolatedInit = !!args.name;
let pathToPackageJson = await findPath(
isolatedInit,
creationDirectory,
"package.json"
);
let shouldCreatePackageJson = false;

if (!pathToPackageJson) {
Expand Down Expand Up @@ -534,9 +537,11 @@ function createCLIParser(argv: string[]) {
}

let isTypescriptProject = false;
let pathToTSConfig = await findUp("tsconfig.json", {
cwd: creationDirectory,
});
let pathToTSConfig = await findPath(
isolatedInit,
creationDirectory,
"tsconfig.json"
);
if (!pathToTSConfig) {
// If there's no tsconfig, offer to create one
// and install @cloudflare/workers-types
Expand Down Expand Up @@ -2857,3 +2862,25 @@ function memoizeGetPort(defaultPort: number) {
return portValue || (portValue = await getPort({ port: defaultPort }));
};
}

/**
* Find the path to the given `basename` file from the `cwd`.
*
* If `isolatedInit` is true then we only look in the `cwd` directory for the file.
* Otherwise we also search up the tree.
*/
async function findPath(
isolatedInit: boolean,
cwd: string,
basename: string
): Promise<string | undefined> {
if (isolatedInit) {
return fs.existsSync(path.resolve(cwd, basename))
? path.resolve(cwd, basename)
: undefined;
} else {
return await findUp(basename, {
cwd: cwd,
});
}
}

0 comments on commit 43ec49b

Please sign in to comment.