Mailspring/build/tasks/eslint-task.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
var chalk = require('chalk');
var eslint = require('eslint');
2016-05-04 03:37:32 +08:00
module.exports = function (grunt) {
grunt.registerMultiTask('eslint', 'Validate files with ESLint', function () {
var opts = this.options({
2016-05-04 03:37:32 +08:00
outputFile: false,
quiet: false,
maxWarnings: -1
2016-05-04 03:37:32 +08:00
});
// legacy
// TODO: remove in the future
if (opts.config) {
opts.configFile = opts.config;
}
if (opts.rulesdir) {
opts.rulePaths = opts.rulesdir;
}
if (this.filesSrc.length === 0) {
grunt.log.writeln(chalk.magenta('Could not find any files to validate.'));
return true;
}
var formatter = eslint.CLIEngine.getFormatter(opts.format);
2016-05-04 03:37:32 +08:00
if (!formatter) {
grunt.warn('Could not find formatter ' + opts.format + '\'.');
2016-05-04 03:37:32 +08:00
return false;
}
var engine = new eslint.CLIEngine(opts);
2016-05-04 03:37:32 +08:00
var report;
2016-05-04 03:37:32 +08:00
try {
report = engine.executeOnFiles(this.filesSrc);
} catch (err) {
grunt.warn(err);
return false;
}
if (opts.fix) {
eslint.CLIEngine.outputFixes(report);
}
var results = report.results;
2016-05-04 03:37:32 +08:00
if (opts.quiet) {
results = eslint.CLIEngine.getErrorResults(results);
}
var output = formatter(results);
2016-05-04 03:37:32 +08:00
if (opts.outputFile) {
grunt.file.write(opts.outputFile, output);
} else if (output) {
console.log(output);
}
var tooManyWarnings = opts.maxWarnings >= 0 && report.warningCount > opts.maxWarnings;
2016-05-04 03:37:32 +08:00
if (report.errorCount === 0 && tooManyWarnings) {
grunt.warn('ESLint found too many warnings (maximum:' + opts.maxWarnings + ')');
2016-05-04 03:37:32 +08:00
}
return report.errorCount === 0;
});
};