-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolhint.js
executable file
·111 lines (87 loc) · 2.8 KB
/
solhint.js
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
#!/usr/bin/env node
const program = require('commander');
const linter = require('./lib/index');
const _ = require('lodash');
const fs = require('fs');
const process = require('process');
function init () {
program
.version('1.1.5');
program
.usage('[options] <file> [...other_files]')
.option('-f, --formatter [name]', 'report formatter name (stylish, table, tap, unix)')
.description('Linter for Solidity programming language')
.action(execMainAction);
program
.command('stdin')
.description('linting of source code data provided to STDIN')
.option('--filename [file_name]', 'name of file received using STDIN')
.action(processStdin);
program
.command('init-config')
.description('create in current directory configuration file for solhint')
.action(writeSampleConfigFile);
program
.parse(process.argv);
program.args.length < 1
&& program.help();
}
function execMainAction () {
const pathPromises = program
.args
.filter(i => typeof(i) === 'string')
.map(processPath);
Promise
.all(pathPromises)
.then(items => _.flatten(items))
.then(reports => {
printReports(reports, program.formatter);
exitWithCode(reports);
});
}
function processStdin(options) {
const STDIN_FILE = 0;
const stdinBuffer = fs.readFileSync(STDIN_FILE);
const report = processStr(stdinBuffer.toString());
report.file = options.filename || 'stdin';
printReports([report]);
}
function writeSampleConfigFile() {
const sampleConfig = {
extends: 'default',
rules: {
'no-complex-fallback': 'warn',
'indent': ['error', 4],
'quotes': ['error', 'double'],
'max-line-length': ['error', 120]
}
};
const sampleConfigJson = JSON.stringify(sampleConfig, (k, v) => v, 4);
fs.writeFileSync('.solhint.json', sampleConfigJson);
console.log('Configuration file created!');
}
const readConfig = _.memoize(function () {
try {
const configStr = fs.readFileSync('.solhint.json').toString();
return JSON.parse(configStr);
} catch (e) {
return { rules: {} };
}
});
function processStr(input) {
return linter.processStr(input, readConfig());
}
function processPath(path) {
return linter.processPath(path, readConfig());
}
function printReports (reports, formatter) {
const formatterName = formatter || 'stylish';
const formatterFn = require(`eslint/lib/formatters/${formatterName}`);
console.log(formatterFn(reports));
return reports;
}
function exitWithCode(reports) {
const errorsCount = reports.reduce((acc, i) => acc + i.errorCount, 0);
process.exit(errorsCount > 0 ? 1 : 0);
}
init();