'use strict';

const gulp = require('gulp');
const loadPlugins = require('gulp-load-plugins');
const isparta = require('isparta');

const Instrumenter = isparta.Instrumenter;
const mochaGlobals = require('./test-globals');

// Load all of our Gulp plugins
const $ = loadPlugins();

// Lint a set of files
function lint() {
  return gulp.src(['packages/**/*.js', '!packages/*/node_modules/**/*'])
    .pipe($.eslint())
    .pipe($.eslint.format())
    .pipe($.eslint.failAfterError());
}

function _mocha() {
  return gulp.src(['test/setup.js', 'test/unit/**/*.js'], {read: false})
    .pipe($.mocha({
      reporter: 'dot',
      globals: Object.keys(mochaGlobals.globals),
      ignoreLeaks: false
    }));
}

function test() {
  return _mocha();
}

function coverage(done) {
  gulp.src(['{server,example,lib}/**/*.js'])
    .pipe($.istanbul({
      instrumenter: Instrumenter,
      includeUntested: true
    }))
    .pipe($.istanbul.hookRequire())
    .on('finish', () => {
      return test()
        .pipe($.istanbul.writeReports())
        .on('end', done);
    });
}

const watchFiles = ['{server/example/lib/test}/**/*', 'package.json', '**/.eslintrc'];

// Run the headless unit tests as you make changes.
function watch() {
  $.watch(watchFiles, ['test']);
}

// Lint
gulp.task('lint', lint);

// Lint and run our tests
gulp.task('test', ['lint'], test);

// Set up coverage and run tests
gulp.task('coverage', ['lint'], coverage);

// Run the headless unit tests as you make changes.
gulp.task('watch', watch);

// An alias of test
gulp.task('default', ['test']);

@@ -271,24 +271,123 @@
 Plugins();%0A%0A
+const allJsFiles = 'packages/**/*.js';%0Aconst ignoreNodeModules = '!packages/*/node_modules/**/*';%0A%0A
 // Lint a se
@@ -438,59 +438,37 @@
 rc(%5B
-'packages/**/*.js', '!packages/*/node_modules/**/*'
+allJsFiles, ignoreNodeModules
 %5D)%0A 
@@ -602,25 +602,59 @@
 c(%5B'
-test/setup.js', '
+packages/api-pls/test/setup.js', 'packages/api-pls/
 test
@@ -896,40 +896,18 @@
 src(
-%5B'%7Bserver,example,lib%7D/**/*.js'%5D
+allJsFiles
 )%0A  
@@ -1181,40 +1181,18 @@
  = %5B
-'%7Bserver/example/lib/test%7D/**/*'
+allJsFiles
 , 'p
@@ -1219,16 +1219,35 @@
 slintrc'
+, ignoreNodeModules
 %5D;%0A%0A// R

Fix unit test command