diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f667f6cae3..e119d076ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1838,6 +1838,9 @@ importers: v-next/hardhat-utils: dependencies: + env-paths: + specifier: ^2.2.0 + version: 2.2.1 fast-equals: specifier: ^5.0.1 version: 5.0.1 diff --git a/v-next/hardhat-utils/package.json b/v-next/hardhat-utils/package.json index b7c11d95f2..3f6ff218d1 100644 --- a/v-next/hardhat-utils/package.json +++ b/v-next/hardhat-utils/package.json @@ -21,6 +21,7 @@ "./error": "./dist/src/error.js", "./eth": "./dist/src/eth.js", "./fs": "./dist/src/fs.js", + "./global-dir": "./dist/src/global-dir.js", "./hex": "./dist/src/hex.js", "./lang": "./dist/src/lang.js", "./number": "./dist/src/number.js", @@ -74,6 +75,7 @@ "typescript-eslint": "7.7.1" }, "dependencies": { + "env-paths": "^2.2.0", "fast-equals": "^5.0.1", "keccak": "^3.0.4", "rfdc": "^1.3.1", diff --git a/v-next/hardhat-utils/src/global-dir.ts b/v-next/hardhat-utils/src/global-dir.ts new file mode 100644 index 0000000000..2506b8f195 --- /dev/null +++ b/v-next/hardhat-utils/src/global-dir.ts @@ -0,0 +1,17 @@ +import { ensureDir } from "./fs.js"; + +/** + * Returns the path to the hardhat configuration directory. + * + * @returns The path to the hardhat configuration directory. + */ +export async function getConfigDir(): Promise { + const { config } = await generatePaths(); + await ensureDir(config); + return config; +} + +async function generatePaths(packageName = "hardhat") { + const { default: envPaths } = await import("env-paths"); + return envPaths(packageName); +} diff --git a/v-next/hardhat-utils/test/global-dir.ts b/v-next/hardhat-utils/test/global-dir.ts new file mode 100644 index 0000000000..bbdadc21a5 --- /dev/null +++ b/v-next/hardhat-utils/test/global-dir.ts @@ -0,0 +1,15 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { getConfigDir } from "../src/global-dir.js"; + +describe("global-dir", () => { + describe("getGlobalDir", () => { + it("should return the path to the configuration directory with default name 'hardhat'", async () => { + const { default: envPaths } = await import("env-paths"); + const expectedPath = envPaths("hardhat").config; + + assert.equal(await getConfigDir(), expectedPath); + }); + }); +});