WFTDA/bouttime

View on GitHub
gulpfile.js

Summary

Maintainability
A
0 mins
Test Coverage
'use strict';
var gulp = require('gulp');
var del = require('del');
var spawn = require('child_process').spawn;

// Load plugins
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var mainBowerFiles = require('main-bower-files');

var sourceFile = './app/scripts/app.cjsx';
var destFolder = './dist/scripts';
var destFileName = 'app.js';
var server;

// Styles
gulp.task('styles', ['sass']);

gulp.task('sass', function() {
  return gulp.src(['app/styles/**/*.scss', 'app/styles/**/*.css'])
    .pipe($.sass({
      errLogToConsole: true,
      includePaths: [
        'app/bower_components/bootstrap-sass/assets/stylesheets',
        'app/bower_components/jquery-minicolors'
      ]
    }))
    .pipe($.autoprefixer('last 1 version'))
    .pipe(gulp.dest('dist/styles'))
    .pipe($.size());
});

var bundler = browserify({
  entries: [sourceFile],
  debug: true,
  verbose: true,
  insertGlobals: true,
  cache: {},
  packageCache: {},
  fullPaths: true,
  extensions: ['.coffee', '.cjsx']
});

bundler.on('update', rebundle);
bundler.on('log', $.util.log);

function rebundle() {
  return bundler.bundle()
    // log errors if they happen
    .on('error', $.util.log.bind($.util, 'Browserify Error'))
    .pipe(source(destFileName))
    .pipe(gulp.dest(destFolder));
}

function watchBundle() {
  return watchify(bundler).bundle()
    // log errors if they happen
    .on('error', $.util.log.bind($.util, 'Browserify Error'))
    .pipe(source(destFileName))
    .pipe(gulp.dest(destFolder));
}

gulp.task('coffee', function() {
  return gulp.src('./app/**/*.coffee')
    .pipe($.coffee({bare: true}).on('error', $.util.log))
    .pipe(gulp.dest('./dist/'));
});
// Scripts
gulp.task('scripts', ['coffee'], rebundle);

// HTML
gulp.task('html', function() {
  return gulp.src('app/*.html')
    .pipe($.useref())
    .pipe(gulp.dest('dist'))
    .pipe($.size());
});

// Images
gulp.task('images', function() {
  return gulp.src('app/images/**/*')
    .pipe(gulp.dest('dist/images'))
    .pipe($.size());
});

// Fonts
gulp.task('fonts', function() {
  return gulp.src(mainBowerFiles({
    filter: '**/*.{eot,svg,ttf,woff,woff2}'
  }).concat('app/fonts/**/*'))
    .pipe(gulp.dest('dist/fonts'));
});

// Clean
gulp.task('clean', function(done) {
  $.cache.clearAll();
  del.sync(['dist/*'], done);
});

// Bundle
gulp.task('bundle', ['styles', 'scripts', 'bower'], function() {
  return gulp.src('./app/*.html')
    .pipe($.useref.assets())
    .pipe($.useref.assets().restore())
    .pipe($.useref())
    .pipe(gulp.dest('dist'));
});

// Bower helper
gulp.task('bower', function() {
  return gulp.src([
    'app/bower_components/**/*.js',
    'app/bower_components/**/*.map',
    'app/bower_components/**/*.css'
  ], {
    base: 'app/bower_components'
  })
    .pipe(gulp.dest('dist/bower_components/'));
});

gulp.task('json', function() {
  return gulp.src('app/scripts/json/**/*.json', {
      base: 'app/scripts'
    })
    .pipe(gulp.dest('dist/scripts/'));
});

// Robots.txt and favicon.ico
gulp.task('extras', function() {
  return gulp.src(['app/*.txt', 'app/*.ico'])
    .pipe(gulp.dest('dist/'))
    .pipe($.size());
});

// Uglify
gulp.task('uglify', ['scripts'], function() {
  return gulp.src('dist/scripts/app.js')
    .pipe($.uglify({mangle: false}))
    .pipe($.stripDebug())
    .pipe(gulp.dest('dist/scripts'));
});

// Build
gulp.task('build', ['html', 'bundle', 'images', 'fonts', 'extras']);

// Package
gulp.task('package', ['clean', 'build', 'uglify']);

// Watch
gulp.task('watch', ['build'], function() {
  var startServer = gulp.start.bind(this, 'server');
  startServer();

  gulp.watch('app/*.html', ['html'], startServer);

  gulp.watch(['app/styles/**/*.scss', 'app/styles/**/*.css'], ['styles'], startServer);

  gulp.watch(['app/**/*.coffee'], ['coffee'], startServer);

  gulp.watch('app/images/**/*', ['images'], startServer);

  gulp.watch('app/fonts/**/*', ['fonts'], startServer);

  gulp.watch(['app/*.txt', 'app/*.ico'], ['extras'], startServer);

  watchBundle();
});

// Dev server stop and restart
gulp.task('server', function(done) {
  if (server) {
    server.kill();
  }
  server = spawn('node', ['./bin/bouttime-server'], {
    stdio: 'inherit'
  });
  server.on('close', function(code) {
    if (code === 8) {
      gulp.log('Error detected, waiting for changes...');
    }
  });
  done();
});

process.on('exit', function() {
  if (server) {
    server.kill();
  }
});

// Default task
gulp.task('default', ['clean', 'build']);