From 8abeadcbc566b1cc26399108b63b2b0687289d86 Mon Sep 17 00:00:00 2001 From: Andrew Holgate Date: Fri, 1 May 2015 20:35:20 +0200 Subject: [PATCH] - Added "force" option to allow to continue running Grunt in the event of failures. - Removed empty spaces --- README.md | 5 +++++ tasks/phpcs.js | 36 ++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f6e9df7..64f7ca8 100644 --- a/README.md +++ b/README.md @@ -112,3 +112,8 @@ Log report to the file. Type: `Integer` Default: `false` Automatically convert tabs to the specified number of spaces when sniffing. + +####force +Type: `Boolean` Default: `false` + +Continue running grunt in the event of failures. diff --git a/tasks/phpcs.js b/tasks/phpcs.js index 5a5d079..aaaaa08 100644 --- a/tasks/phpcs.js +++ b/tasks/phpcs.js @@ -12,7 +12,7 @@ module.exports = function(grunt) { var path = require('path'), exec = require('child_process').exec; - + var command = { flags: { verbose: 'v', @@ -31,10 +31,11 @@ module.exports = function(grunt) { defaults = { bin: 'phpcs', report: 'full', - maxBuffer: 200*1024 + maxBuffer: 200*1024, + force: false }, done = null; - + grunt.registerMultiTask('phpcs', 'Run PHP Code Sniffer', function() { var done = null, parameters = null, @@ -42,32 +43,35 @@ module.exports = function(grunt) { options = this.options(defaults), execute = path.normalize(options.bin), files = [].concat.apply([], this.files.map(function(mapping) { return mapping.src; })).sort(); - + // removes duplicate files - files = files.filter(function(file, position) { + files = files.filter(function(file, position) { return !position || file !== files[position - 1]; }); - + // generates parameters parameters = Object.keys(options).map(function(option) { - return option in command.flags && options[option] === true ? - '-' + command.flags[option] : option in command.options && options[option] !== undefined ? + return option in command.flags && options[option] === true ? + '-' + command.flags[option] : option in command.options && options[option] !== undefined ? '--' + command.options[option] + '=' + options[option] : null; }).filter(Boolean); - + execute += ' ' + parameters.join(' ') + ' "' + files.join('" "') + '"'; - + grunt.verbose.writeln('Executing: ' + execute); - + done = this.async(); - + exec(execute, {maxBuffer: options.maxBuffer}, function(error, stdout, stderr) { /* jshint -W030 */ typeof options.callback === 'function' && options.callback.call(this, error, stdout, stderr, done); stdout && grunt.log.write(stdout); - error && grunt.fail.warn(stderr ? stderr : 'Task phpcs:' + target + ' failed.'); - !error && grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.'); - done(error); + if (!options.force && error) { + grunt.fail.warn(stderr ? stderr : 'Task phpcs:' + target + ' failed.'); + } else if (!error) { + grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' lint free.'); + } + done(); }); }); -}; \ No newline at end of file +};