1 | import chalk from "chalk";
|
2 | import debug from "debug";
|
3 | import fsExtra from "fs-extra";
|
4 | import * as path from "path";
|
5 |
|
6 | import {
|
7 | SOLC_INPUT_FILENAME,
|
8 | SOLC_OUTPUT_FILENAME,
|
9 | } from "../../internal/constants";
|
10 | import { Reporter } from "../../internal/sentry/reporter";
|
11 | import { EthereumProvider, ProjectPaths, SolcConfig } from "../../types";
|
12 |
|
13 | const log = debug("buidler:core:compilation-watcher");
|
14 |
|
15 | export async function watchCompilerOutput(
|
16 | provider: EthereumProvider,
|
17 | solcConfig: SolcConfig,
|
18 | paths: ProjectPaths
|
19 | ) {
|
20 | const chokidar = await import("chokidar");
|
21 |
|
22 | const compilerVersion = solcConfig.version;
|
23 | const solcInputPath = path.join(paths.cache, SOLC_INPUT_FILENAME);
|
24 | const solcOutputPath = path.join(paths.cache, SOLC_OUTPUT_FILENAME);
|
25 |
|
26 | const addCompilationResult = async () => {
|
27 | if (
|
28 | !(await fsExtra.pathExists(path.join(paths.cache, SOLC_INPUT_FILENAME)))
|
29 | ) {
|
30 | return false;
|
31 | }
|
32 |
|
33 | if (
|
34 | !(await fsExtra.pathExists(path.join(paths.cache, SOLC_OUTPUT_FILENAME)))
|
35 | ) {
|
36 | return false;
|
37 | }
|
38 |
|
39 | try {
|
40 | log("Adding new compilation result to the node");
|
41 |
|
42 | const compilerInput = await fsExtra.readJSON(solcInputPath, {
|
43 | encoding: "utf8",
|
44 | });
|
45 | const compilerOutput = await fsExtra.readJSON(solcOutputPath, {
|
46 | encoding: "utf8",
|
47 | });
|
48 |
|
49 | await provider.send("buidler_addCompilationResult", [
|
50 | compilerVersion,
|
51 | compilerInput,
|
52 | compilerOutput,
|
53 | ]);
|
54 | } catch (error) {
|
55 | console.warn(
|
56 | chalk.yellow(
|
57 | "There was a problem adding the new compiler result. Run Buidler with --verbose to learn more."
|
58 | )
|
59 | );
|
60 |
|
61 | log(
|
62 | "Last compilation result couldn't be added. Please report this to help us improve Buidler.\n",
|
63 | error
|
64 | );
|
65 |
|
66 | Reporter.reportError(error);
|
67 | }
|
68 | };
|
69 |
|
70 | log(`Watching changes on '${solcOutputPath}'`);
|
71 |
|
72 | chokidar
|
73 | .watch(solcOutputPath, {
|
74 | ignoreInitial: true,
|
75 | awaitWriteFinish: {
|
76 | stabilityThreshold: 250,
|
77 | pollInterval: 50,
|
78 | },
|
79 | })
|
80 | .on("add", addCompilationResult)
|
81 | .on("change", addCompilationResult);
|
82 | }
|