Skip to content

Commit

Permalink
Merge pull request #22 from ilyakam/release/0.4.0
Browse files Browse the repository at this point in the history
[0.4.0] - 2016-10-09
  • Loading branch information
ilyakam authored Oct 9, 2016
2 parents d147b6b + 85bb0be commit 1a77263
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.4.0] - 2016-10-09
### Added
- Allow use of custom and external reporters

## [0.3.0] - 2016-06-13
### Added
- Allow use of `extend` in `pug-lint`
Expand Down
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,54 @@ gulp.task('lint:template', function () {
.pipe(pugLinter.reporter('fail'))
})
```

Specify external modules as reporters using either the module's
constructor or the module's name:

```js
// gulpfile.js
var gulp = require('gulp')
var pugLinter = require('gulp-pug-linter')
var myPugLintReporter = require('my-pug-lint-reporter')

gulp.task('lint:template', function () {
return gulp
.src('./**/*.pug')
.pipe(pugLinter())
.pipe(pugLinter.reporter(myPugLintReporter))
})
```

_- OR -_

```js
// gulpfile.js
var gulp = require('gulp')
var pugLinter = require('gulp-pug-linter')

gulp.task('lint:template', function () {
return gulp
.src('./**/*.pug')
.pipe(pugLinter())
.pipe(pugLinter.reporter('my-pug-lint-reporter'))
})
```

Specify your own custom reporter:

```js
// gulpfile.js
var gulp = require('gulp')
var pugLinter = require('gulp-pug-linter')

var myReporter = function (errors) {
if (errors.length) { console.error("It broke!"); }
};

