diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 249f0bcec7..65b2b76b10 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1434,6 +1434,9 @@ importers: chalk: specifier: ^5.3.0 version: 5.3.0 + env-paths: + specifier: ^2.2.0 + version: 2.2.1 semver: specifier: ^7.6.2 version: 7.6.2 diff --git a/v-next/core/package.json b/v-next/core/package.json index d8b49a0073..304779af9d 100644 --- a/v-next/core/package.json +++ b/v-next/core/package.json @@ -15,6 +15,7 @@ "exports": { ".": "./dist/src/index.js", "./config": "./dist/src/config.js", + "./global-dir": "./dist/src/global-dir.js", "./types/arguments": "./dist/src/types/arguments.js", "./types/cli": "./dist/src/types/cli.js", "./types/config": "./dist/src/types/config.js", @@ -53,8 +54,8 @@ ], "devDependencies": { "@eslint-community/eslint-plugin-eslint-comments": "^4.3.0", - "@microsoft/api-extractor": "^7.43.4", "@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2", + "@microsoft/api-extractor": "^7.43.4", "@types/node": "^20.14.9", "@types/semver": "^7.5.8", "@typescript-eslint/eslint-plugin": "^7.7.1", @@ -77,6 +78,7 @@ "@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2", "@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2", "chalk": "^5.3.0", + "env-paths": "^2.2.0", "semver": "^7.6.2" } } diff --git a/v-next/core/src/global-dir.ts b/v-next/core/src/global-dir.ts new file mode 100644 index 0000000000..d5e189810f --- /dev/null +++ b/v-next/core/src/global-dir.ts @@ -0,0 +1,17 @@ +import { ensureDir } from "@ignored/hardhat-vnext-utils/fs"; + +/** + * 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/core/test/global-dir.ts b/v-next/core/test/global-dir.ts new file mode 100644 index 0000000000..bbdadc21a5 --- /dev/null +++ b/v-next/core/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); + }); + }); +});