-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
49 lines (42 loc) · 1.14 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs');
const getStdin = require('get-stdin');
const minimist = require('minimist');
const linter = require('.');
const argv = minimist(process.argv.slice(2), {
v: 'version',
h: 'help'
});
if (argv.v || argv.version) {
process.stdout.write(require('./package').version);
return;
}
if (argv.h || argv.help) {
fs.createReadStream(`${__dirname}/usage.txt`)
.pipe(process.stdout)
.on('close', () => process.exit(1));
return;
}
function output(report, reporter) {
const formatter = reporter ? linter.getFormatter(reporter) : require('eslint-formatter-pretty');
process.stdout.write(formatter(report.results));
process.exit(report.errorCount === 0 ? 0 : 1);
}
if (argv.stdin) {
getStdin().then(text => {
if (argv.fix) {
const report = linter.lintText(text, argv);
const result = report.results.shift();
console.log(result.output);
} else {
output(linter.lintText(text, argv), argv.reporter);
}
});
} else {
linter.lintFiles(argv._, argv).then(report => {
if (argv.fix) {
linter.outputFixes(report);
}
output(report, argv.reporter);
});
}