Created
July 26, 2015 23:56
-
-
Save busches/64bb190faca1e7046b61 to your computer and use it in GitHub Desktop.
Gulpfile with ES5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var args = require('yargs').argv; | |
var browserSync = require('browser-sync'); | |
var del = require('del'); | |
var path = require('path'); | |
var assign = require('lodash.assign'); | |
var config = require('./gulp.config')(); | |
var $ = require('gulp-load-plugins')({lazy: true}); | |
var port = process.env.PORT || config.defaultPort; | |
gulp.task('help', $.taskListing); | |
gulp.task('default', ['help']); | |
gulp.task('vet', function() { | |
log('Analyzing source with JSHint and JSCS'); | |
return gulp | |
.src(config.alljs) | |
.pipe($.if(args.verbose, $.print())) | |
.pipe($.jscs()) | |
.pipe($.jshint()) | |
.pipe($.jshint.reporter('jshint-stylish', {verbose: true})) | |
.pipe($.jshint.reporter('fail')); | |
}); | |
gulp.task('styles', ['clean-styles'], function() { | |
log ('Compliling Less --> CSS'); | |
return gulp | |
.src(config.less) | |
.pipe($.plumber()) | |
.pipe($.less()) | |
// .on('error', errorLogger) | |
.pipe($.autoprefixer({browsers: ['last 2 versions', '> 5%']})) | |
.pipe(gulp.dest(config.temp)); | |
}); | |
gulp.task('fonts', ['clean-fonts'], function() { | |
log('Copying fonts'); | |
return gulp.src(config.fonts) | |
.pipe($.plumber()) | |
.pipe(gulp.dest(config.build + 'fonts')); | |
}); | |
gulp.task('images', ['clean-images'], function() { | |
log('Copying and compressing the images'); | |
return gulp.src(config.images) | |
.pipe($.imagemin({optimizationLevel: 4})) | |
.pipe(gulp.dest(config.build + 'images')); | |
}); | |
gulp.task('clean', function(done) { | |
var delconfig = [].concat(config.build, config.temp); | |
log('Cleaning: ' + $.util.colors.blue(delconfig)); | |
del(delconfig, done); | |
}); | |
gulp.task('clean-fonts', function(done) { | |
clean(config.build + 'fonts/**/*', done); | |
}); | |
gulp.task('clean-images', function(done) { | |
clean(config.build + 'images/**/*', done); | |
}); | |
gulp.task('clean-styles', function(done) { | |
clean(config.temp + '**/*.css', done); | |
}); | |
gulp.task('clean-code', function(done) { | |
var files = [].concat( | |
config.temp + '**/*.js', | |
config.build + '**/*.html', | |
config.build + '**/*.js' | |
); | |
clean(files, done); | |
}); | |
gulp.task('less-watcher', function() { | |
gulp.watch([config.less], ['styles']); | |
}); | |
gulp.task('wiredep', function() { | |
log('Wire up the bower css js and our app js into the html'); | |
var options = config.getWiredepDefaultOptions(); | |
var wiredep = require('wiredep').stream; | |
return gulp | |
.src(config.index) | |
.pipe($.plumber()) | |
.pipe(wiredep(options)) | |
.pipe($.inject(gulp.src(config.js))) | |
.pipe(gulp.dest(config.client)); | |
}); | |
gulp.task('template-cache', ['clean-code'], function() { | |
log('Creating AngularJS $templatecache'); | |
return gulp | |
.src(config.htmltemplates) | |
.pipe($.plumber()) | |
.pipe($.minifyHtml({empty: true})) | |
.pipe($.angularTemplatecache( | |
config.templateCache.file, | |
config.templateCache.options | |
)) | |
.pipe(gulp.dest(config.temp)); | |
}); | |
gulp.task('inject', ['wiredep', 'styles', 'template-cache'], function() { | |
log('Wire up the app css into the html and call wiredep'); | |
return gulp | |
.src(config.index) | |
.pipe($.plumber()) | |
.pipe($.inject(gulp.src(config.css))) | |
.pipe(gulp.dest(config.client)); | |
}); | |
gulp.task('build', ['optimize', 'fonts', 'images'], function() { | |
log('Building everything'); | |
var msg = { | |
title: 'gulp build', | |
subtitle: 'Deployed to the build folder', | |
message: 'Running `gulp serve-build`' | |
}; | |
del(config.temp); | |
log(msg); | |
notify(msg); | |
}); | |
gulp.task('build-specs', ['template-cache'], function() { | |
log('building the spec runner'); | |
var wiredep = require('wiredep').stream; | |
var options = config.getWiredepDefaultOptions(); | |
var specs = config.specs; | |
options.devDependencies = true; | |
if (args.startServers) { | |
specs = [].concat(specs, config.serverIntegrationSpecs); | |
} | |
return gulp | |
.src(config.specRunner) | |
.pipe(wiredep(options)) | |
.pipe($.inject(gulp.src(config.testLibraries), {name: 'inject:testlibraries', read: false})) | |
.pipe($.inject(gulp.src(config.js))) | |
.pipe($.inject(gulp.src(config.specHelpers), {name: 'inject:spechelpers', read: false})) | |
.pipe($.inject(gulp.src(specs), {name: 'inject:specs', read: false})) | |
.pipe($.inject(gulp.src( | |
config.temp + config.templateCache.file), | |
{name: 'inject:templates', read: false})) | |
.pipe(gulp.dest(config.client)); | |
}); | |
gulp.task('optimize', ['inject', 'test'], function() { | |
log('Optimizing the javascript, css, html'); | |
var assets = $.useref.assets({searchPath: './'}); | |
var cssFilter = $.filter('**/*.css'); | |
var jsAppFilter = $.filter('**/' + config.optimized.app); | |
var jsLibFilter = $.filter('**/' + config.optimized.lib); | |
var templateCache = config.temp + config.templateCache.file; | |
return gulp | |
.src(config.index) | |
.pipe($.plumber()) | |
.pipe($.inject( | |
gulp.src(templateCache, {read: false}), | |
{starttag: '<!-- inject:templates:js -->'} | |
)) | |
.pipe(assets) | |
.pipe(cssFilter) | |
.pipe($.csso()) | |
.pipe(cssFilter.restore()) | |
.pipe(jsLibFilter) | |
.pipe($.uglify()) | |
.pipe(jsLibFilter.restore()) | |
.pipe(jsAppFilter) | |
.pipe($.ngAnnotate()) | |
.pipe($.uglify()) | |
.pipe(jsAppFilter.restore()) | |
.pipe($.rev()) | |
.pipe(assets.restore()) | |
.pipe($.useref()) | |
.pipe($.revReplace()) | |
.pipe(gulp.dest(config.build)) | |
.pipe($.rev.manifest()) | |
.pipe(gulp.dest(config.build)); | |
}); | |
gulp.task('bump', function() { | |
var msg = 'Bumping versions'; | |
var type = args.type; | |
var version = args.version; | |
var options = {}; | |
if (version) { | |
options.version = version; | |
msg += ' to ' + version; | |
} else { | |
options.type = type; | |
msg += ' for a ' + type; | |
} | |
log(msg); | |
return gulp | |
.src(config.packages) | |
.pipe($.bump(options)) | |
.pipe(gulp.dest(config.root)); | |
}); | |
gulp.task('serve-build', ['build'], function() { | |
serve(false, false); | |
}); | |
gulp.task('serve-dev', ['inject'], function() { | |
serve(true, false); | |
}); | |
gulp.task('serve-specs', ['build-specs'], function(done) { | |
log('Run the spec runner'); | |
serve(true, true); | |
done(); | |
}); | |
gulp.task('test', ['vet', 'template-cache'], function(done) { | |
startTests(true, done); | |
}); | |
gulp.task('auto-test', ['vet', 'template-cache'], function(done) { | |
startTests(false, done); | |
}); | |
function serve(isDev, specRunner) { | |
var nodeOptions = { | |
script: config.nodeServer, | |
delayTime: 1, | |
env: { | |
'PORT': port, | |
'NODE_ENV': isDev ? 'dev' : 'build' | |
}, | |
watch: [config.server] | |
}; | |
return $.nodemon(nodeOptions) | |
.on('restart', function(env) { | |
log('*** nodemon restarted'); | |
log('files changed on restart:\n' + env); | |
setTimeout(function() { | |
browserSync.notify('reloading now...'); | |
browserSync.reload({stream: false}); | |
}, config.browserReloadDelay); | |
}) | |
.on('start', function() { | |
log('*** nodemon started'); | |
startBrowserSync(isDev, specRunner); | |
}) | |
.on('crash', function() { | |
log('*** nodemon crashed: script crashed for some reason'); | |
}) | |
.on('exit', function() { | |
log('** nodemon exited cleanly'); | |
}); | |
} | |
function clean(path, done) { | |
log('Cleaning: ' + $.util.colors.blue(path)); | |
del(path, done); | |
} | |
function log(msg) { | |
if (typeof(msg) === 'object') { | |
for (var item in msg) { | |
if (msg.hasOwnProperty(item)) { | |
$.util.log($.util.colors.blue(msg[item])); | |
} | |
} | |
} else { | |
$.util.log($.util.colors.blue(msg)); | |
} | |
} | |
function changeEvent(event) { | |
var srcPattern = new RegExp('/.*(?=/' + config.source + ')/'); | |
log('File: ' + event.path.replace(srcPattern, '') + ' ' + event.type); | |
} | |
function startBrowserSync(isDev, specRunner) { | |
if (args.nosync || browserSync.active) { | |
return; | |
} | |
log('Starting browser-sync on port ' + port); | |
if (isDev) { | |
gulp.watch([config.less], ['styles']) | |
.on('change', function(event) { | |
changeEvent(event); | |
}); | |
} else { | |
gulp.watch([config.less, config.js, config.html], ['optimize', browserSync.reload]) | |
.on('change', function(event) { | |
changeEvent(event); | |
}); | |
} | |
var options = { | |
proxy: 'localhost:' + port, | |
port: 3000, | |
files: isDev ? [ | |
config.client + '**/*.*', | |
'!' + config.less, | |
config.temp + '**/*.css' | |
] : [], | |
ghostMode: { | |
clicks: true, | |
location: false, | |
forms: true, | |
scroll: true | |
}, | |
injectChanges: true, | |
logFileChanges: true, | |
logLevel: 'debug', | |
logPrefix: 'gulp-patterns', | |
notify: true, | |
reloadDelay: 1000 | |
}; | |
if (specRunner) { | |
options.startPath = config.specRunner; | |
} | |
browserSync(options); | |
} | |
function startTests(singleRun, done) { | |
var child; | |
var fork = require('child_process').fork; | |
var Server = require('karma').Server; | |
var excludeFiles = []; | |
var serverSpecs = config.serverIntegrationSpecs; | |
if (args.startServers) { | |
log('Starting server'); | |
var savedEnv = process.env; | |
savedEnv.NODE_ENV = 'dev'; | |
savedEnv.port = 8888; | |
child = fork(config.nodeServer); | |
} else { | |
if (serverSpecs && serverSpecs.length) { | |
excludeFiles = serverSpecs; | |
} | |
} | |
var server = new Server({ | |
configFile: __dirname + '/karma.conf.js', | |
exclude: excludeFiles, | |
singleRun: !!singleRun | |
}, karmaCompleted); | |
server.start(); | |
function karmaCompleted(karmaResult) { | |
log('Karma completed'); | |
if (child) { | |
log('Shutting down the child process'); | |
child.kill(); | |
} | |
if (karmaResult === 1) { | |
done('karma: tests failed with code' + karmaResult); | |
} else { | |
done(); | |
} | |
} | |
} | |
function notify(options) { | |
var notifier = require('node-notifier'); | |
var notifyOptions = { | |
sound: 'Bottle', | |
contentImage: path.join(__dirname, 'gulp.png'), | |
icon: path.join(__dirname, 'gulp.png') | |
}; | |
assign(notifyOptions, options); | |
notifier.notify(notifyOptions); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment