UNPKG

grunt-phplint

Version:

A Grunt 4.0 task for running phplint on your php files

78 lines (59 loc) 1.44 kB
'use strict'; var spawn = require("child_process").spawn; var grunt = require("grunt"), _ = grunt.util._; var PhpLintCommandWrapper = function(options) { this.options = options; }; PhpLintCommandWrapper.prototype = { lintFile: function(filePath, done) { var cmdArgs = this._parseOptions(this.options.phpArgs), phpLintCmd = spawn(this.options.phpCmd, cmdArgs.concat([filePath])), output = ""; phpLintCmd.stdout.on("data", function(data) { output += data; }); phpLintCmd.stderr.on("data", function(data) { done(new Error(data), output); }); phpLintCmd.on("exit", function(code) { if(code !== 0) { return done(new Error("php returned non zero code: " + code), output); } done(null, output); }); }, //Parse an object into an array of command line arguments _parseOptions: function(opts) { var newOpts, currKey, pushVal; // Convert an object to an array of opts if(_.isObject(opts)) { newOpts = []; currKey = ""; pushVal = function(v) { newOpts.push(currKey); if (v !== null) { newOpts.push(v); } }; _.each(opts, function(val, key) { if (val === false) { return; } currKey = key; if(_.isArray(val)) { // Push an array of values _.each(val, pushVal); } else { // Push a single value pushVal(val); } }); opts = newOpts; } return opts; } }; module.exports = PhpLintCommandWrapper;