Skip to content

Commit f5226ac

Browse files
Merge branch 'v-next' of github.com:NomicFoundation/hardhat into feature/add-logic-for-init-command
2 parents 64c6d05 + 733fa6a commit f5226ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+10290
-4392
lines changed

pnpm-lock.yaml

+7,425-3,807
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/core/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This package contains the new core of Hardhat, which includes:
55
- The config system
66
- The configuration variables system
77
- The plugin system
8-
- The global arguments system
8+
- The global options system
99
- The hooks system
1010
- The tasks system
1111
- The user interruptions system

v-next/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"./types/cli": "./dist/src/types/cli.js",
1515
"./types/common": "./dist/src/types/common.js",
1616
"./types/config": "./dist/src/types/config.js",
17-
"./types/global-parameters": "./dist/src/types/global-parameters.js",
17+
"./types/global-options": "./dist/src/types/global-options.js",
1818
"./types/hooks": "./dist/src/types/hooks.js",
1919
"./types/hre": "./dist/src/types/hre.js",
2020
"./types/plugins": "./dist/src/types/plugins.js",

v-next/core/scripts/api-extractor.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export * from "./types/common.js";
1616
export type * from "./types/common.js";
1717
export * from "./types/config.js";
1818
export type * from "./types/config.js";
19-
export * from "./types/global-parameters.js";
20-
export type * from "./types/global-parameters.js";
19+
export * from "./types/global-options.js";
20+
export type * from "./types/global-options.js";
2121
export * from "./types/hooks.js";
2222
export type * from "./types/hooks.js";
2323
export * from "./types/hre.js";

v-next/core/src/config.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import type { ParameterTypeToValueType } from "./types/common.js";
12
import type { ConfigurationVariable } from "./types/config.js";
2-
import type { GlobalParameter } from "./types/global-parameters.js";
3+
import type { GlobalOption } from "./types/global-options.js";
34
import type {
45
EmptyTaskDefinitionBuilder,
56
NewTaskDefinitionBuilder,
67
TaskOverrideDefinitionBuilder,
78
} from "./types/tasks.js";
89

