UNPKG

1.88 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const commander = require('commander');
4const CLIEngine = require('eslint').CLIEngine;
5const path = require('path');
6const os = require('os');
7const tsmlb = require('tsmlb');
8const filterer = require('./filterer');
9const ignores = require('./ignores');
10const pkg = require(path.join(__dirname, 'package.json'));
11
12commander.
13 version(pkg.version).
14 option('-e, --errors', 'Produces a report that only includes errors; not warnings.').
15 option('-w, --warnings', 'Produces a report that only includes warnings; not errors.').
16 option('--format', 'Formats files where possible to comply with videojs-standard.').
17 arguments('[targets...]').
18 action(targets => {
19 commander.targets = targets;
20 }).
21 parse(process.argv);
22
23// If no targets were specified, default to this directory.
24if (!commander.targets) {
25 commander.targets = ['.'];
26}
27
28const cli = new CLIEngine({
29 cwd: process.cwd(),
30 configFile: path.join(__dirname, 'eslintrc.json'),
31 fix: Boolean(commander.format),
32 ignorePattern: ignores(process.cwd())
33});
34
35const report = filterer(cli.executeOnFiles(commander.targets),
36 commander.errors,
37 commander.warnings);
38
39if (commander.format) {
40 CLIEngine.outputFixes(report);
41
42 const applied = report.results.
43 map(result => result.output ? result.filePath : '').
44 filter(Boolean);
45
46 if (applied.length) {
47 console.log(tsmlb`
48 Applied fixes to ${applied.length} files:
49 ${applied.join(os.EOL + ' ')}
50 `);
51 }
52
53 if (applied.length < report.results.length) {
54 console.log(tsmlb`
55 Could not apply fixes to ${applied.length ? report.results.length - applied.length : 'any'} files!
56 `);
57 }
58} else {
59 const formatter = cli.getFormatter();
60
61 console.log(formatter(report.results));
62}
63
64// Exit with a correct code.
65process.exit(report.errorCount ? 1 : 0);