-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.js
executable file
·81 lines (65 loc) · 1.97 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
77
78
79
80
81
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const chalk = require('chalk');
const logSymbols = require('log-symbols');
const updateNotifier = require('update-notifier');
const groupBy = require('lodash.groupby');
const formatter = require('eslint-formatter-pretty');
const ghLint = require('.');
const cli = meow(`
Usage
$ clinton [<path>]
Options
--no-inherit Prevent inheriting from the default rules.
--ignores Ignore files. Can be added multiple times.
--fix Automatically fix problems.
Examples
$ clinton
${chalk.underline('.editorconfig')}
${logSymbols.warning} Use ${chalk.bold('.editorconfig')} to define and maintain consistent coding styles between editors. ${chalk.dim('editorconfig')}
${chalk.yellow('1 warning')}
$ clinton ~/projects/project
${chalk.underline('license')}
${logSymbols.error} No MIT license found. ${chalk.dim('license-mit')}
${chalk.red('1 error')}
`, {
boolean: ['inherit', 'fix'],
default: {
inherit: true
}
});
updateNotifier({pkg: cli.pkg}).notify();
const log = validations => {
const files = groupBy(validations, 'file');
const filePaths = Object.keys(files);
const ret = [];
for (const filePath of filePaths) {
ret.push({
filePath: filePath === 'undefined' ? 'Project' : filePath,
errorCount: files[filePath].reduce((c, m) => c + (m.severity === 'error' ? 1 : 0), 0),
warningCount: files[filePath].reduce((c, m) => c + (m.severity === 'warn' ? 1 : 0), 0),
messages: files[filePath]
});
}
if (ret.length > 0) {
console.log(formatter(ret));
}
};
const exit = validations => {
const hasError = validations.some(validation => validation.severity === 'error');
if (hasError) {
process.exit(1);
}
};
const lint = () => ghLint.lint(cli.input[0] || '.', cli.flags)
.then(validations => {
log(validations);
exit(validations);
});
if (cli.flags.fix) {
ghLint.fix(cli.input[0] || '.', cli.flags)
.then(() => lint());
} else {
lint();
}