Skip to content

Commit 1d7dd29

Browse files
committed
export resolveHardhatConfigPath in public API
1 parent 755a500 commit 1d7dd29

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

v-next/hardhat/src/config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
import { findClosestHardhatConfig } from "./internal/helpers/config-loading.js";
2+
13
export type * from "@nomicfoundation/hardhat-core/config";
24
export * from "@nomicfoundation/hardhat-core/config";
5+
6+
/**
7+
* Attempts to find the nearest Hardhat config file, starting from the current
8+
* working directory. If no config file is found, an error is thrown.
9+
*
10+
* @returns The path to the nearest Hardhat config file.
11+
*/
12+
export async function resolveHardhatConfigPath(): Promise<string> {
13+
const configPath = process.env.HARDHAT_CONFIG;
14+
15+
if (configPath !== undefined) {
16+
return configPath;
17+
}
18+
19+
return findClosestHardhatConfig();
20+
}

v-next/hardhat/src/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {
2-
importUserConfig,
3-
resolveConfigPath,
4-
} from "./internal/helpers/config-loading.js";
1+
import { resolveHardhatConfigPath } from "./config.js";
2+
import { importUserConfig } from "./internal/helpers/config-loading.js";
53
import { getHardhatRuntimeEnvironmentSingleton } from "./internal/hre-singleton.js";
64

7-
const configPath = await resolveConfigPath();
5+
const configPath = await resolveHardhatConfigPath();
86
const userConfig = await importUserConfig(configPath);
97

108
const hre = await getHardhatRuntimeEnvironmentSingleton(userConfig);

v-next/hardhat/src/internal/cli/main.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ import {
2626
import { isCi } from "@nomicfoundation/hardhat-utils/ci";
2727
import { kebabToCamelCase } from "@nomicfoundation/hardhat-utils/string";
2828

29+
import { resolveHardhatConfigPath } from "../../config.js";
2930
import { builtinPlugins } from "../builtin-plugins/index.js";
30-
import {
31-
importUserConfig,
32-
resolveConfigPath,
33-
} from "../helpers/config-loading.js";
31+
import { importUserConfig } from "../helpers/config-loading.js";
3432
import { getHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
3533

3634
import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js";
@@ -55,7 +53,7 @@ export async function main(cliArguments: string[], print = console.log) {
5553
}
5654

5755
if (hardhatSpecialArgs.configPath === undefined) {
58-
hardhatSpecialArgs.configPath = await resolveConfigPath();
56+
hardhatSpecialArgs.configPath = await resolveHardhatConfigPath();
5957
}
6058

6159
try {

v-next/hardhat/src/internal/helpers/config-loading.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HardhatError } from "@nomicfoundation/hardhat-errors";
55
import { findUp } from "@nomicfoundation/hardhat-utils/fs";
66
import { isObject } from "@nomicfoundation/hardhat-utils/lang";
77

8-
async function findClosestHardhatConfig(): Promise<string> {
8+
export async function findClosestHardhatConfig(): Promise<string> {
99
let hardhatConfigPath = await findUp("hardhat.config.ts");
1010

1111
if (hardhatConfigPath !== undefined) {
@@ -21,16 +21,6 @@ async function findClosestHardhatConfig(): Promise<string> {
2121
throw new HardhatError(HardhatError.ERRORS.GENERAL.NO_CONFIG_FILE_FOUND);
2222
}
2323

24-
export async function resolveConfigPath(): Promise<string> {
25-
const configPath = process.env.HARDHAT_CONFIG;
26-
27-
if (configPath !== undefined) {
28-
return configPath;
29-
}
30-
31-
return findClosestHardhatConfig();
32-
}
33-
3424
export async function importUserConfig(configPath: string) {
3525
const normalizedPath = isAbsolute(configPath)
3626
? configPath

v-next/hardhat/test/src/hre/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

4+
import { resolveHardhatConfigPath } from "../../../src/config.js";
45
import { createHardhatRuntimeEnvironment } from "../../../src/hre.js";
56
import { builtinPlugins } from "../../../src/internal/builtin-plugins/index.js";
6-
import { resolveConfigPath } from "../../../src/internal/helpers/config-loading.js";
77
import {
88
getHardhatRuntimeEnvironmentSingleton,
99
resetHardhatRuntimeEnvironmentSingleton,
@@ -47,13 +47,13 @@ describe("HRE", () => {
4747
it("should return the HARDHAT_CONFIG env variable if it is set", async () => {
4848
process.env.HARDHAT_CONFIG = "env.config.js";
4949

50-
assert.equal(await resolveConfigPath(), "env.config.js");
50+
assert.equal(await resolveHardhatConfigPath(), "env.config.js");
5151

5252
delete process.env.HARDHAT_CONFIG;
5353
});
5454

5555
it("should throw if the config file is not found", async () => {
56-
await assert.rejects(resolveConfigPath(), {
56+
await assert.rejects(resolveHardhatConfigPath(), {
5757
message: "HHE5: No Hardhat config file found",
5858
});
5959
});
@@ -63,7 +63,7 @@ describe("HRE", () => {
6363
useFixtureProject("config-js");
6464

6565
it("should load a config file in the current directory", async () => {
66-
const configPath = await resolveConfigPath();
66+
const configPath = await resolveHardhatConfigPath();
6767

6868
assert(
6969
configPath.endsWith("hardhat.config.js"),
@@ -76,7 +76,7 @@ describe("HRE", () => {
7676
useFixtureProject("config-js", "nested-folder");
7777

7878
it("should load a config file in the parent directory", async () => {
79-
const configPath = await resolveConfigPath();
79+
const configPath = await resolveHardhatConfigPath();
8080

8181
assert(
8282
configPath.endsWith("hardhat.config.js"),
@@ -91,7 +91,7 @@ describe("HRE", () => {
9191
useFixtureProject("config-ts");
9292

9393
it("should load a config file in the current directory", async () => {
94-
const configPath = await resolveConfigPath();
94+
const configPath = await resolveHardhatConfigPath();
9595

9696
assert(
9797
configPath.endsWith("hardhat.config.ts"),
@@ -104,7 +104,7 @@ describe("HRE", () => {
104104
useFixtureProject("config-ts", "nested-folder");
105105

106106
it("should load a config file in the parent directory", async () => {
107-
const configPath = await resolveConfigPath();
107+
const configPath = await resolveHardhatConfigPath();
108108

109109
assert(
110110
configPath.endsWith("hardhat.config.ts"),

v-next/hardhat/test/src/internal/cli/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ Usage: hardhat [GLOBAL OPTIONS] <TASK> [SUBTASK] [TASK OPTIONS] [--] [TASK ARGUM
231231
AVAILABLE TASKS:
232232
233233
example Example task
234+
run Runs a user-defined script after compiling the project
234235
task A task that uses param1
235236
236237
GLOBAL OPTIONS:

0 commit comments

Comments
 (0)