This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcli.js
executable file
·76 lines (65 loc) · 1.8 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
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const updateNotifier = require('update-notifier');
const meow = require('meow');
const pretty = require('eslint-formatter-pretty');
const { ESLint, CLIEngine } = require('eslint');
const marlint = require('./');
const stylish = CLIEngine.getFormatter();
const cli = meow(
{
help: [
'Usage',
' $ marlint [<file|glob> ...]',
'',
'Options',
' --quiet Disable warning errors',
' --fix Automagically fixes code',
' --json Output JSON result to be consumed in other app',
' --verbose Show debug log',
'',
'Examples',
' $ marlint',
' $ marlint --fix',
' $ marlint --quiet --json lint-result.json',
'',
'Tips',
' Put options in package.json instead of using flags so other tools can read it.',
],
},
{
boolean: ['quiet', 'fix', 'verbose'],
}
);
updateNotifier({ pkg: cli.pkg }).notify();
const input = cli.input;
const opts = cli.flags;
marlint
.lintFiles(input, opts)
.then((results) => {
if (opts.fix) {
ESLint.outputFixes(report);
}
if (opts.verbose) {
console.log('Lint finished');
}
if (opts.quiet) {
results = ESLint.getErrorResults(results);
}
const output = stylish(results);
if (opts.json) {
const formatter = CLIEngine.getFormatter('json');
fs.writeFileSync(opts.json, formatter(results));
process.stdout.write(`Test results written to: ${opts.json}\n`);
}
process.stdout.write(output);
process.exit(getErrorCount(results) === 0 ? 0 : 1);
})
.catch((err) => {
console.error(err.stack);
process.exit(1);
});
function getErrorCount(results) {
return results.reduce((acc, res) => acc + res.errorCount, 0);
}