Skip to content

Commit 02d6b97

Browse files
committed
Add global args resolution
1 parent 5fc1ba3 commit 02d6b97

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

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

+55-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { ParameterTypeToValueType } from "../types/common.js";
1+
import type {
2+
ParameterTypeToValueType,
3+
ParameterValue,
4+
} from "../types/common.js";
25
import type {
36
GlobalOptions,
47
GlobalOption,
@@ -7,21 +10,24 @@ import type {
710
import type { HardhatPlugin } from "../types/plugins.js";
811

912
import { HardhatError } from "@nomicfoundation/hardhat-errors";
13+
import { camelToSnakeCase } from "@nomicfoundation/hardhat-utils/string";
1014

1115
import { ParameterType } from "../types/common.js";
1216

1317
import {
1418
RESERVED_PARAMETER_NAMES,
1519
isParameterValueValid,
1620
isValidParamNameCasing,
21+
parseParameterValue,
1722
} from "./parameters.js";
1823

1924
/**
20-
* Builds a map of the global options, validating them.
25+
* Builds a map of the global option definitions by going through all the
26+
* plugins and validating the global options they define.
2127
*
2228
* Note: this function can be used before initializing the HRE, so the plugins
2329
* shouldn't be consider validated. Hence, we should validate the global
24-
* parameters.
30+
* options.
2531
*/
2632
export function buildGlobalOptionsMap(
2733
resolvedPlugins: HardhatPlugin[],
@@ -60,6 +66,10 @@ export function buildGlobalOptionsMap(
6066
return globalOptionsMap;
6167
}
6268

69+
/**
70+
* Builds a global option definition, validating the name, type, and default
71+
* value.
72+
*/
6373
export function buildGlobalOptionDefinition<T extends ParameterType>({
6474
name,
6575
description,
@@ -104,13 +114,50 @@ export function buildGlobalOptionDefinition<T extends ParameterType>({
104114
};
105115
}
106116

117+
/**
118+
* Resolves global options by merging user-provided options with environment
119+
* variables, adhering to predefined global option definitions. This function
120+
* ensures that only options specified in the globalOptionsMap are considered.
121+
* Each option is validated against its definition in the map, with
122+
* user-provided options taking precedence over environment variables. If an
123+
* option is not provided by the user or set as an environment variable, its
124+
* default value (as specified in the globalOptionsMap) is used.
125+
*
126+
* @param userProvidedGlobalOptions The options explicitly provided by the
127+
* user. These take precedence over equivalent environment variables.
128+
* @param globalOptionsMap A map defining valid global options, their default
129+
* values, and expected types. This map is used to validate and parse the options.
130+
* @returns {GlobalOptions} An object containing the resolved global options,
131+
* with each option adhering to its definition in the globalOptionsMap.
132+
* @throws {HardhatError} with descriptor
133+
* {@link HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE} if a user-provided
134+
* option has an invalid value for its type.
135+
*/
107136
export function resolveGlobalOptions(
108137
userProvidedGlobalOptions: Partial<GlobalOptions>,
109-
_globalOptionsMap: GlobalOptionsMap,
138+
globalOptionsMap: GlobalOptionsMap,
110139
): GlobalOptions {
111-
// TODO: Validate the userProvidedGlobalOptions and get the remaining ones
112-
// from env variables
140+
const globalOptions: GlobalOptions = {};
141+
// iterate over the definitions to parse and validate the arguments
142+
for (const [name, { option }] of globalOptionsMap) {
143+
let value =
144+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
145+
-- GlobalOptions is empty for user extension, so we need to cast it to
146+
assign the value. */
147+
(userProvidedGlobalOptions as Record<string, string | undefined>)[name];
148+
if (value === undefined) {
149+
value = process.env[`HARDHAT_${camelToSnakeCase(name).toUpperCase()}`];
150+
}
151+
152+
let parsedValue: ParameterValue;
153+
if (value !== undefined) {
154+
parsedValue = parseParameterValue(value, option.parameterType, name);
155+
} else {
156+
parsedValue = option.defaultValue;
157+
}
158+
159+
globalOptions[name] = parsedValue;
160+
}
113161

114-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TODO
115-
return userProvidedGlobalOptions as GlobalOptions;
162+
return globalOptions;
116163
}

0 commit comments

Comments
 (0)