-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlint.js
94 lines (83 loc) · 2.21 KB
/
lint.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
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint
no-param-reassign: ["error", {
"props": true,
"ignorePropertyModificationsFor": ["args"]
}],
no-console: "off"
*/
const path = require('path');
const chalk = require('chalk');
const { CLIEngine } = require('eslint');
const optionsForApi = require('./eslintOptions');
// helpers ==========================
function camelize(str) {
return str.replace(/-(\w)/g, (_, c) => (
c ?
c.toUpperCase() :
''
));
}
function normalizeConfig(args) {
const config = {};
Object.keys(args).forEach((key) => {
if (key !== '_') {
config[camelize(key)] = args[key];
}
});
return config;
}
function format(label, msg) {
let lines = msg.split('\n');
lines = lines.map((line, idx) => (
idx === 0 ?
`${label} ${line}` :
line.padStart(chalk.reset(label).length)
));
return lines.join('\n');
}
// ==================================
module.exports = function lint(args = {}, api) {
const cwd = api.resolve('.');
const options = optionsForApi(api);
const files = args._ && args._.length ? args._ : ['src/**/*.{vue,js}'];
if (args['no-fix']) {
args.fix = false;
delete args['no-fix'];
}
const config = Object.assign({}, options, {
fix: true,
cwd,
}, normalizeConfig(args));
const engine = new CLIEngine(config);
const report = engine.executeOnFiles(files);
const formatter = engine.getFormatter(args.format || 'codeframe');
if (config.fix) {
CLIEngine.outputFixes(report);
}
if (!report.errorCount) {
if (!args.silent) {
const hasFixed = report.results.some(f => f.output);
if (hasFixed) {
console.log('The following files have been auto-fixed:');
console.log();
report.results.forEach((f) => {
if (f.output) {
console.log(chalk` {blue ${path.relative(cwd, f.filePath)}}`);
}
});
console.log();
}
if (report.warningCount) {
console.log(formatter(report.results));
} else {
console.log(format(
chalk`{bgGreen.black DONE }`,
hasFixed ? 'All lint errors auto-fixed.' : 'No lint errors found!',
));
}
}
} else {
console.log(formatter(report.results));
process.exit(1);
}
};