UNPKG

1.66 kBJavaScriptView Raw
1module.exports = function(grunt) {
2 var coffeelint = require('coffeelint');
3 var reporter = require('coffeelint-stylish').reporter;
4
5 grunt.registerMultiTask('coffeelint', 'Validate files with CoffeeLint', function() {
6
7 var errorCount = 0;
8 var warnCount = 0;
9 var files = this.filesSrc;
10 var options = this.options();
11
12 if (options.configFile != undefined) {
13 var config = grunt.file.readJSON(options.configFile);
14 options.configFile = undefined;
15 for (var key in options) {
16 config[key] = options[key];
17 }
18 options = config;
19 }
20
21 files.forEach(function(file) {
22 grunt.verbose.writeln('Linting ' + file + '...');
23
24 var literate = !!file.match(/\.(litcoffee|coffee\.md)$/i);
25 var errors = coffeelint.lint(grunt.file.read(file), options, literate);
26
27 if (!errors.length) {
28 return grunt.verbose.ok();
29 }
30
31 reporter(file, errors);
32
33 errors.forEach(function(error) {
34 var status, message;
35
36 if (error.level === 'error') {
37 errorCount += 1;
38 } else if (error.level === 'warn') {
39 warnCount += 1;
40 } else {
41 return;
42 }
43
44 message = file + ':' + error.lineNumber + ' ' + error.message +
45 ' (' + error.rule + ')';
46
47 grunt.event.emit('coffeelint:' + error.level, error.level, message);
48 grunt.event.emit('coffeelint:any', error.level, message);
49 });
50 });
51
52 if (errorCount && !options.force) {
53 return false;
54 }
55
56 if (!warnCount && !errorCount) {
57 grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.');
58 }
59 });
60};