gulp.task('lint:template', function () {
return gulp
.src('./**/*.pug')
.pipe(pugLinter())
.pipe(pugLinter.reporter(myReporter))
})
```
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ function gulpPugLinter () {
* @returns {Array} List of error messages
*/
function checkFile (file) {
var errors = linter.checkFile(file.path)

return errors.map(function (error) {
return error.message
})
return linter.checkFile(file.path)
}

linter.configure(config)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"coveralls": "^2.11.9",
"gulp": "^3.9.1",
"istanbul": "^0.4.2",
"mocha": "^2.5.0",
"mockery": "^1.7.0",
"mocha": "^3.0.1",
"mockery": "^2.0.0",
"sinon": "^1.17.4",
"standard": "^7.0.0"
"standard": "^8.0.0"
},
"engines": {
"node": "5.*"
Expand All @@ -43,5 +43,5 @@
"pretest": "standard",
"test": "npm run-script coveralls"
},
"version": "0.3.0"
"version": "0.4.0"
}
83 changes: 73 additions & 10 deletions reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,79 @@ var throughObj = require('through2').obj

const PLUGIN_NAME = 'gulp-pug-linter'

module.exports = function (hasToFail) {
/**
* @name defaultReporter
* @description Logs all errors via `gulp-util`
* @param {Array} errors Pug lint error objects with `message`
*/
var defaultReporter = function (errors) {
var allErrors

if (errors.length) {
allErrors = errors.map(function (error) {
return error.message
}).join('\n\n')

gutil.log(allErrors)
}
}

/**
* @name failReporter
* @description Emits a plugin error including all Pug lint errors at once
* @param {Array} errors Pug lint error objects with `message`
*/
var failReporter = function (errors) {
var allErrors

if (errors.length) {
allErrors = errors.map(function (error) {
return error.message
}).join('\n\n')

this.emit('error', new gutil.PluginError(PLUGIN_NAME, allErrors))
}
}

/**
* @name loadReporter
* @description Returns an error reporter
* @param {*} [type] 'fail' string to load a reporter that fails on error,
* any function or string to load a custom module reporter,
* or empty to load the default reporter.
* @returns {Function} Error reporter that accepts an array of errors
*/
var loadReporter = function (type) {
var reporter

if (type == null) {
return defaultReporter
}

if (type === 'fail') {
return failReporter
}

if (typeof type === 'function') {
reporter = type
}

if (typeof type === 'string') {
try {
reporter = require(type)
} catch (error) {}
}

if (typeof reporter !== 'function') {
throw new gutil.PluginError(PLUGIN_NAME, type + ' is not a valid reporter')
}

return reporter
}

module.exports = function (type) {
var errors = []
var reporter = loadReporter(type)

return throughObj(function (file, encoding, callback) {
if (file.pugLinter && file.pugLinter.errors.length) {
Expand All @@ -13,15 +84,7 @@ module.exports = function (hasToFail) {

return callback(null, file)
}, function (callback) {
var allErrors

if (errors.length) {
allErrors = errors.join('\n\n')

hasToFail
? this.emit('error', new gutil.PluginError(PLUGIN_NAME, allErrors))
: gutil.log(allErrors)
}
reporter.call(this, errors)

return callback()
})
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('gulp-pug-linter', function () {
it('should stream a file that contains errors', function () {
stream.on('data', function (processedFile) {
expect(processedFile.pugLinter.errors)
.to.contain('some error')
.to.contain({message: 'some error'})
})
})
})
Expand Down
130 changes: 126 additions & 4 deletions test/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('#reporter()', function () {
path: 'path.pug'
})

streamFile.pugLinter = {errors: ['some error']}
streamFile.pugLinter = {errors: [{message: 'some error'}]}

it('should print the error', function () {
it('should print the error for default reporter', function () {
gulpUtil = {log: function () {}}

sinon.stub(gulpUtil)
Expand Down Expand Up @@ -49,7 +49,67 @@ describe('#reporter()', function () {
mockery.deregisterAll()
})

it('should throw an error when set to fail', function () {
it('should report errors for named reporter', function () {
var funcReporter = function (errors) {}

var spiedReporter = sinon.spy(funcReporter)

mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
})

mockery.registerMock('pug-mock-reporter', spiedReporter)

reporter = require('../reporter')

stream = reporter('pug-mock-reporter')

stream.write(streamFile)

stream.end()

expect(spiedReporter.calledWith([{message: 'some error'}]))
.to.be.ok

mockery.disable()

mockery.deregisterAll()
})

it('should report errors for function reporter', function () {
var funcReporter = function (errors) {}

var spy = sinon.spy(funcReporter)

reporter = require('../reporter')

stream = reporter(spy)

stream.write(streamFile)

stream.end()

expect(spy.calledWith([{message: 'some error'}]))
.to.be.ok
})

it('should throw an error for missing reporter', function () {
function shouldThrowError () {
reporter = require('../reporter')

stream = reporter('missingReporter')

stream.write(streamFile)

stream.end()
}

expect(shouldThrowError)
.to.throw('missingReporter is not a valid reporter')
})

it('should throw an error for fail reporter', function () {
function shouldThrowError () {
reporter = require('../reporter')

Expand All @@ -73,7 +133,69 @@ describe('#reporter()', function () {
path: 'path.pug'
})

it('should not throw an error', function () {
it('should print no errors for default reporter', function () {
gulpUtil = {log: function () {}}

sinon.stub(gulpUtil)

mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
})

mockery.registerMock('gulp-util', gulpUtil)

reporter = require('../reporter')

stream = reporter()

stream.write(streamFile)

stream.end()

expect(gulpUtil.log.called)
.to.not.be.ok

gulpUtil.log.restore()

mockery.disable()

mockery.deregisterAll()
})

it('should report empty errors for function reporter', function () {
var funcReporter = function (errors) {}

var spy = sinon.spy(funcReporter)

reporter = require('../reporter')

stream = reporter(spy)

stream.write(streamFile)

stream.end()

expect(spy.calledWith([]))
.to.be.ok
})

it('should throw an error for missing reporter', function () {
function shouldThrowError () {
reporter = require('../reporter')

stream = reporter('missingReporter')

stream.write(streamFile)

stream.end()
}

expect(shouldThrowError)
.to.throw('missingReporter is not a valid reporter')
})

it('should not throw an error for fail reporter', function () {
function shouldNotThrowError () {
reporter = require('../reporter')

Expand Down

0 comments on commit 1a77263

Please sign in to comment.