-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.ts
63 lines (49 loc) · 1.58 KB
/
index.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
import {
TASK_RUN,
TASK_TEST
} from "@nomiclabs/buidler/builtin-tasks/task-names";
import { extendConfig, task } from "@nomiclabs/buidler/config";
import { ensurePluginLoadedWithUsePlugin } from "@nomiclabs/buidler/plugins";
import {
BuidlerRuntimeEnvironment,
RunSuperFunction,
TaskArguments
} from "@nomiclabs/buidler/types";
import debug from "debug";
const log = debug("buidler:plugin:ganache");
import { GanacheService } from "./ganache-service";
ensurePluginLoadedWithUsePlugin();
export default function() {
task(TASK_TEST, async (args, env, runSuper) => {
return handlePluginTask(args, env, runSuper);
});
task(TASK_RUN, async (args, env, runSuper) => {
return handlePluginTask(args, env, runSuper);
});
extendConfig((resolvedConfig: any, config: any) => {
const defaultOptions = GanacheService.getDefaultOptions();
if (config.networks && config.networks.ganache) {
const customOptions = config.networks.ganache;
resolvedConfig.networks.ganache = { ...defaultOptions, ...customOptions };
} else {
resolvedConfig.networks.ganache = defaultOptions;
}
});
}
async function handlePluginTask(
args: string,
env: BuidlerRuntimeEnvironment,
runSuper: RunSuperFunction<TaskArguments>
) {
if (env.network.name !== "ganache") {
return runSuper();
}
log("Starting Ganache");
const options = env.network.config;
const ganacheService = await GanacheService.create(options);
await ganacheService.startServer();
const ret = await runSuper();
log("Stopping Ganache");
await ganacheService.stopServer();
return ret;
}