Skip to content

Commit

Permalink
Refactor Gulpfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Sep 6, 2015
1 parent fecaaaf commit 62cbe18
Showing 1 changed file with 87 additions and 58 deletions.
145 changes: 87 additions & 58 deletions app/templates/gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import gulp from 'gulp');
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
import del from 'del';
import glob from 'glob';
Expand All @@ -23,43 +23,46 @@ const mainFile = manifest.main;
const destinationFolder = path.dirname(mainFile);
const exportFileName = path.basename(mainFile, path.extname(mainFile));

// Remove the built files
gulp.task('clean', cb => {
del([destinationFolder], cb);
});
// Remove a directory
function _clean(dir, done) {
del([dir], done);
}

// Remove our temporary files
gulp.task('clean-tmp', cb => {
del(['tmp'], cb);
});
function cleanDist(done) {
_clean(destinationFolder, done)
}

function cleanTmp() {
_clean('tmp', done)
}

// Send a notification when JSCS fails,
// so that you know your changes didn't build
function jscsNotify(file) {
function _jscsNotify(file) {
if (!file.jscs) { return; }
return file.jscs.success ? false : 'JSCS failed';
}

function createLintTask(taskName, files) {
gulp.task(taskName, () => {
return gulp.src(files)
.pipe($.plumber())
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError())
.pipe($.jscs())
.pipe($.notify(jscsNotify));
});
// Lint a set of files
function lint(files) {
return gulp.src(files)
.pipe($.plumber())
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError())
.pipe($.jscs())
.pipe($.notify(_jscsNotify));
}

// Lint our source code
createLintTask('lint-src', ['src/**/*.js']);
function lintSrc() {
return lint('src/**/*.js');
}

// Lint our test code
createLintTask('lint-test', ['test/**/*.js']);
function lintTest() {
return lint('test/**/*.js');
}

// Build two versions of the library
gulp.task('build', ['lint-src', 'clean'], done => {
function build(done) {
esperanto.bundle({
base: 'src',
entry: config.entryFileName,
Expand All @@ -86,9 +89,9 @@ gulp.task('build', ['lint-src', 'clean'], done => {
.on('end', done);
})
.catch(done);
});
}

function bundle(bundler) {
function _runBrowserifyBundle(bundler) {
return bundler.bundle()
.on('error', err => {
console.log(err.message);
Expand All @@ -101,39 +104,46 @@ function bundle(bundler) {
.pipe($.livereload());
}

function getBundler() {
// Build the unit test suite for running tests
// in the browser
function _browserifyBundle() {
// Our browserify bundle is made up of our unit tests, which
// should individually load up pieces of our application.
// We also include the browserify setup file.
const testFiles = glob.sync('./test/unit/**/*.js');
const allFiles = ['./test/setup/browserify.js'].concat(testFiles);

// Create our bundler, passing in the arguments required for watchify
const bundler = browserify(allFiles, watchify.args);
let bundler = browserify(allFiles, watchify.args);

// Watch the bundler, and re-bundle it whenever files change
bundler = watchify(bundler);
bundler.on('update', () => bundle(bundler));
bundler.on('update', () => _runBrowserifyBundle(bundler));

// Set up Babelify so that ES6 works in the tests
bundler.transform(babelify.configure({
sourceMapRelative: __dirname + '/src'
}));

return bundler;
};

// Build the unit test suite for running tests
// in the browser
gulp.task('browserify', () => bundle(getBundler()));
return _runBrowserifyBundle(bundler);
}

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

gulp.task('coverage', ['lint-src', 'lint-test'], done => {
function _registerBabel() {
require('babel-core/register');
}

function test() {
_registerBabel();
return _mocha();
}

function coverage(done) {
_registerBabel();
gulp.src(['src/**/*.js'])
.pipe($.istanbul({ instrumenter: isparta.Instrumenter }))
.pipe($.istanbul.hookRequire())
Expand All @@ -142,19 +152,7 @@ gulp.task('coverage', ['lint-src', 'lint-test'], done => {
.pipe($.istanbul.writeReports())
.on('end', done);
});
});

// Lint and run our tests
gulp.task('test', ['lint-src', 'lint-test'], () => {
require('babel-core/register');
return test();
});

// Ensure that linting occurs before browserify runs. This prevents
// the build from breaking due to poorly formatted code.
gulp.task('build-in-sequence', callback => {
runSequence(['lint-src', 'lint-test'], 'browserify', callback);
});
}

// These are JS files that should be watched by Gulp. When running tests in the browser,
// watchify is used instead, so these aren't included.
Expand All @@ -163,16 +161,47 @@ const jsWatchFiles = ['src/**/*', 'test/**/*'];
const otherWatchFiles = ['package.json', '**/.eslintrc', '.jscsrc'];

// Run the headless unit tests as you make changes.
gulp.task('watch', () => {
function watch() {
const watchFiles = jsWatchFiles.concat(otherWatchFiles);
gulp.watch(watchFiles, ['test']);
});
}

function testBrowser() {
// Ensure that linting occurs before browserify runs. This prevents
// the build from breaking due to poorly formatted code.
runSequence(['lint-src', 'lint-test'], () => {
_browserifyBundle();
$.livereload.listen({port: 35729, host: 'localhost', start: true});
gulp.watch(otherWatchFiles, ['lint-src', 'lint-test']);
});
}

// Remove the built files
gulp.task('clean', cleanDist);

// Remove our temporary files
gulp.task('clean-tmp', cleanTmp);

// Lint our source code
gulp.task('lint-src', lintSrc);

// Lint our test code
gulp.task('lint-test', lintTest);

// Build two versions of the library
gulp.task('build', ['lint-src', 'clean'], build);

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

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

// Set up a livereload environment for our spec runner
gulp.task('test-browser', ['build-in-sequence'], () => {
$.livereload.listen({port: 35729, host: 'localhost', start: true});
return gulp.watch(otherWatchFiles, ['build-in-sequence']);
});
gulp.task('test-browser', testBrowser);

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

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

0 comments on commit 62cbe18

Please sign in to comment.