Skip to content

Commit

Permalink
Merge pull request #5734 from NomicFoundation/feature/move-global-dir…
Browse files Browse the repository at this point in the history
…-into-hh-utils

Move the global-dir functions from hardhat to harhdat-utils
  • Loading branch information
ChristopherDedominici authored Sep 9, 2024
2 parents be2e1f9 + 61c6b65 commit 9e4ad9c
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 52 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions v-next/hardhat-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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",
Expand Down Expand Up @@ -76,6 +77,7 @@
},
"dependencies": {
"debug": "^4.1.1",
"env-paths": "^2.2.0",
"ethereum-cryptography": "^2.2.1",
"fast-equals": "^5.0.1",
"rfdc": "^1.3.1",
Expand Down
51 changes: 51 additions & 0 deletions v-next/hardhat-utils/src/global-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { generatePaths, HARDHAT_PACKAGE_NAME } from "./internal/global-dir.js";

import { ensureDir } from "@ignored/hardhat-vnext-utils/fs";

/**
* Returns the configuration directory path for a given package (defaults to "hardhat").
* Ensures that the directory exists before returning the path.
*
* @param packageName The name of the package for which to generate paths. Defaults to "hardhat" if no package name is provided.
* @returns The path to the hardhat configuration directory.
* @throws FileSystemAccessError for any error.
*/
export async function getConfigDir(
packageName: string = HARDHAT_PACKAGE_NAME,
): Promise<string> {
const { config } = await generatePaths(packageName);
await ensureDir(config);
return config;
}

/**
* Returns the cache directory path for a given package (defaults to "hardhat").
* Ensures that the directory exists before returning the path.
*
* @param packageName The name of the package for which to generate paths. Defaults to "hardhat" if no package name is provided.
* @returns The path to the hardhat cache directory.
* @throws FileSystemAccessError for any error.
*/
export async function getCacheDir(
packageName: string = HARDHAT_PACKAGE_NAME,
): Promise<string> {
const { cache } = await generatePaths(packageName);
await ensureDir(cache);
return cache;
}

/**
* Returns the telemetry directory path for a given package (defaults to "hardhat").
* Ensures that the directory exists before returning the path.
*
* @param packageName The name of the package for which to generate paths. Defaults to "hardhat" if no package name is provided.
* @returns A promise that resolves to the path of the telemetry directory.
* @throws FileSystemAccessError for any error.
*/
export async function getTelemetryDir(
packageName: string = HARDHAT_PACKAGE_NAME,
): Promise<string> {
const { data } = await generatePaths(packageName);
await ensureDir(data);
return data;
}
10 changes: 10 additions & 0 deletions v-next/hardhat-utils/src/internal/global-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type envPaths from "env-paths";

export const HARDHAT_PACKAGE_NAME = "hardhat";

export async function generatePaths(
packageName: string,
): Promise<envPaths.Paths> {
const { default: envPaths } = await import("env-paths");
return envPaths(packageName);
}
54 changes: 54 additions & 0 deletions v-next/hardhat-utils/test/global-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import {
getCacheDir,
getConfigDir,
getTelemetryDir,
} from "../src/global-dir.js";

async function getExpectedPath(packageName: string) {
const { default: envPaths } = await import("env-paths");
return envPaths(packageName);
}

describe("global-dir", () => {
const HARDHAT_PACKAGE_NAME = "hardhat";
const CUSTOM_PACKAGE_NAME = "custom-name";

describe("getConfigDir", () => {
it("should return the path to the configuration directory with default name 'hardhat'", async () => {
const path = await getConfigDir();
assert.equal(path, (await getExpectedPath(HARDHAT_PACKAGE_NAME)).config);
});

it("should return the path to the configuration directory with custom name", async () => {
const path = await getConfigDir(CUSTOM_PACKAGE_NAME);
assert.equal(path, (await getExpectedPath(CUSTOM_PACKAGE_NAME)).config);
});
});

describe("getCacheDir", () => {
it("should return the path to the cache directory with default name 'hardhat'", async () => {
const path = await getCacheDir();
assert.equal(path, (await getExpectedPath(HARDHAT_PACKAGE_NAME)).cache);
});

it("should return the path to the cache directory with custom name", async () => {
const path = await getCacheDir(CUSTOM_PACKAGE_NAME);
assert.equal(path, (await getExpectedPath(CUSTOM_PACKAGE_NAME)).cache);
});
});

describe("getTelemetryDir", () => {
it("should return the path to the telemetry directory with default name 'hardhat'", async () => {
const path = await getTelemetryDir();
assert.equal(path, (await getExpectedPath(HARDHAT_PACKAGE_NAME)).data);
});

it("should return the path to the telemetry directory with custom name", async () => {
const path = await getTelemetryDir(CUSTOM_PACKAGE_NAME);
assert.equal(path, (await getExpectedPath(CUSTOM_PACKAGE_NAME)).data);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { NewTaskActionFunction } from "../../../types/tasks.js";

import { emptyDir, remove } from "@ignored/hardhat-vnext-utils/fs";

import { getCacheDir } from "../../global-dir.js";
import { getCacheDir } from "@ignored/hardhat-vnext-utils/global-dir";

interface CleanActionArguments {
global: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import type { REPLServer } from "node:repl";
import path from "node:path";
import repl from "node:repl";

import { getCacheDir } from "@ignored/hardhat-vnext-utils/global-dir";
import debug from "debug";

import { getCacheDir } from "../../global-dir.js";

const log = debug("hardhat:core:tasks:console");

interface ConsoleActionArguments {
Expand Down
3 changes: 1 addition & 2 deletions v-next/hardhat/src/internal/cli/telemetry/analytics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
readJsonFile,
writeJsonFile,
} from "@ignored/hardhat-vnext-utils/fs";
import { getTelemetryDir } from "@ignored/hardhat-vnext-utils/global-dir";
import debug from "debug";

import { getTelemetryDir } from "../../../global-dir.js";

const log = debug("hardhat:cli:telemetry:analytics:utils");

const ANALYTICS_FILE_NAME = "analytics.json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
readJsonFile,
writeJsonFile,
} from "@ignored/hardhat-vnext-utils/fs";
import { getTelemetryDir } from "@ignored/hardhat-vnext-utils/global-dir";
import debug from "debug";

import { getTelemetryDir } from "../../global-dir.js";
import { confirmationPromptWithTimeout } from "../prompt/prompt.js";

import { sendTelemetryConsentAnalytics } from "./analytics/analytics.js";
Expand Down
43 changes: 0 additions & 43 deletions v-next/hardhat/src/internal/global-dir.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
remove,
writeUtf8File,
} from "@ignored/hardhat-vnext-utils/fs";
import { getCacheDir } from "@ignored/hardhat-vnext-utils/global-dir";
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";

import cleanAction from "../../../../src/internal/builtin-plugins/clean/task-action.js";
import { getCacheDir } from "../../../../src/internal/global-dir.js";
import { createHardhatRuntimeEnvironment } from "../../../../src/internal/hre-intialization.js";

let hre: HardhatRuntimeEnvironment;
Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat/test/internal/global-dir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { getConfigDir } from "../../src/internal/global-dir.js";
import { getConfigDir } from "@ignored/hardhat-vnext-utils/global-dir";

describe("global-dir", () => {
describe("getGlobalDir", () => {
Expand Down

0 comments on commit 9e4ad9c

Please sign in to comment.