Skip to content

Commit

Permalink
added lint reports on overview, new options, logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrod Overson committed Jan 30, 2013
1 parent 6cd13c7 commit fe0d6e9
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 54 deletions.
1 change: 1 addition & 0 deletions .jshintrc
@@ -1,3 +1,4 @@

{
"curly": false,
"eqeqeq": true,
Expand Down
33 changes: 30 additions & 3 deletions Gruntfile.js
Expand Up @@ -74,12 +74,13 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-casper');

grunt.registerTask('runbin',function(){
grunt.registerTask('runtest',function(){
var done = this.async();

grunt.util.spawn({
cmd : './bin/plato',
args : [
'-q',
'-dtmp',
'-ttest report',
'test/fixtures/a.js','test/fixtures/b.js','test/fixtures/empty.js'
Expand All @@ -88,7 +89,33 @@ module.exports = function(grunt) {
function(err, result, code){
console.log(result.stdout);
if (err || code !== 0) {
grunt.fail('Running plato binary failed');
grunt.fatal('Running plato binary failed');
}
done();
}
);
});

grunt.registerTask('runbin',function(){
var done = this.async();

grunt.util.spawn({
cmd : './bin/plato',
args : [
'-q',
'-r',
'-l.jshintrc',
'-xvendor|bundles',
'-dreports',
'-tPlato report',
'lib/'
]
},
function(err, result, code){
console.log(result.stdout);
if (err || code !== 0) {
console.log(err);
grunt.fatal('Running plato binary failed');
}
done();
}
Expand All @@ -97,7 +124,7 @@ module.exports = function(grunt) {

grunt.registerTask('optimize', ['uglify']);
// Default task.
grunt.registerTask('test', ['jshint', 'nodeunit', 'runbin'/*, 'casperjs'*/]);
grunt.registerTask('test', ['jshint', 'nodeunit', 'runtest', 'runbin'/*, 'casperjs'*/]);
grunt.registerTask('default', ['test']);

};
2 changes: 1 addition & 1 deletion bin/plato
Expand Up @@ -17,5 +17,5 @@ if (cli.args.h) {
}

cli.exec(function(){
console.log('Done!')
console.log('Done!');
});
2 changes: 1 addition & 1 deletion lib/assets/css/plato-overview.css
Expand Up @@ -64,7 +64,7 @@
}

.plato-file-chart {
height:40px;
height:55px;
}

.plato-file-link {
Expand Down
52 changes: 35 additions & 17 deletions lib/assets/scripts/plato-overview.js
@@ -1,4 +1,4 @@
/*global $:false, _:false, Morris:false, __report:false, Raphael:false */
/*global $:false, _:false, Morris:false, __report:false, __options: false, Raphael:false */
/*jshint browser:true*/

$(function(){
Expand All @@ -10,9 +10,9 @@ $(function(){
// $('.plato-file-link').fitText(1.2, { minFontSize: '20px', maxFontSize: '28px' });

var colors = [
'#50ABD2',
'#ED913D',
'#E8182E'
'#01939A',
'#FFAB00',
'#FF0700'
];

var graphHeight = 10,
Expand Down Expand Up @@ -58,39 +58,51 @@ $(function(){

value = report.complexity.aggregate.complexity.halstead.bugs.toFixed(2);
horizontalGraph(chart,2,value, value * 5, 'est bugs', getColor(value,colors,[1,5]));

value = report.jshint.messages;
horizontalGraph(chart,3,value, value * 5, 'lint errors', getColor(value,colors,[1,10]));
},0);
});
}

function drawOverviewCharts(reports) {
$('.chart').empty();

var maintainability = {
element: 'chart_maintainability',
data: [],
xkey: 'label',
ykeys: ['value'],
ymax : 171,
labels: ['Maintainability'],
barColors : ['#ff9b40']
};
var sloc = {
element: 'chart_sloc',
data: [],
xkey: 'label',
ykeys: ['value'],
ymax : 400,
labels: ['Lines'],
barColors : ['#FAAF78']
barColors : ['#1f6b75']
};
var maintainability = {
element: 'chart_maintainability',
var bugs = {
element: 'chart_bugs',
data: [],
xkey: 'label',
ykeys: ['value'],
ymax : 171,
labels: ['Maintainability'],
barColors : ['#FAAF78']
labels: ['Bugs'],
ymax: 20,
barColors : ['#ff9b40']
};
var bugs = {
element: 'chart_bugs',
var lint = {
element: 'chart_lint',
data: [],
xkey: 'label',
ykeys: ['value'],
labels: ['Bugs'],
labels: ['Errors'],
ymax: 20,
barColors : ['#D54C2C']
barColors : ['#1f6b75']
};

reports.forEach(function(report){
Expand All @@ -101,15 +113,19 @@ $(function(){

sloc.data.push({
value : report.complexity.aggregate.complexity.sloc.physical,
label : report.complexity.module
label : report.info.fileShort
});
bugs.data.push({
value : report.complexity.aggregate.complexity.halstead.bugs.toFixed(2),
label : report.complexity.module
label : report.info.fileShort
});
maintainability.data.push({
value : report.complexity.maintainability ? report.complexity.maintainability.toFixed(2) : 0,
label : report.complexity.module
label : report.info.fileShort
});
lint.data.push({
value : report.jshint && report.jshint.messages,
label : report.info.fileShort
});
});

Expand All @@ -123,6 +139,8 @@ $(function(){
Morris.Bar(maintainability)
];

if (__options.flags.jshint) charts.push(Morris.Bar(lint));

charts.forEach(function(chart){
chart.on('click', onGraphClick);
});
Expand Down
15 changes: 9 additions & 6 deletions lib/cli.js
Expand Up @@ -8,7 +8,8 @@ var getopt = require('posix-getopt');

// Local lib
var plato = require('./plato'),
info = require('./info');
info = require('./info'),
util = require('./util');

exports.exec = function(options, done) {
if (typeof options === 'function') {
Expand All @@ -26,14 +27,16 @@ exports.exec = function(options, done) {
var outputDir = exports.args.d.value;
var platoOptions = {
recurse : !!exports.args.r,
title : exports.args.t && exports.args.t.value
title : exports.args.t && exports.args.t.value,
exclude : exports.args.x && new RegExp(exports.args.x.value)
};

if (exports.args.l) {
var jshintrc = {};
if (typeof exports.args.l.value === 'string') {
var json = fs.readFileSync(exports.args.l.value);
jshintrc = JSON.parse(json);
var json = fs.readFileSync(exports.args.l.value).toString();

jshintrc = JSON.parse(util.stripComments(json));
}
platoOptions.jshint = { globals : jshintrc.globals || {} };
delete jshintrc.globals;
Expand All @@ -43,12 +46,12 @@ exports.exec = function(options, done) {
plato.inspect(files, outputDir, platoOptions, done);
};


exports.options = require('./cli/options');

exports.args = parseArgs(exports.options);

function parseArgs(options) {

function parseArgs(options) {// \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/
var optionString = '',
required = [];

Expand Down
10 changes: 10 additions & 0 deletions lib/cli/options.json
Expand Up @@ -4,11 +4,21 @@
"desc": "Display this help text.",
"type": "Boolean"
},
"q": {
"long": "quiet",
"desc": "Reduce output to errors only",
"type": "Boolean"
},
"v": {
"long": "version",
"desc": "Print the version.",
"type": "Boolean"
},
"x": {
"long": "exclude",
"desc": "File exclusion regex",
"type": "String"
},
"d": {
"long": "dir",
"desc": "The output directory",
Expand Down
23 changes: 17 additions & 6 deletions lib/plato.js
Expand Up @@ -19,6 +19,7 @@ var _ = require('lodash');

// local lib
var util = require('./util'),
Logger = require('./logger'),
reporters = {
complexity : require('./reporters/complexity'),
jshint : require('./reporters/jshint')
Expand All @@ -29,6 +30,8 @@ var overviewTemplate = __dirname + '/templates/overview.html',
assets = __dirname + '/assets/',
fileDir = 'files';

var log = new Logger(Logger.WARNING);

exports.inspect = function(files, outputDir, options, done) {

var flags = {
Expand All @@ -48,6 +51,8 @@ exports.inspect = function(files, outputDir, options, done) {
if (options[flag]) flags[flag] = _.clone(options[flag]);
});

if (options.q) log.level = Logger.ERROR;

var reports = [];

var fileOutputDir = path.join(outputDir,fileDir);
Expand All @@ -56,19 +61,21 @@ exports.inspect = function(files, outputDir, options, done) {

var runReports = function(files,done) {
files.forEach(function(file) {
if (options.exclude && options.exclude.test(file)) return;

if (options.recurse && fs.statSync(file).isDirectory()) {
files = fs.readdirSync(file).map(function(innerFile){
return path.join(file,innerFile);
});
runReports(files);
} else if (file.match(/\.js$/)) {
console.log('Reading "%s"', file);
log.info('Reading "%s"', file);

var fileShort = file.replace(commonBasePath, '');
var fileSafe = fileShort.replace(/[^a-zA-Z0-9]/g,'_');
var source = fs.readFileSync(file).toString().trim();
if (!source) {
console.log('Not parsing empty file "%s"', file);
log.info('Not parsing empty file "%s"', file);
return;
}
var report = {
Expand All @@ -86,8 +93,8 @@ exports.inspect = function(files, outputDir, options, done) {
report[name] = reporter.process(source, flags[name], report.info);
} catch(e) {
error = true;
console.log('Error reading file : ', e.toString());
console.log(e.stack);
log.error('Error reading file : ', e.toString());
log.error(e.stack);
return;
}
});
Expand Down Expand Up @@ -118,7 +125,8 @@ exports.inspect = function(files, outputDir, options, done) {
var overviewReport = exports.getOverviewReport(reports);
writeReport(reportFile, overviewReport);
writeOverview(overview, overviewReport, {
title : options.title
title : options.title,
flags : flags
});
done(reports);
});
Expand Down Expand Up @@ -150,6 +158,9 @@ exports.getOverviewReport = function (reports) {
var aggregate = _.cloneDeep(report.complexity.aggregate);
culledReports.push({
info : report.info,
jshint : {
messages : report.jshint.messages.length
},
complexity : {
aggregate : aggregate,
module : report.complexity.module,
Expand All @@ -169,7 +180,7 @@ exports.getOverviewReport = function (reports) {
};

function writeFile(file, source) {
console.log('Writing file "%s".', file);
log.info('Writing file "%s".', file);
fs.writeFileSync(file, source, 'utf8');
}

Expand Down
10 changes: 10 additions & 0 deletions lib/templates/overview.html
Expand Up @@ -12,6 +12,10 @@
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<script>
var __options = <%= JSON.stringify(options) %>
</script>

<link href="assets/css/vendor/bootstrap-3.0.0-wip.css" rel="stylesheet">
<link href="assets/css/vendor/font-awesome.css" rel="stylesheet">
<link href="assets/css/vendor/font-awesome-ie7.css" rel="stylesheet">
Expand Down Expand Up @@ -69,6 +73,12 @@ <h2 class="span12">Lines of code</h2>
<h2 class="span12">Estimated number of bugs</h2>
<div class="span12"><div id='chart_bugs' class='chart'></div></div>
</div>
<% if (options.flags.jshint) { %>
<div class="row">
<h2 class="span12">Lint errors</h2>
<div class="span12"><div id='chart_lint' class='chart'></div></div>
</div>
<% } %>
</div>


Expand Down
10 changes: 10 additions & 0 deletions lib/util.js
Expand Up @@ -23,3 +23,13 @@ exports.formatJSON = function (report) {
},2);
};

exports.stripComments = function (str) {
/*jshint regexp:false */
str = str || '';

var multiline = /\/\*(?:(?!\*\/)|.|\n)*?\*\//g;
var singleline = /\/\/.*/g;

return str.replace(multiline, '').replace(singleline, '');
};

0 comments on commit fe0d6e9

Please sign in to comment.