From 62cbe1884bc1d6661303ff756281a50bd050643a Mon Sep 17 00:00:00 2001 From: James Kyle Date: Sat, 5 Sep 2015 23:06:47 -0700 Subject: [PATCH] Refactor Gulpfile --- app/templates/gulpfile.babel.js | 145 +++++++++++++++++++------------- 1 file changed, 87 insertions(+), 58 deletions(-) diff --git a/app/templates/gulpfile.babel.js b/app/templates/gulpfile.babel.js index 2426b78..7f4aba9 100644 --- a/app/templates/gulpfile.babel.js +++ b/app/templates/gulpfile.babel.js @@ -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'; @@ -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, @@ -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); @@ -101,7 +104,9 @@ 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. @@ -109,31 +114,36 @@ function getBundler() { 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()) @@ -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. @@ -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']);