UNPKG

3.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.HardhatContext = void 0;
4const errors_1 = require("./core/errors");
5const errors_list_1 = require("./core/errors-list");
6const vars_manager_1 = require("./core/vars/vars-manager");
7const dsl_1 = require("./core/tasks/dsl");
8const global_dir_1 = require("./util/global-dir");
9const platform_1 = require("./util/platform");
10class HardhatContext {
11 constructor() {
12 this.tasksDSL = new dsl_1.TasksDSL();
13 this.environmentExtenders = [];
14 this.providerExtenders = [];
15 this.configExtenders = [];
16 // NOTE: This is experimental and will be removed. Please contact our team if
17 // you are planning to use it.
18 this.experimentalHardhatNetworkMessageTraceHooks = [];
19 this.varsManager = new vars_manager_1.VarsManager((0, global_dir_1.getVarsFilePath)());
20 }
21 static isCreated() {
22 const globalWithHardhatContext = global;
23 return globalWithHardhatContext.__hardhatContext !== undefined;
24 }
25 static createHardhatContext() {
26 if (this.isCreated()) {
27 throw new errors_1.HardhatError(errors_list_1.ERRORS.GENERAL.CONTEXT_ALREADY_CREATED);
28 }
29 const globalWithHardhatContext = global;
30 const ctx = new HardhatContext();
31 globalWithHardhatContext.__hardhatContext = ctx;
32 return ctx;
33 }
34 static getHardhatContext() {
35 const globalWithHardhatContext = global;
36 const ctx = globalWithHardhatContext.__hardhatContext;
37 if (ctx === undefined) {
38 throw new errors_1.HardhatError(errors_list_1.ERRORS.GENERAL.CONTEXT_NOT_CREATED);
39 }
40 return ctx;
41 }
42 static deleteHardhatContext() {
43 const globalAsAny = global;
44 globalAsAny.__hardhatContext = undefined;
45 }
46 setHardhatRuntimeEnvironment(env) {
47 if (this.environment !== undefined) {
48 throw new errors_1.HardhatError(errors_list_1.ERRORS.GENERAL.CONTEXT_HRE_ALREADY_DEFINED);
49 }
50 this.environment = env;
51 }
52 getHardhatRuntimeEnvironment() {
53 if (this.environment === undefined) {
54 throw new errors_1.HardhatError(errors_list_1.ERRORS.GENERAL.CONTEXT_HRE_NOT_DEFINED);
55 }
56 return this.environment;
57 }
58 setConfigLoadingAsStarted() {
59 this._filesLoadedBeforeConfig = (0, platform_1.getRequireCachedFiles)();
60 }
61 setConfigLoadingAsFinished() {
62 this._filesLoadedAfterConfig = (0, platform_1.getRequireCachedFiles)();
63 }
64 getFilesLoadedDuringConfig() {
65 // No config was loaded
66 if (this._filesLoadedBeforeConfig === undefined) {
67 return [];
68 }
69 (0, errors_1.assertHardhatInvariant)(this._filesLoadedAfterConfig !== undefined, "Config loading was set as started and not finished");
70 return arraysDifference(this._filesLoadedAfterConfig, this._filesLoadedBeforeConfig);
71 }
72}
73exports.HardhatContext = HardhatContext;
74function arraysDifference(a, b) {
75 return a.filter((e) => !b.includes(e));
76}
77//# sourceMappingURL=context.js.map
\No newline at end of file