Skip to content

Commit

Permalink
Merge pull request #5698 from NomicFoundation/core-in-hardhat-continu…
Browse files Browse the repository at this point in the history
…ation

`core` in `hardhat` continuation
  • Loading branch information
alcuadrado committed Sep 2, 2024
2 parents e0d62ae + f155c05 commit a5eda69
Show file tree
Hide file tree
Showing 35 changed files with 1,776 additions and 1,963 deletions.
8 changes: 0 additions & 8 deletions v-next/hardhat/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
const { createConfig } = require("../../config-v-next/eslint.cjs");

module.exports = createConfig(__filename);

module.exports.rules["no-restricted-imports"][1].paths.push({
// TODO: Rename this once we migrate to the official package names
name: "@ignored/hardhat-vnext-core",
importNames: ["createBaseHardhatRuntimeEnvironment"],
message:
"Use `createHardhatRuntimeEnvironment` from `hre.js` instead of this function.",
});
24 changes: 4 additions & 20 deletions v-next/hardhat/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { findClosestHardhatConfig } from "./internal/helpers/config-loading.js";

// Note: We import the builtin plugins' types here, so that any type extension
// they may have gets loaded.
import "./internal/builtin-plugins/index.js";

export type * from "./internal/core/config.js";
export * from "./internal/core/config.js";

/**
* Attempts to find the nearest Hardhat config file, starting from the current
* working directory. If no config file is found, an error is thrown.
*
* @returns The path to the nearest Hardhat config file.
*/
export async function resolveHardhatConfigPath(): Promise<string> {
const configPath = process.env.HARDHAT_CONFIG;
export type { HardhatUserConfig } from "./types/config.js";

if (configPath !== undefined) {
return configPath;
}

return findClosestHardhatConfig();
}
// NOTE: We import the builtin plugins in this module, so that their
// type-extensions are loaded then the user imports `hardhat/config`.
import "./internal/builtin-plugins/index.js";
68 changes: 5 additions & 63 deletions v-next/hardhat/src/hre.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,6 @@
import type { UnsafeHardhatRuntimeEnvironmentOptions } from "./internal/core/types.js";
import type { HardhatUserConfig } from "./types/config.js";
import type { GlobalOptions } from "./types/global-options.js";
import type { HardhatRuntimeEnvironment } from "./types/hre.js";
// NOTE: We import the builtin plugins in this module, so that their
// type-extensions are loaded then the user imports `hardhat/hre`.
import "./internal/builtin-plugins/index.js";

import { BUILTIN_GLOBAL_OPTIONS_DEFINITIONS } from "./internal/builtin-global-options.js";
import { builtinPlugins } from "./internal/builtin-plugins/index.js";
import { resolveProjectRoot } from "./internal/core/hre.js";
import {
buildGlobalOptionDefinitions,
createBaseHardhatRuntimeEnvironment,
resolvePluginList,
} from "./internal/core/index.js";

