Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.iml

coverage/
test-output.xml
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var fs = require('fs');
var gutil = require('gulp-util');
var through = require('through2');
var csslint = require('csslint').CSSLint;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
43 changes: 41 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from me trying to mock out console... Does nothing that I can tell, as we manually restore the stub

var a = 0;

var file = getFile('fixtures/usingImportant.css');
Expand Down Expand Up @@ -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();
});
});
});