[484] | 1 | /* |
---|
| 2 | * grunt-contrib-jshint |
---|
| 3 | * http://gruntjs.com/ |
---|
| 4 | * |
---|
| 5 | * Copyright (c) 2013 "Cowboy" Ben Alman, contributors |
---|
| 6 | * Licensed under the MIT license. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | 'use strict'; |
---|
| 10 | |
---|
| 11 | module.exports = function(grunt) { |
---|
| 12 | |
---|
| 13 | var path = require('path'); |
---|
| 14 | var jshint = require('./lib/jshint').init(grunt); |
---|
| 15 | |
---|
| 16 | grunt.registerMultiTask('jshint', 'Validate files with JSHint.', function() { |
---|
| 17 | var done = this.async(); |
---|
| 18 | |
---|
| 19 | // Merge task-specific and/or target-specific options with these defaults. |
---|
| 20 | var options = this.options({ |
---|
| 21 | force: false, |
---|
| 22 | reporterOutput: null, |
---|
| 23 | }); |
---|
| 24 | |
---|
| 25 | // log (verbose) options before hooking in the reporter |
---|
| 26 | grunt.verbose.writeflags(options, 'JSHint options'); |
---|
| 27 | |
---|
| 28 | // Report JSHint errors but dont fail the task |
---|
| 29 | var force = options.force; |
---|
| 30 | delete options.force; |
---|
| 31 | |
---|
| 32 | // Whether to output the report to a file |
---|
| 33 | var reporterOutput = options.reporterOutput; |
---|
| 34 | delete options.reporterOutput; |
---|
| 35 | |
---|
| 36 | // Hook into stdout to capture report |
---|
| 37 | var output = ''; |
---|
| 38 | if (reporterOutput) { |
---|
| 39 | grunt.util.hooker.hook(process.stdout, 'write', { |
---|
| 40 | pre: function(out) { |
---|
| 41 | output += out; |
---|
| 42 | return grunt.util.hooker.preempt(); |
---|
| 43 | } |
---|
| 44 | }); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | jshint.lint(this.filesSrc, options, function(results, data) { |
---|
| 48 | var failed = 0; |
---|
| 49 | if (results.length > 0) { |
---|
| 50 | // Fail task if errors were logged except if force was set. |
---|
| 51 | failed = force; |
---|
| 52 | } else { |
---|
| 53 | if (jshint.usingGruntReporter === true && data.length > 0) { |
---|
| 54 | grunt.log.ok(data.length + ' file' + (data.length === 1 ? '' : 's') + ' lint free.'); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // Write the output of the reporter if wanted |
---|
| 59 | if (reporterOutput) { |
---|
| 60 | grunt.util.hooker.unhook(process.stdout, 'write'); |
---|
| 61 | reporterOutput = grunt.template.process(reporterOutput); |
---|
| 62 | var destDir = path.dirname(reporterOutput); |
---|
| 63 | if (!grunt.file.exists(destDir)) { |
---|
| 64 | grunt.file.mkdir(destDir); |
---|
| 65 | } |
---|
| 66 | grunt.file.write(reporterOutput, output); |
---|
| 67 | grunt.log.ok('Report "' + reporterOutput + '" created.'); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | done(failed); |
---|
| 71 | }); |
---|
| 72 | }); |
---|
| 73 | |
---|
| 74 | }; |
---|