gulpfile.js
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const del = require('del');
const path = require('path');
const mkdirp = require('mkdirp');
const isparta = require('isparta');
const manifest = require('./package.json');
const config = manifest.nodeBoilerplateOptions;
const mainFile = manifest.main;
const destinationFolder = path.dirname(mainFile);
// Remove the built files
gulp.task('clean', function(cb) {
del([destinationFolder], cb);
});
// Send a notification when JSHint fails,
// so that you know your changes didn't build
function jshintNotify(file) {
if (!file.jshint) { return; }
return file.jshint.success ? false : 'JSHint failed';
}
function jscsNotify(file) {
if (!file.jscs) { return; }
return file.jscs.success ? false : 'JSCS failed';
}
// Lint our source code
gulp.task('lint-src', function() {
return gulp.src(['src/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(jshintNotify))
.pipe($.jscs())
.pipe($.notify(jscsNotify))
.pipe($.jshint.reporter('fail'));
});
// Lint our test code
gulp.task('lint-test', function() {
return gulp.src(['test/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(jshintNotify))
.pipe($.jscs())
.pipe($.notify(jscsNotify))
.pipe($.jshint.reporter('fail'));
});
// Build two versions of the library
gulp.task('build', ['lint-src', 'clean'], function() {
// Create our output directory
mkdirp.sync(destinationFolder);
return gulp.src('src/**/*.js')
.pipe($.plumber())
.pipe($.babel({ blacklist: ['useStrict'] }))
.pipe(gulp.dest(destinationFolder));
});
function test() {
return gulp.src(['test/setup/node.js', 'test/unit/**/*.js'], {read: false})
.pipe($.plumber())
.pipe($.mocha({reporter: 'dot', globals: config.mochaGlobals}));
}
// Make babel preprocess the scripts the user tries to import from here on.
require('babel/register');
gulp.task('coverage', function(done) {
require('babel/register');
gulp.src(['src/*.js'])
.pipe($.plumber())
.pipe($.istanbul({ instrumenter: isparta.Instrumenter }))
.pipe($.istanbul.hookRequire())
.on('finish', function() {
return test()
.pipe($.istanbul.writeReports())
.on('end', done);
});
});
// Lint and run our tests
gulp.task('test', ['lint-src', 'lint-test'], test);
// Run the headless unit tests as you make changes.
gulp.task('watch', ['test'], function() {
gulp.watch(['src/**/*', 'test/**/*', '.jshintrc', 'test/.jshintrc'], ['test']);
});
// An alias of test
gulp.task('default', ['test']);