diff --git a/index.js b/index.js index 966f39b..ba146bf 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ var gutil = require('gulp-util'), PJV = require('package-json-validator').PJV, through = require('through2'); +var PLUGIN_NAME = require('./package').name; + function printErrors(results) { if (results) { @@ -41,11 +43,11 @@ function printErrors(results) { } } else { - throw new gutil.PluginError('gulp-nice-package', 'Failed to get results from validator'); + throw new gutil.PluginError(PLUGIN_NAME, 'Failed to get results from validator'); } } -module.exports = function (spec, options) { +var nicePackagePlugin = function (spec, options) { function validate(file, enc, cb) { @@ -55,7 +57,7 @@ module.exports = function (spec, options) { } if (file.isStream()) { - this.emit('error', new gutil.PluginError('gulp-nice-package', 'Streaming not supported')); + this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported')); return cb(); } @@ -70,4 +72,28 @@ module.exports = function (spec, options) { } return through.obj(validate); -}; \ No newline at end of file +}; + +nicePackagePlugin.failOnError = function () { + return through.obj(function (file, enc, cb) { + var error = null; + + function size(arr) { + return arr ? arr.length : 0; + } + + if (file.nicePackage.valid === false) { + error = new gutil.PluginError( + PLUGIN_NAME, + 'Failed with ' + + size(file.nicePackage.errors) + ' error(s), ' + + size(file.nicePackage.warnings) + ' warning(s), ' + + size(file.nicePackage.recommendations) + ' recommendation(s)' + ); + } + + return cb(error, file); + }); +}; + +module.exports = nicePackagePlugin; diff --git a/readme.md b/readme.md index 35c9d1f..b33576b 100644 --- a/readme.md +++ b/readme.md @@ -78,6 +78,21 @@ I know, it seems a bit terse. The reasoning is, you may want to pipe other trans error is thrown, the pipe will cease and you have no way to continue. A valid use case may be to actually fix the the package.json file programmatically if it's invalid, e.g. with [gulp-shrinkwrap](https://github.com/chmontgomery/gulp-shrinkwrap). +If you have no need to further process the stream after validation, you can pipe the stream to `validate.failOnError()` +to fail the build in the event one or more errors are raised during validation. + +```js +// gulpfile.js +var gulp = require('gulp'); +var validate = require('gulp-nice-package'); + +gulp.task('validate-json', function () { + return gulp.src('package.json') + .pipe(validate()) + .pipe(validate.failOnError()); +}); +``` + ## License [MIT](http://opensource.org/licenses/MIT) © [Chris Montgomery](http://www.chrismontgomery.info/) diff --git a/test/validation.js b/test/validation.js index f44e5e9..a746092 100644 --- a/test/validation.js +++ b/test/validation.js @@ -3,6 +3,20 @@ var gutil = require('gulp-util'), validator = require('./../index.js'), assert = require('assert'); +function createPackageFile(path) { + return new gutil.File({ + contents: new Buffer(JSON.stringify(require(path))) + }); +} + +function createInvalidPackageFile() { + return createPackageFile('./fixtures/package-with-errors.json'); +} + +function createValidPackageFile() { + return createPackageFile('./fixtures/package-valid.json'); +} + it('should have invalid json file', function (cb) { var stream = validator(); @@ -12,9 +26,7 @@ it('should have invalid json file', function (cb) { cb(); }); - stream.write(new gutil.File({ - contents: new Buffer(JSON.stringify(require('./fixtures/package-with-errors.json'))) - })); + stream.write(createInvalidPackageFile()); }); it('should have valid json file', function (cb) { @@ -25,7 +37,44 @@ it('should have valid json file', function (cb) { cb(); }); - stream.write(new gutil.File({ - contents: new Buffer(JSON.stringify(require('./fixtures/package-valid.json'))) - })); + stream.write(createValidPackageFile()); +}); + +describe('failOnError', function () { + it('should report an error when the json file is invalid', function (cb) { + var errorReported = false; + var stream = validator() + .on('end', function () { + assert.ok(errorReported); + cb(); + }); + + stream + .pipe(validator.failOnError()) + .on('error', function (error) { + errorReported = true; + assert.strictEqual(error.message, 'Failed with 1 error(s), 3 warning(s), 2 recommendation(s)'); + }); + + stream.write(createInvalidPackageFile()); + stream.end(); + }); + + it('should not report an error when the json file is valid', function (cb) { + var errorReported = false; + var stream = validator() + .on('end', function () { + assert.ok(!errorReported); + cb(); + }); + + stream + .pipe(validator.failOnError()) + .on('error', function () { + errorReported = true; + }); + + stream.write(createValidPackageFile()); + stream.end(); + }); });