Skip to content

Commit

Permalink
Move some methods to separate module
Browse files Browse the repository at this point in the history
Also accept JSCS options as task options

Fix jscs-dev#4
  • Loading branch information
markelog committed Dec 14, 2013
1 parent 17b10b0 commit 2d77f2a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 16 deletions.
22 changes: 6 additions & 16 deletions tasks/jscs.js
Expand Up @@ -2,6 +2,7 @@ module.exports = function( grunt ) {
"use strict";

var fs = require( "fs" ),
jscs = require( "./lib/jscs" ).init( grunt ),
builder = require( "xmlbuilder" ),
Checker = require( "jscs/lib/checker" ),
defaults = {
Expand All @@ -10,26 +11,15 @@ module.exports = function( grunt ) {

grunt.registerMultiTask( "jscs", "JavaScript Code Style checker", function() {
var errorCount, i,
jscs = new Checker(),
checker = new Checker(),
options = this.options( defaults ),
cfgPath = options.config,
config = jscs.getConfig( options ),
files = this.filesSrc,
done = this.async(),
junitXML = options.junit ? builder.create( "testsuites" ) : null;

if ( !grunt.file.isPathAbsolute( cfgPath ) ) {
// Prepend the cwd, as jscs does via CLI
cfgPath = process.cwd() + "/" + options.config;
}

if ( !fs.existsSync( cfgPath ) ) {
// Can go further without an config file.
// TODO: Accept all jscs configs in the options object.
grunt.fail.fatal( "The config file " + options.config + " was not found!" );
}

jscs.registerDefaultRules();
jscs.configure( require( cfgPath ) );
checker.registerDefaultRules();
checker.configure( config );

errorCount = i = 0;

Expand Down Expand Up @@ -65,7 +55,7 @@ module.exports = function( grunt ) {
}
}

files.map( jscs.checkFile, jscs ).forEach(function( promise ) {
files.map( checker.checkFile, checker ).forEach(function( promise ) {
if ( !promise ) {
update();
return;
Expand Down
81 changes: 81 additions & 0 deletions tasks/lib/jscs.js
@@ -0,0 +1,81 @@
exports.init = function( grunt ) {

// Task specific options
var taskOptions = [ "config" ];

/**
* @see jQuery.isEmptyObject
* @private
*/
function isEmptyObject( obj ) {
var name;

for ( name in obj ) {
return false;
}

return true;
}

/**
* Get config
* @param {Object} options
* @return {Object}
*/
exports.getConfig = function( options ) {
var config = options.config && this.findConfig( options.config ) || this.getOptions( options );

if ( !config ) {
if ( options.config ) {
grunt.fail.fatal( "The config file " + options.config + " was not found" );

} else {
grunt.fail.fatal( "Nor config file nor inline options was not found" );
}
}

return config;
}

/**
* Read config file
* @param {String} path
* @return {Boolean|Object}
*/
exports.findConfig = function( path ) {
if ( !grunt.file.isPathAbsolute( path ) ) {

// Prepend the cwd, as jscs does via CLI
path = process.cwd() + "/" + path;
}

if ( grunt.file.exists( path ) ) {
return grunt.file.readJSON( path );
}

return false;
}

/**
* Get inline options
* @param {Object} options
* @return {Boolean|Object}
*/
exports.getOptions = function( options ) {
var _options = {};

// Copy options to another object so this method would not be destructive
for ( var option in options ) {

// If to jscs would be given a grunt task option
// that not defined in jscs it would throw
if ( !~taskOptions.indexOf( option ) ) {
_options[ option ] = options[ option ]
}
}

return isEmptyObject( _options ) ? false : _options;
}

return exports;
}

0 comments on commit 2d77f2a

Please sign in to comment.