9-
import { buildGlobalParameterDefinition } from "./internal/global-parameters.js";
10+
import { buildGlobalOptionDefinition } from "./internal/global-options.js";
1011
import {
1112
EmptyTaskDefinitionBuilderImplementation,
1213
NewTaskDefinitionBuilderImplementation,
@@ -55,15 +56,15 @@ export function overrideTask(
5556
}
5657

5758
/**
58-
* Defines a global parameter.
59+
* Defines a global option.
5960
*/
60-
export function globalParameter(options: {
61+
export function globalOption<T extends ParameterType>(options: {
6162
name: string;
6263
description: string;
63-
parameterType: ParameterType;
64-
defaultValue: any;
65-
}): GlobalParameter {
66-
return buildGlobalParameterDefinition(options);
64+
parameterType?: T;
65+
defaultValue: ParameterTypeToValueType<T>;
66+
}): GlobalOption {
67+
return buildGlobalOptionDefinition(options);
6768
}
6869

6970
/**
@@ -72,8 +73,8 @@ export function globalParameter(options: {
7273
export function globalFlag(options: {
7374
name: string;
7475
description: string;
75-
}): GlobalParameter {
76-
return buildGlobalParameterDefinition({
76+
}): GlobalOption {
77+
return buildGlobalOptionDefinition({
7778
...options,
7879
parameterType: ParameterType.BOOLEAN,
7980
defaultValue: false,

v-next/core/src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UnsafeHardhatRuntimeEnvironmentOptions } from "./types/cli.js";
22
import type { HardhatUserConfig } from "./types/config.js";
3-
import type { GlobalArguments } from "./types/global-parameters.js";
3+
import type { GlobalOptions } from "./types/global-options.js";
44
import type { HardhatRuntimeEnvironment } from "./types/hre.js";
55

66
import { HardhatRuntimeEnvironmentImplementation } from "./internal/hre.js";
@@ -9,23 +9,23 @@ import { HardhatRuntimeEnvironmentImplementation } from "./internal/hre.js";
99
* Creates an instances of the Hardhat Runtime Environment.
1010
*
1111
* @param config - The user's Hardhat configuration.
12-
* @param userProvidedGlobalArguments - The global arguments provided by the
12+
* @param userProvidedGlobalOptions - The global options provided by the
1313
* user.
1414
* @param unsafeOptions - Options used to bypass some initialization, to avoid
1515
* redoing it in the CLI. Should only be used in the official CLI.
1616
* @returns The Hardhat Runtime Environment.
1717
*/
1818
export async function createHardhatRuntimeEnvironment(
1919
config: HardhatUserConfig,
20-
userProvidedGlobalArguments: Partial<GlobalArguments> = {},
20+
userProvidedGlobalOptions: Partial<GlobalOptions> = {},
2121
unsafeOptions?: UnsafeHardhatRuntimeEnvironmentOptions,
2222
): Promise<HardhatRuntimeEnvironment> {
2323
return HardhatRuntimeEnvironmentImplementation.create(
2424
config,
25-
userProvidedGlobalArguments,
25+
userProvidedGlobalOptions,
2626
unsafeOptions,
2727
);
2828
}
2929

3030
export { resolvePluginList } from "./internal/plugins/resolve-plugin-list.js";
31-
export { buildGlobalParameterMap } from "./internal/global-parameters.js";
31+
export { buildGlobalOptionsMap } from "./internal/global-options.js";
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import type { ParameterTypeToValueType } from "../types/common.js";
2+
import type {
3+
GlobalOptions,
4+
GlobalOption,
5+
GlobalOptionsMap,
6+
} from "../types/global-options.js";
7+
import type { HardhatPlugin } from "../types/plugins.js";
8+
9+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
10+
11+
import { ParameterType } from "../types/common.js";
12+
13+
import {
14+
RESERVED_PARAMETER_NAMES,
15+
isParameterValueValid,
16+
isValidParamNameCasing,
17+
} from "./parameters.js";
18+
19+
/**
20+
* Builds a map of the global options, validating them.
21+
*
22+
* Note: this function can be used before initializing the HRE, so the plugins
23+
* shouldn't be consider validated. Hence, we should validate the global
24+
* parameters.
25+
*/
26+
export function buildGlobalOptionsMap(
27+
resolvedPlugins: HardhatPlugin[],
28+
): GlobalOptionsMap {
29+
const globalOptionsMap: GlobalOptionsMap = new Map();
30+
31+
for (const plugin of resolvedPlugins) {
32+
if (plugin.globalOptions === undefined) {
33+
continue;
34+
}
35+
36+
for (const option of plugin.globalOptions) {
37+
const existingByName = globalOptionsMap.get(option.name);
38+
if (existingByName !== undefined) {
39+
throw new HardhatError(
40+
HardhatError.ERRORS.GENERAL.GLOBAL_OPTION_ALREADY_DEFINED,
41+
{
42+
plugin: plugin.id,
43+
globalOption: option.name,
44+
definedByPlugin: existingByName.pluginId,
45+
},
46+
);
47+
}
48+
49+
const validatedGlobalOption = buildGlobalOptionDefinition(option);
50+
51+
const mapEntry = {
52+
pluginId: plugin.id,
53+
option: validatedGlobalOption,
54+
};
55+
56+
globalOptionsMap.set(validatedGlobalOption.name, mapEntry);
57+
}
58+
}
59+
60+
return globalOptionsMap;
61+
}
62+
63+
export function buildGlobalOptionDefinition<T extends ParameterType>({
64+
name,
65+
description,
66+
parameterType,
67+
defaultValue,
68+
}: {
69+
name: string;
70+
description: string;
71+
parameterType?: T;
72+
defaultValue: ParameterTypeToValueType<T>;
73+
}): GlobalOption {
74+
const type = parameterType ?? ParameterType.STRING;
75+
76+
if (!isValidParamNameCasing(name)) {
77+
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
78+
name,
79+
});
80+
}
81+
82+
if (RESERVED_PARAMETER_NAMES.has(name)) {
83+
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
84+
name,
85+
});
86+
}
87+
88+
if (!isParameterValueValid(type, defaultValue)) {
89+
throw new HardhatError(
90+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
91+
{
92+
value: defaultValue,
93+
name: "defaultValue",
94+
type: parameterType,
95+
},
96+
);
97+
}
98+
99+
return {
100+
name,
101+
description,
102+
parameterType: type,
103+
defaultValue,
104+
};
105+
}
106+
107+
export function resolveGlobalOptions(
108+
userProvidedGlobalOptions: Partial<GlobalOptions>,
109+
_globalOptionsMap: GlobalOptionsMap,
110+
): GlobalOptions {
111+
// TODO: Validate the userProvidedGlobalOptions and get the remaining ones
112+
// from env variables
113+
114+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TODO
115+
return userProvidedGlobalOptions as GlobalOptions;
116+
}

v-next/core/src/internal/global-parameters.ts

-78
This file was deleted.

0 commit comments

Comments
 (0)