diff --git a/.gitignore b/.gitignore index e22121a..fc848a8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.iml coverage/ +test-output.xml diff --git a/README.md b/README.md index 567477a..99ebaf0 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ file.csslint.opt = {}; // The options you passed to CSSLint ## Using reporters -Several reporters come built-in to css-lint. To use one of these reporters, pass the name to `csslint.reporter`. +Several reporters come built-in to css-lint. To use one of these reporters, pass the name to `csslint.reporter`, along with an optional file path. If the file path is included, the report will be written to this location, instead of printed in the console. For a list of all reporters supported by `csslint`, see the [csslint wiki](https://github.com/CSSLint/csslint/wiki/Command-line-interface#--format). @@ -79,6 +79,15 @@ gulp.task('lint', function() { .pipe(csslint.reporter('junit-xml')); ``` +If you want to print the report to disk instead of output to the console, supply the a second optional file path where the file will be written. + +```js +gulp.task('lint', function() { + gulp.files('lib/*.css') + .pipe(csslint()) + .pipe(csslint.reporter('junit-xml', 'csslint-junit-report.xml')); +``` + ### Custom reporters Custom reporter functions can be passed as `csslint.reporter(reporterFunc)`. The reporter function will be called for each linted file and passed the file object as described above. diff --git a/index.js b/index.js index 0827449..c26299e 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict'; +var fs = require('fs'); var gutil = require('gulp-util'); var through = require('through2'); var csslint = require('csslint').CSSLint; @@ -74,7 +75,7 @@ var cssLintPlugin = function(options) { }); }; -cssLintPlugin.reporter = function(customReporter) { +cssLintPlugin.reporter = function(customReporter, filePath) { var reporter = csslint.getFormatter('text'); var builtInReporter = true; var output; @@ -117,7 +118,12 @@ cssLintPlugin.reporter = function(customReporter) { if (builtInReporter) { output += reporter.endFormat(); - gutil.log(output); + if (filePath) { + return fs.writeFile(filePath, output, cb); + } + else { + gutil.log(output); + } } return cb(); diff --git a/test/main.js b/test/main.js index d927726..5e08c18 100644 --- a/test/main.js +++ b/test/main.js @@ -251,7 +251,7 @@ describe('gulp-csslint', function() { stream.end(); }); - it('should support built-in CSSLint formatters', sinon.test(function(done) { + it('should support built-in CSSLint formatters', function(done) { var a = 0; var file = getFile('fixtures/usingImportant.css'); @@ -283,6 +283,45 @@ describe('gulp-csslint', function() { lintStream.write(file); lintStream.end(); - })); + }); + + it('should write built-in report to disk', function(done) { + var a = 0; + + var file = getFile('fixtures/usingImportant.css'); + var expected = getContents('expected/checkstyle-xml.xml'); + + var lintStream = cssLintPlugin(); + var reporterStream = cssLintPlugin.reporter('checkstyle-xml', 'test-output.xml'); + + sinon.stub(gutil, 'log'); + + reporterStream.on('data', function() { + ++a; + }); + lintStream.on('data', function(file) { + reporterStream.write(file); + }); + lintStream.once('end', function() { + reporterStream.end(); + }); + + reporterStream.once('end', function() { + a.should.equal(1); + sinon.assert.notCalled(gutil.log); + + gutil.log.restore(); + + fs.readFile('test-output.xml', function(err, content) { + (err === null).should.be.true; + + content.toString().should.equal(expected); + done(); + }); + }); + + lintStream.write(file); + lintStream.end(); + }); }); });