-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathresolved-task.ts
289 lines (260 loc) · 8.42 KB
/
resolved-task.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type {
ArgumentValue,
OptionDefinition,
PositionalArgumentDefinition,
} from "../../types/arguments.js";
import type { HardhatRuntimeEnvironment } from "../../types/hre.js";
import type {
NewTaskActionFunction,
Task,
TaskActions,
TaskArguments,
TaskOverrideActionFunction,
} from "../../types/tasks.js";
import {
HardhatError,
assertHardhatInvariant,
} from "@ignored/hardhat-vnext-errors";
import { ensureError } from "@ignored/hardhat-vnext-utils/error";
import { isArgumentValueValid } from "../arguments.js";
import { detectPluginNpmDependencyProblems } from "../plugins/detect-plugin-npm-dependency-problems.js";
import { formatTaskId } from "./utils.js";
export class ResolvedTask implements Task {
readonly #hre: HardhatRuntimeEnvironment;
public static createEmptyTask(
hre: HardhatRuntimeEnvironment,
id: string[],
description: string,
pluginId?: string,
): ResolvedTask {
return new ResolvedTask(
id,
description,
[{ pluginId, action: undefined }],
new Map(),
[],
pluginId,
new Map(),
hre,
);
}
public static createNewTask(
hre: HardhatRuntimeEnvironment,
id: string[],
description: string,
action: NewTaskActionFunction | string,
options: Record<string, OptionDefinition>,
positionalArguments: PositionalArgumentDefinition[],
pluginId?: string,
): ResolvedTask {
return new ResolvedTask(
id,
description,
[{ pluginId, action }],
new Map(Object.entries(options)),
positionalArguments,
pluginId,
new Map(),
hre,
);
}
constructor(
public readonly id: string[],
public readonly description: string,
public readonly actions: TaskActions,
public readonly options: Map<string, OptionDefinition>,
public readonly positionalArguments: PositionalArgumentDefinition[],
public readonly pluginId: string | undefined,
public readonly subtasks: Map<string, Task>,
hre: HardhatRuntimeEnvironment,
) {
this.#hre = hre;
}
public get isEmpty(): boolean {
return this.actions.length === 1 && this.actions[0].action === undefined;
}
/**
* This method runs the task with the given arguments.
* It validates the arguments, resolves the file actions, and runs the task
* actions by calling them in order.
*
* @param taskArguments The arguments to run the task with.
* @returns The result of running the task.
* @throws HardhatError if the task is empty, a required argument is missing,
* a argument has an invalid type, or the file actions can't be resolved.
*/
public async run(taskArguments: TaskArguments): Promise<any> {
if (this.isEmpty) {
throw new HardhatError(HardhatError.ERRORS.TASK_DEFINITIONS.EMPTY_TASK, {
task: formatTaskId(this.id),
});
}
const providedArgumentNames = new Set(Object.keys(taskArguments));
const argumentDefinitions = [
...this.options.values(),
...this.positionalArguments,
];
const validatedTaskArguments: TaskArguments = {};
for (const argumentDefinition of argumentDefinitions) {
const value = taskArguments[argumentDefinition.name];
const isPositional = "isVariadic" in argumentDefinition;
if (isPositional) {
this.#validateRequiredArgument(argumentDefinition, value);
}
this.#validateArgumentType(
argumentDefinition,
value,
isPositional && argumentDefinition.isVariadic,
);
// resolve defaults for optional arguments
validatedTaskArguments[argumentDefinition.name] =
value ?? argumentDefinition.defaultValue;
providedArgumentNames.delete(argumentDefinition.name);
}
// At this point, the set should be empty as all the task arguments have
// been processed. If there are any extra arguments, an error is thrown
this.#validateExtraArguments(providedArgumentNames);
const next = async (
nextTaskArguments: TaskArguments,
currentIndex = this.actions.length - 1,
): Promise<any> => {
// The first action may be empty if the task was originally an empty task
const currentAction = this.actions[currentIndex].action ?? (() => {});
const actionFn =
typeof currentAction === "function"
? currentAction
: await this.#resolveFileAction(
currentAction,
this.actions[currentIndex].pluginId,
);
if (currentIndex === 0) {
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
We know that the first action in the array is a NewTaskActionFunction */
return (actionFn as NewTaskActionFunction)(
nextTaskArguments,
this.#hre,
);
}
return actionFn(
nextTaskArguments,
this.#hre,
async (newTaskArguments: TaskArguments) => {
return next(newTaskArguments, currentIndex - 1);
},
);
};
return next(validatedTaskArguments);
}
/**
* Validates that a required argument has a value. A argument is required if
* it doesn't have a default value.
*
* @throws HardhatError if the argument is required and doesn't have a value.
*/
#validateRequiredArgument(
argument: PositionalArgumentDefinition,
value: ArgumentValue | ArgumentValue[],
) {
if (argument.defaultValue === undefined && value === undefined) {
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.MISSING_VALUE_FOR_TASK_ARGUMENT,
{
argument: argument.name,
task: formatTaskId(this.id),
},
);
}
}
/**
* Validates that a argument has the correct type. If the argument is optional
* and doesn't have a value, the type is not validated as it will be resolved
* to the default value.
*
* @throws HardhatError if the argument has an invalid type.
*/
#validateArgumentType(
argument: OptionDefinition | PositionalArgumentDefinition,
value: ArgumentValue | ArgumentValue[],
isVariadic: boolean = false,
) {
// skip type validation for optional arguments with undefined value
if (value === undefined && argument.defaultValue !== undefined) {
return;
}
// check if the value is valid for the argument type
if (!isArgumentValueValid(argument.type, value, isVariadic)) {
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_VALUE_FOR_TYPE,
{
value,
name: argument.name,
type: argument.type,
task: formatTaskId(this.id),
},
);
}
}
/**
* Validates that no extra arguments were provided in the task arguments.
*
* @throws HardhatError if extra arguments were provided. The error message
* includes the name of the first extra argument.
*/
#validateExtraArguments(providedArgumentNames: Set<string>) {
if (providedArgumentNames.size > 0) {
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.UNRECOGNIZED_TASK_OPTION,
{
option: [...providedArgumentNames][0],
task: formatTaskId(this.id),
},
);
}
}
/**
* Resolves the action file for a task. The action file is imported and the
* default export function is returned.
*
* @throws HardhatError if the module can't be imported or doesn't have a
* default export function.
*/
async #resolveFileAction(
actionFileUrl: string,
actionPluginId?: string,
): Promise<NewTaskActionFunction | TaskOverrideActionFunction> {
let resolvedActionFn;
try {
resolvedActionFn = await import(actionFileUrl);
} catch (error) {
ensureError(error);
if (actionPluginId !== undefined) {
const plugin = this.#hre.config.plugins.find(
(p) => p.id === actionPluginId,
);
assertHardhatInvariant(
plugin !== undefined,
`Plugin with id ${actionPluginId} not found.`,
);
await detectPluginNpmDependencyProblems(plugin);
}
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_ACTION_URL,
{
action: actionFileUrl,
task: formatTaskId(this.id),
},
error,
);
}
if (typeof resolvedActionFn.default !== "function") {
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_ACTION,
{
action: actionFileUrl,
task: formatTaskId(this.id),
},
);
}
return resolvedActionFn.default;
}
}