Skip to content

Commit d84d424

Browse files
committed
Add global args resolution
1 parent 72ad108 commit d84d424

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

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

+47-7
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
GlobalArguments,
47
GlobalParameter,
@@ -7,17 +10,20 @@ 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 parameters, validating them.
25+
* Builds a map of the global parameter definitions by going through all the
26+
* plugins and validating the global parameters 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
@@ -60,6 +66,10 @@ export function buildGlobalParametersMap(
6066
return globalParametersMap;
6167
}
6268

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

117+
/**
118+
* Resolves the global arguments by parsing the user provided arguments and
119+
* environment variables. The arguments are validated against the global
120+
* parameter definitions, and the default values are used when the arguments
121+
* are not provided. Only the arguments defined in the global parameters map
122+
* are resolved.
123+
*
124+
* @param userProvidedGlobalArguments The arguments provided by the user. These
125+
* take precedence over environment variables.
126+
* @param globalParametersMap The map of global parameter definitions to
127+
* validate the arguments.
128+
*/
107129
export function resolveGlobalArguments(
108130
userProvidedGlobalArguments: Partial<GlobalArguments>,
109-
_globalParametersMap: GlobalParametersMap,
131+
globalParametersMap: GlobalParametersMap,
110132
): GlobalArguments {
111-
// TODO: Validate the userProvidedGlobalArguments and get the remaining ones
112-
// from env variables
133+
const globalArguments: GlobalArguments = {};
134+
// iterate over the definitions to parse and validate the arguments
135+
for (const [name, { param }] of globalParametersMap) {
136+
let value =
137+
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
138+
-- GlobalArguments is empty for user extension, so we need to cast it to
139+
assign the value. */
140+
(userProvidedGlobalArguments as Record<string, string | undefined>)[name];
141+
if (value === undefined) {
142+
value = process.env[`HARDHAT_${camelToSnakeCase(name).toUpperCase()}`];
143+
}
144+
145+
let parsedValue: ParameterValue;
146+
if (value !== undefined) {
147+
parsedValue = parseParameterValue(value, param.parameterType, name);
148+
} else {
149+
parsedValue = param.defaultValue;
150+
}
151+
152+
globalArguments[name] = parsedValue;
153+
}
113154

114-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TODO
115-
return userProvidedGlobalArguments as GlobalArguments;
155+
return globalArguments;
116156
}

0 commit comments

Comments
 (0)