forked from protofire/solhint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolhint.js
executable file
·130 lines (103 loc) · 3.54 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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.10');
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 reportLists = program.args.filter(_.isString).map(processPath);
const reports =_.flatten(reportLists);
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 configPath = '.solhint.json';
const sampleConfig = [
'{ ',
' "extends": "default", ',
' "rules": { ',
' "indent": ["error", 4], ',
' "quotes": ["error", "double"], ',
' "max-line-length": ["error", 120] ',
' } ',
'} '
];
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, sampleConfig.join('\n'));
console.log('Configuration file created!');
} else {
console.log('Configuration file already exists');
}
}
const readIgnore = _.memoize(function () {
try {
return fs
.readFileSync('.solhintignore')
.toString()
.split('\n')
.map(i => i.trim());
} catch (e) {
return [];
}
});
const readConfig = _.memoize(function () {
let config = {};
try {
const configStr = fs.readFileSync('.solhint.json').toString();
config = JSON.parse(configStr);
} catch (e) {
if (e instanceof SyntaxError) {
console.log('ERROR: Configuration file [.solhint.json] is not a valid JSON!\n');
process.exit(0);
}
}
const configExcludeFiles = _.flatten(config.excludedFiles);
config.excludedFiles = _.concat(configExcludeFiles, readIgnore());
return config;
});
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();