/**
* Creates an instances of the Hardhat Runtime Environment.
*
* @param config - The user's Hardhat configuration.
* @param userProvidedGlobalOptions - The global options provided by the
* user.
* @param projectRoot - The root of the Hardhat project. Hardhat expects this
* to be the root of the npm project containing your config file. If none is
* provided, it will use the root of the npm project that contains the CWD.
* @returns The Hardhat Runtime Environment.
*/
export async function createHardhatRuntimeEnvironment(
config: HardhatUserConfig,
userProvidedGlobalOptions: Partial<GlobalOptions> = {},
projectRoot?: string,
unsafeOptions: UnsafeHardhatRuntimeEnvironmentOptions = {},
): Promise<HardhatRuntimeEnvironment> {
const resolvedProjectRoot = await resolveProjectRoot(projectRoot);

if (unsafeOptions.resolvedPlugins === undefined) {
const plugins = [...builtinPlugins, ...(config.plugins ?? [])];

const resolvedPlugins = await resolvePluginList(
resolvedProjectRoot,
plugins,
);

unsafeOptions.resolvedPlugins = resolvedPlugins;
}

if (unsafeOptions.globalOptionDefinitions === undefined) {
const pluginGlobalOptionDefinitions = buildGlobalOptionDefinitions(
unsafeOptions.resolvedPlugins,
);

const globalOptionDefinitions = new Map([
...BUILTIN_GLOBAL_OPTIONS_DEFINITIONS,
...pluginGlobalOptionDefinitions,
]);

unsafeOptions.globalOptionDefinitions = globalOptionDefinitions;
}

return createBaseHardhatRuntimeEnvironment(
config,
userProvidedGlobalOptions,
resolvedProjectRoot,
unsafeOptions,
);
}
export { resolveHardhatConfigPath } from "./internal/config-loading.js";
export { createHardhatRuntimeEnvironment } from "./internal/hre-intialization.js";
30 changes: 7 additions & 23 deletions v-next/hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,15 @@ import type { HardhatRuntimeEnvironment } from "./types/hre.js";
import type { TaskManager } from "./types/tasks.js";
import type { UserInterruptionManager } from "./types/user-interruptions.js";

import { resolveHardhatConfigPath } from "./config.js";
import { createHardhatRuntimeEnvironment } from "./hre.js";
import { resolveProjectRoot } from "./internal/core/index.js";
import {
getGlobalHardhatRuntimeEnvironment,
setGlobalHardhatRuntimeEnvironment,
} from "./internal/global-hre-instance.js";
import { importUserConfig } from "./internal/helpers/config-loading.js";
import { getOrCreateGlobalHardhatRuntimeEnvironment } from "./internal/hre-intialization.js";

let maybeHre: HardhatRuntimeEnvironment | undefined =
getGlobalHardhatRuntimeEnvironment();
// NOTE: We import the builtin plugins in this module, so that their
// type-extensions are loaded then the user imports `hardhat`.
import "./internal/builtin-plugins/index.js";

if (maybeHre === undefined) {
/* eslint-disable no-restricted-syntax -- Allow top-level await here */
const configPath = await resolveHardhatConfigPath();
const projectRoot = await resolveProjectRoot(configPath);
const userConfig = await importUserConfig(configPath);

maybeHre = await createHardhatRuntimeEnvironment(userConfig, {}, projectRoot);
/* eslint-enable no-restricted-syntax */

setGlobalHardhatRuntimeEnvironment(maybeHre);
}

const hre: HardhatRuntimeEnvironment = maybeHre;
const hre: HardhatRuntimeEnvironment =
// eslint-disable-next-line no-restricted-syntax -- Allow top-level await here
await getOrCreateGlobalHardhatRuntimeEnvironment();

export const config: HardhatConfig = hre.config;
export const tasks: TaskManager = hre.tasks;
Expand Down
3 changes: 2 additions & 1 deletion v-next/hardhat/src/internal/builtin-global-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GlobalOptionDefinitions } from "../types/global-options.js";

import { globalOption, ArgumentType } from "../config.js";
import { globalOption } from "../config.js";
import { ArgumentType } from "../types/arguments.js";

