-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.ts
100 lines (88 loc) · 2.32 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
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
import { internalTask, task } from "@nomiclabs/buidler/config";
import { BuidlerPluginError } from "@nomiclabs/buidler/internal/core/errors";
import * as fs from "fs";
import { join } from "path";
function getDefaultConfig() {
return {
extends: ["solhint:default"]
};
}
function getFormatter(formatterName = "stylish") {
try {
const solhintPath = require.resolve("solhint");
const formatterPath = require.resolve(
`eslint/lib/formatters/${formatterName}`,
{ paths: [solhintPath] }
);
return require(formatterPath);
} catch (ex) {
throw new BuidlerPluginError(
`An error occurred loading the solhint formatter ${formatterName}`,
ex
);
}
}
async function hasConfigFile(rootDirectory: string) {
const files = [
".solhint.json",
".solhintrc",
".solhintrc.json",
".solhintrc.yaml",
".solhintrc.yml",
".solhintrc.js",
"solhint.config.js"
];
for (const file of files) {
if (fs.existsSync(join(rootDirectory, file))) {
return true;
}
}
return false;
}
async function getSolhintConfig(rootDirectory: string) {
let solhintConfig;
const { loadConfig, applyExtends } = await import(
"solhint/lib/config/config-file"
);
if (await hasConfigFile(rootDirectory)) {
try {
solhintConfig = await loadConfig();
} catch (err) {
throw new BuidlerPluginError(
"An error occurred when loading your solhint config.",
err
);
}
} else {
solhintConfig = getDefaultConfig();
}
try {
solhintConfig = applyExtends(solhintConfig);
} catch (err) {
throw new BuidlerPluginError(
"An error occurred when processing your solhint config.",
err
);
}
return solhintConfig;
}
function printReport(reports: any) {
const formatter = getFormatter();
console.log(formatter(reports));
}
export default function() {
internalTask("buidler-solhint:run-solhint", async (_, { config }) => {
const { processPath } = await import("solhint/lib/index");
return processPath(
join(config.paths.sources, "**", "*.sol"),
await getSolhintConfig(config.paths.root)
);
});
task("check", async (_, { run }, runSuper) => {
if (runSuper.isDefined) {
await runSuper();
}
const reports = await run("buidler-solhint:run-solhint");
printReport(reports);
});
}