Skip to content

Commit f972ba6

Browse files
committed
Make the utils Package module work with directories
1 parent 63aa294 commit f972ba6

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

v-next/hardhat-utils/src/package.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,49 @@ export interface PackageJson {
2929
* Searches for the nearest `package.json` file, starting from the directory of
3030
* the provided file path or url string and moving up the directory tree.
3131
*
32-
* @param filePathOrUrl The file path or url string from which to start the
33-
* search. The url must be a file url. This is useful when you want to find
34-
* the nearest `package.json` file relative to the current module, as you
35-
* can use `import.meta.url`.
32+
* @param pathOrUrl A path or url string from which to start the search. The url
33+
* must be a file url. This is useful when you want to find the nearest
34+
* `package.json` file relative to the current module, as you can use
35+
* `import.meta.url`.
3636
* @returns The absolute path to the nearest `package.json` file.
3737
* @throws PackageJsonNotFoundError If no `package.json` file is found.
3838
*/
3939
export async function findClosestPackageJson(
40-
filePathOrUrl: string,
40+
pathOrUrl: string,
4141
): Promise<string> {
42-
const filePath = getFilePath(filePathOrUrl);
42+
const filePath = getFilePath(pathOrUrl);
4343

4444
if (filePath === undefined) {
45-
throw new PackageJsonNotFoundError(filePathOrUrl);
45+
throw new PackageJsonNotFoundError(pathOrUrl);
4646
}
4747

48-
const packageJsonPath = await findUp("package.json", path.dirname(filePath));
48+
const packageJsonPath = await findUp("package.json", filePath);
4949

5050
if (packageJsonPath === undefined) {
51-
throw new PackageJsonNotFoundError(filePathOrUrl);
51+
throw new PackageJsonNotFoundError(pathOrUrl);
5252
}
5353

5454
return packageJsonPath;
5555
}
5656

5757
/**
58-
* Reads the nearest `package.json` file, starting from the directory of the
59-
* provided file path or url string and moving up the directory tree.
58+
* Reads the nearest `package.json` file, starting from provided path or url
59+
* string and moving up the directory tree.
6060
*
61-
* @param filePathOrUrl The file path or url string from which to start the
62-
* search. The url must be a file url. This is useful when you want to find
63-
* the nearest `package.json` file relative to the current module, as you
64-
* can use `import.meta.url`.
61+
* @param pathOrUrl A path or url string from which to start the search. The url
62+
* must be a file url. This is useful when you want to find the nearest
63+
* `package.json` file relative to the current module, as you can use
64+
* `import.meta.url`.
6565
* @returns The contents of the nearest `package.json` file, parsed as a
6666
* {@link PackageJson} object.
6767
* @throws PackageJsonNotFoundError If no `package.json` file is found.
6868
* @throws PackageJsonReadError If the `package.json` file is found but cannot
6969
* be read.
7070
*/
7171
export async function readClosestPackageJson(
72-
filePathOrUrl: string,
72+
pathOrUrl: string,
7373
): Promise<PackageJson> {
74-
const packageJsonPath = await findClosestPackageJson(filePathOrUrl);
74+
const packageJsonPath = await findClosestPackageJson(pathOrUrl);
7575
try {
7676
return await readJsonFile<PackageJson>(packageJsonPath);
7777
} catch (e) {
@@ -81,16 +81,16 @@ export async function readClosestPackageJson(
8181
}
8282

8383
/**
84-
* Finds the root directory of the nearest package, starting from the directory
85-
* of the provided file path or url string and moving up the directory tree.
84+
* Finds the root directory of the nearest package, starting from the provided
85+
* path or url string and moving up the directory tree.
8686
*
8787
* This function uses `findClosestPackageJson` to find the nearest `package.json`
8888
* file and then returns the directory that contains that file.
8989
*
90-
* @param filePathOrUrl The file path or url string from which to start the
91-
* search. The url must be a file url. This is useful when you want to find
92-
* the nearest `package.json` file relative to the current module, as you
93-
* can use `import.meta.url`.
90+
* @param pathOrUrl A path or url string from which to start the search. The url
91+
* must be a file url. This is useful when you want to find the nearest
92+
* `package.json` file relative to the current module, as you can use
93+
* `import.meta.url`.
9494
* @returns The absolute path of the root directory of the nearest package.
9595
*/
9696
export async function findClosestPackageRoot(

v-next/hardhat-utils/test/package.ts

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ describe("package", () => {
4848
assert.equal(actualPath, expectedPath);
4949
});
5050

51+
it("Should find the closest package.json relative to a directory when its within that directory", async () => {
52+
const expectedPath = path.join(getTmpDir(), "package.json");
53+
const fromPath = getTmpDir();
54+
await createFile(expectedPath);
55+
56+
const actualPath = await findClosestPackageJson(fromPath);
57+
58+
assert.equal(actualPath, expectedPath);
59+
});
60+
5161
it("Should throw PackageJsonNotFoundError if no package.json is found", async () => {
5262
const fromPath = path.join(getTmpDir(), "subdir", "subsubdir");
5363

0 commit comments

Comments
 (0)