export const BUILTIN_GLOBAL_OPTIONS_DEFINITIONS: GlobalOptionDefinitions =
new Map([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NewTaskActionFunction } from "../../../types/tasks.js";

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

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

interface CleanActionArguments {
global: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import repl from "node:repl";

import debug from "debug";

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

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

Expand Down
6 changes: 4 additions & 2 deletions v-next/hardhat/src/internal/cli/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { ArgumentTypeToValueType } from "../../../types/arguments.js";
import type {
ArgumentType,
ArgumentTypeToValueType,
} from "../../../types/arguments.js";
import type { GlobalOptionDefinitions } from "../../../types/global-options.js";
import type { Task } from "../../../types/tasks.js";
import type { ArgumentType } from "../../core/config.js";

import { camelToKebabCase } from "@ignored/hardhat-vnext-utils/string";

Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat/src/internal/cli/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HardhatError } from "@ignored/hardhat-vnext-errors";

import { findClosestHardhatConfig } from "../../helpers/config-loading.js";
import { findClosestHardhatConfig } from "../../config-loading.js";

import { createProject } from "./project-creation.js";

Expand Down
49 changes: 25 additions & 24 deletions v-next/hardhat/src/internal/cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import type {
OptionDefinition,
PositionalArgumentDefinition,
} from "../../types/arguments.js";
import type {
GlobalOptionDefinitions,
GlobalOptions,
Expand All @@ -14,23 +10,26 @@ import {
assertHardhatInvariant,
} from "@ignored/hardhat-vnext-errors";
import { isCi } from "@ignored/hardhat-vnext-utils/ci";
import { getRealPath } from "@ignored/hardhat-vnext-utils/fs";
import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
import debug from "debug";

import { resolveHardhatConfigPath } from "../../config.js";
import { createHardhatRuntimeEnvironment } from "../../hre.js";
import {
ArgumentType,
type OptionDefinition,
type PositionalArgumentDefinition,
} from "../../types/arguments.js";
import { BUILTIN_GLOBAL_OPTIONS_DEFINITIONS } from "../builtin-global-options.js";
import { builtinPlugins } from "../builtin-plugins/index.js";
import { ArgumentType } from "../core/config.js";
import {
buildGlobalOptionDefinitions,
parseArgumentValue,
resolvePluginList,
resolveProjectRoot,
} from "../core/index.js";
importUserConfig,
resolveHardhatConfigPath,
} from "../config-loading.js";
import { parseArgumentValue } from "../core/arguments.js";
import { buildGlobalOptionDefinitions } from "../core/global-options.js";
import { resolveProjectRoot } from "../core/hre.js";
import { resolvePluginList } from "../core/plugins/resolve-plugin-list.js";
import { setGlobalHardhatRuntimeEnvironment } from "../global-hre-instance.js";
import { importUserConfig } from "../helpers/config-loading.js";
import { createHardhatRuntimeEnvironment } from "../hre-intialization.js";

import { printErrorMessages } from "./error-handler.js";
import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js";
Expand Down Expand Up @@ -73,17 +72,13 @@ export async function main(

log("Retrieved telemetry consent");

if (builtinGlobalOptions.configPath === undefined) {
builtinGlobalOptions.configPath = await resolveHardhatConfigPath();

log("Resolved config path");
}

const projectRoot = await resolveProjectRoot(
await getRealPath(builtinGlobalOptions.configPath),
const configPath = await resolveHardhatConfigPath(
builtinGlobalOptions.configPath,
);

const userConfig = await importUserConfig(builtinGlobalOptions.configPath);
const projectRoot = await resolveProjectRoot(configPath);

const userConfig = await importUserConfig(configPath);

log("User config imported");

Expand All @@ -97,10 +92,12 @@ export async function main(

const pluginGlobalOptionDefinitions =
buildGlobalOptionDefinitions(resolvedPlugins);

const globalOptionDefinitions = new Map([
...BUILTIN_GLOBAL_OPTIONS_DEFINITIONS,
...pluginGlobalOptionDefinitions,
]);

const userProvidedGlobalOptions = await parseGlobalOptions(
globalOptionDefinitions,
cliArguments,
Expand All @@ -111,7 +108,11 @@ export async function main(

const hre = await createHardhatRuntimeEnvironment(
userConfig,
{ ...builtinGlobalOptions, ...userProvidedGlobalOptions },
{
...builtinGlobalOptions,
config: configPath,
...userProvidedGlobalOptions,
},
projectRoot,
{ resolvedPlugins, globalOptionDefinitions },
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@ignored/hardhat-vnext-utils/fs";
import debug from "debug";

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@ignored/hardhat-vnext-utils/fs";
import debug from "debug";

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

import { sendTelemetryConsentAnalytics } from "./analytics/analytics.js";
Expand Down
Loading

0 comments on commit a5eda69

Please sign in to comment.