Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including gulp-nice-package with all npm packages installed. Try it out:

var gulpNicePackage = require("gulp-nice-package")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

gulp-nice-package v1.1.0

Opinionated package.json validator

gulp-nice-package NPM version Build Status

Opinionated package.json validator. Uses package.json-validator under the covers

Install

npm install gulp-nice-package --save-dev

Usage

// gulpfile.js
var gulp = require('gulp');
var validate = require('gulp-nice-package');

gulp.task('validate-json', function () {
  return gulp.src('package.json')
    .pipe(validate());
});

Options

Any options defined are simply passed along to package.json-validator

// gulpfile.js
var gulp = require('gulp');
var validate = require('gulp-nice-package');

gulp.task('validate-json', function () {
  return gulp.src('package.json')
    .pipe(validate('npm', {
      warnings: false,
      recommendations: false
    }));
});

Failing your build

The below will both print errors to the console and exit the gulp process with an appropriate error code for properly failing builds.

var gulp = require('gulp'),
  validate = require('gulp-nice-package'),
  mapstream = require('map-stream');

process.on('exit', function () {
  process.nextTick(function () {
    process.exit(1);
  });
});

gulp.task('validate-json', function () {
  var isValid = true;
  return gulp.src('package.json')
    .pipe(validate())
    .pipe(mapstream(function (file, cb) {
      isValid = file.nicePackage.valid;
      cb(null, file);
    }))
    .on('end', function() {
      if (!isValid) {
        process.emit('exit');
      }
    });
});

I know, it seems a bit terse. The reasoning is, you may want to pipe other transforms onto the stream and, if an 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.

License

MIT © Chris Montgomery

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free