grunt-filesize-report
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "grunt-filesize-report", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "(Very) simple filesize reporter for grunt. Primary use was to generate a very simple xml format for use by Jenkins plot plugin.", | ||
@@ -26,3 +26,4 @@ "author": { | ||
"grunt": ">=0.4.x", | ||
"grunt-contrib-jshint": "^0.11.2" | ||
"grunt-contrib-jshint": "^0.11.2", | ||
"violation-reporter": "0.0.3" | ||
}, | ||
@@ -29,0 +30,0 @@ "keywords": [ |
@@ -15,12 +15,10 @@ # grunt-filesize-report | ||
options: { | ||
output: { | ||
folder: dirs.reports + '/filesize', .. defaults to './' | ||
short: true, //defaults to false - will output full info in the console | ||
xml: true, // default is false | ||
json: false, //default is false | ||
filename: 'myoutput' // defaults to 'filesize-[target].[ext]', | ||
thresholds: [102400,204800] // [warning, error] thresholds for individual files | ||
totalThresholds: [524288,1048576] // [warning, error] for totals of all files | ||
failOnerror: false // fail task when error threshold crossed | ||
} | ||
folder: dirs.reports + '/filesize', .. defaults to './' | ||
short: true, //defaults to false - will output full info in the console | ||
xml: true, // default is false | ||
json: false, //default is false | ||
filename: 'myoutput' // defaults to 'filesize-[target].[ext]', | ||
thresholds: [102400,204800] // [warning, error] thresholds for individual files | ||
totalThresholds: [524288,1048576] // [warning, error] for totals of all files | ||
failOnerror: false // fail task when error threshold crossed | ||
}, | ||
@@ -53,2 +51,2 @@ css: { | ||
Released under the [MIT License](http://opensource.org/licenses/MIT) | ||
Released under the [MIT License](http://opensource.org/licenses/MIT) |
@@ -6,97 +6,130 @@ var fs = require('fs'); | ||
var MultiReporter = require('violation-reporter')(grunt); | ||
var XMLReporter = require('violation-reporter/tasks/XML')(grunt); | ||
var pmdReporter = require('violation-reporter/tasks/PmdXML')(grunt, XMLReporter); | ||
var checkstyleReporter = require('violation-reporter/tasks/CheckstyleXML')(grunt, XMLReporter); | ||
var jenkinsReporter = require('./JenkinsXML')(grunt, XMLReporter); | ||
var consoleReporter = require('./Console')(grunt); | ||
var defaults = { | ||
failOnError: false, | ||
thresholds: [102400,204800], | ||
totalThresholds: [524288,1048576] | ||
fail: true, | ||
failOn: 'error', | ||
thresholds: { | ||
error: 0.5, | ||
warning: 0.75 | ||
}, | ||
filesizes: { | ||
size: 102400, | ||
total: 524288 | ||
} | ||
}; | ||
function formatSize(num) { | ||
return num.toFixed(2).toString(); | ||
function getReporter(files, options) { | ||
var reporter = new MultiReporter(files, options); | ||
reporter.addReporter(consoleReporter); | ||
if (options.pmdXML) { | ||
reporter.addReporter(pmdReporter); | ||
} | ||
if (options.checkstyleXML) { | ||
reporter.addReporter(checkstyleReporter); | ||
} | ||
if (options.jenkinsXML) { | ||
reporter.addReporter(jenkinsReporter); | ||
} | ||
return reporter; | ||
} | ||
grunt.registerMultiTask("filesize", "", function() { | ||
grunt.registerMultiTask('filesize', 'Analyzes filesizes', function() { | ||
var done = this.async(); | ||
var files = this.filesSrc || grunt.file.expand(this.file.src); | ||
var excluded = this.data.exclude; | ||
var options = this.options(defaults); | ||
var errors = 0; | ||
var total = 0; | ||
var files = this.filesSrc.filter(function (file) { | ||
return grunt.file.exists(file); | ||
}).map(function(file){ | ||
var stat = fs.statSync(file); | ||
if(stat.size > options.thresholds[1]) { | ||
errors += 1; | ||
} | ||
total += stat.size; | ||
return { | ||
fullpath: path.resolve(file), | ||
filename: file, | ||
basename: path.basename(file), | ||
size: stat.size, | ||
kb: stat.size / 1024, | ||
mb: stat.size / 1024 / 1024 | ||
}; | ||
}); | ||
var reporter = getReporter(files, options); | ||
if(total > options.totalThresholds[1]) { | ||
errors += 1; | ||
// Exclude any unwanted files from 'files' array | ||
if (excluded) { | ||
grunt.file.expand(excluded).forEach(function(ex){ | ||
files.splice(files.indexOf(ex), 1); | ||
}); | ||
} | ||
function getColor(size, thresholds) { | ||
thresholds = thresholds || options.thresholds; | ||
if(size > thresholds[1]) { | ||
return 'red'; | ||
} else if(size > thresholds[0]) { | ||
return 'yellow'; | ||
var results = []; | ||
function getSeverity(ratio) { | ||
if (ratio < options.thresholds.error) { | ||
return 'error'; | ||
} else if (ratio < options.thresholds.warning) { | ||
return 'warning'; | ||
} | ||
return 'green'; | ||
return 'info'; | ||
} | ||
if (options.console !== false) { | ||
var widths = [75,10,10,10]; | ||
var sep = ' '; | ||
function setFailures(severity) { | ||
if (options.failOn === 'error' && severity !== 'error') { | ||
return; | ||
} | ||
grunt.fail.errorcount++; | ||
} | ||
grunt.log.writeln('=========================='); | ||
grunt.log.writeln('Filesize analysis: '+this.target); | ||
grunt.log.writeln('=========================='); | ||
grunt.log.writeln(grunt.log.table(widths, ['File', sep + 'B', sep + 'kB', sep + 'MB'])); | ||
grunt.log.writeln(); | ||
function processAnalysis(analysis, file) { | ||
if(!options.short) { | ||
files.forEach(function (file) { | ||
grunt.log.writeln(grunt.log.table(widths, [file.filename, sep + file.size.toString(), sep + formatSize(file.kb), sep + formatSize(file.mb)]) [getColor(file.size)]); | ||
}); | ||
grunt.log.writeln(); | ||
} | ||
var violations = []; | ||
var ratio; | ||
var severity; | ||
var metrics = analysis.metrics; | ||
grunt.log.writeln(grunt.log.table(widths,['Total',sep+total.toString(),sep+formatSize(total / 1024),sep+formatSize(total / 1024 / 1024)]) [getColor(total,options.totalThresholds)]); | ||
} | ||
Object.keys(metrics).forEach(function(key) { | ||
options.filename = options.folder + '/' + (options.filename || 'filesize-' + this.target); | ||
ratio = options.filesizes[key] / metrics[key]; | ||
if(options.folder) { | ||
grunt.file.mkdir(options.folder); | ||
} | ||
severity = getSeverity(ratio); | ||
if (options.xml) { | ||
var str = '<filesizes type="'+this.target+'">'; | ||
violations.push({ | ||
filepath: file, | ||
line: 0, | ||
column: 0, | ||
name: key, | ||
rule: key, | ||
severity: severity, | ||
message: 'Filesize too large', | ||
ratio: ratio, | ||
value: metrics[key] | ||
}); | ||
files.forEach(function(file) { | ||
str += '<' + file.basename + '>' + file.size + '</'+file.basename+'>'; | ||
setFailures(severity); | ||
}); | ||
str += '</filesizes>'; | ||
fs.writeFileSync(options.filename + '.xml', str); | ||
reporter.violations(file, violations); | ||
results.push(file); | ||
} | ||
if (options.json) { | ||
fs.writeFileSync(options.filename + '.json',JSON.stringify(files)); | ||
} | ||
reporter.start(); | ||
if(options.failOnError) { | ||
grunt.fail.warn('Error threshold broken'); | ||
} | ||
var total = 0; | ||
files.forEach(function(file) { | ||
var stat = fs.statSync(file); | ||
var analysis = { | ||
metrics: { | ||
size: stat.size | ||
} | ||
}; | ||
processAnalysis(analysis, file); | ||
total += stat.size; | ||
}); | ||
processAnalysis({ | ||
metrics : { | ||
total: total | ||
} | ||
}, 'Total'); | ||
reporter.finish(); | ||
done(options.fail === false || this.errorCount === 0); | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52753
19
160
3
51
2
1