forked from lwhiteley/eslint-output
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
121 lines (104 loc) · 3.05 KB
/
Copy pathapp.js
File metadata and controls
121 lines (104 loc) · 3.05 KB
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
#!/usr/bin/env node
const { ESLint } = require('eslint');
const path = require('path');
const yargs = require('yargs');
const write = require('write');
const debug = require('debug')('eslint-output');
const rc = require('./config');
const cwd = path.resolve(process.cwd());
const {
formatOverrides,
maxWarnings,
quiet,
_: additionalArguments,
} = yargs.options({
formatOverrides: { type: 'array', alias: 'o' },
maxWarnings: { type: 'number', alias: 'm' },
quiet: { type: 'boolean', default: false, alias: 'q' },
}).argv;
const config = {
useEslintrc: true,
...(rc.eslintConfig || {}),
cwd,
};
const cli = new ESLint(config);
const createReport = async () => {
const filesToVerify = additionalArguments.length
? additionalArguments
: rc.files || ['.'];
let report = await cli.lintFiles(filesToVerify);
if (quiet) {
report = ESLint.getErrorResults(report);
}
const totalWarnings = report.reduce(
(prev, cur) => prev + cur.warningCount,
0,
);
const totalErrors = report.reduce((prev, cur) => prev + cur.errorCount, 0);
const outputs = {
console(output) {
return console.log(output);
},
file(output, format) {
if (!format.path) {
return debug(
`a 'path' prop is required for this format (${format.name}), please specify and run again`,
);
}
try {
return write.sync(path.resolve(cwd, format.path), output);
} catch (e) {
return debug(
`Could not write file for eslint format: ${format.name} - ${e.message}`,
);
}
},
};
let { formats } = rc;
if (!Array.isArray(rc.formats)) {
formats = [{ name: 'stylish' }];
debug("using default format 'stylish'");
}
if (formatOverrides) {
formatOverrides.forEach((override) => {
const [idKey, value] = override.split('=');
const [id, key] = idKey.split('.');
if (id !== undefined && key !== undefined && value !== undefined) {
const index = formats.findIndex((format) => format.id === id);
if (index >= 0) {
formats[index][key] = value;
} else {
debug(`no format entry with id ${id} found.`);
}
} else {
debug(
`invalid override provided: ${override}. expected format: id.key=value`,
);
}
});
}
formats.forEach(async (format) => {
const formatter = await cli.loadFormatter(format.name);
if (formatter) {
const outputMethod = outputs[format.output] || outputs.console;
outputMethod(formatter.format(report), format, report);
} else {
debug(`could not find formatter with name ${format.name}`);
}
});
const isRunFailed = () => {
const exceededMaxWarnings =
typeof maxWarnings === 'number' && totalWarnings > maxWarnings;
if (exceededMaxWarnings) {
console.error(
`Max warnings of ${maxWarnings} exceeded: ${totalWarnings} warnings`,
);
}
return totalErrors > 0 || exceededMaxWarnings;
};
if (isRunFailed()) {
process.exitCode = 1;
debug('exited with code: 1');
}
};
createReport();