1
- import type { ParameterTypeToValueType } from "../types/common.js" ;
1
+ import type {
2
+ ParameterTypeToValueType ,
3
+ ParameterValue ,
4
+ } from "../types/common.js" ;
2
5
import type {
3
6
GlobalArguments ,
4
7
GlobalParameter ,
@@ -7,17 +10,20 @@ import type {
7
10
import type { HardhatPlugin } from "../types/plugins.js" ;
8
11
9
12
import { HardhatError } from "@nomicfoundation/hardhat-errors" ;
13
+ import { camelToSnakeCase } from "@nomicfoundation/hardhat-utils/string" ;
10
14
11
15
import { ParameterType } from "../types/common.js" ;
12
16
13
17
import {
14
18
RESERVED_PARAMETER_NAMES ,
15
19
isParameterValueValid ,
16
20
isValidParamNameCasing ,
21
+ parseParameterValue ,
17
22
} from "./parameters.js" ;
18
23
19
24
/**
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.
21
27
*
22
28
* Note: this function can be used before initializing the HRE, so the plugins
23
29
* shouldn't be consider validated. Hence, we should validate the global
@@ -60,6 +66,10 @@ export function buildGlobalParametersMap(
60
66
return globalParametersMap ;
61
67
}
62
68
69
+ /**
70
+ * Builds a global parameter definition, validating the name, type, and default
71
+ * value.
72
+ */
63
73
export function buildGlobalParameterDefinition < T extends ParameterType > ( {
64
74
name,
65
75
description,
@@ -104,13 +114,43 @@ export function buildGlobalParameterDefinition<T extends ParameterType>({
104
114
} ;
105
115
}
106
116
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
+ */
107
129
export function resolveGlobalArguments (
108
130
userProvidedGlobalArguments : Partial < GlobalArguments > ,
109
- _globalParametersMap : GlobalParametersMap ,
131
+ globalParametersMap : GlobalParametersMap ,
110
132
) : 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
+ }
113
154
114
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- TODO
115
- return userProvidedGlobalArguments as GlobalArguments ;
155
+ return globalArguments ;
116
156
}
0 commit comments