const gulp = require('gulp');
const watch = require('gulp-watch');
const concat = require('gulp-concat');
const less = require('gulp-less');
const minify = require('gulp-minify-css');
const ngMinify = require('gulp-uglify');
const ngAnnotate = require('gulp-ng-annotate');
const del = require('del');
const plumber = require('gulp-plumber');
const templateCache = require('gulp-angular-templatecache');
const rev = require('gulp-rev');
const revDel = require('rev-del');
function getTime () {
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return strTime;
}
function handleError (err) {
console.log(err.toString());
process.exit(-1);
}
process.on('uncaughtException', function (er) {
console.error('Throwing error:', er);
});
/////////////////////
// JS CONCAT ////////
/////////////////////
var jsFileList = ['public/js/module.js',
'public/js/**/*.js', 'public/components/**/*.js',
'!public/js/lib/*.js', '!public/js/code/*.js'];
gulp.task('jscc', function() {
// on change of files, trigger the given gulp target
watch(jsFileList, function () {
gulp.start('js-copy');
});
});
gulp.task('js-copy', function() {
gulp.src(jsFileList)
.pipe(concat('code.js'))
.pipe(gulp.dest('public/js/code'))
.pipe(concat('code.min.js'))
.pipe(ngAnnotate())
.pipe(ngMinify())
//.pipe(rev())
//.pipe(gulp.dest('public/js/code'))
//.pipe(rev.manifest('manifest.json'))
//.pipe(revDel())
.pipe(gulp.dest('public/js/code'))
.on('error', handleError);
});
/////////////////////
// LESS COMPILE /////
/////////////////////
var lessFileList = ['public/less/**/*.less', 'public/components/**/*.less'];
gulp.task('lessc', function() {
// on change of files, trigger the given gulp target
watch(lessFileList, function () {
gulp.start('less-copy');
});
});
gulp.task('less-copy', function () {
gulp.src(lessFileList)
.pipe(plumber())
.pipe(less())
.pipe(concat('styles.css'))
.pipe(gulp.dest('public/css'))
.pipe(concat('styles.min.css'))
.pipe(minify())
// .pipe(rev())
// .pipe(gulp.dest('public/css'))
// .pipe(rev.manifest('manifest.json'))
// .pipe(revDel('manifest.json'))
.pipe(gulp.dest('public/css'))
.on('error', handleError);
});
///////////////////////////
// BUILD TEMPLATE CACHE ///
///////////////////////////
var templateFileList = ['public/components/**/*.html'];
gulp.task('template-watcher', function() {
// on change of files, trigger the given gulp target
watch(templateFileList, function () {
gulp.start('build-template-cache');
});
});
gulp.task('build-template-cache', function () {
// Result: public/js/routes/templates.js
gulp.src(['public/**/*.html', '!public/html/**/*.html'])
.pipe(templateCache())
.pipe(gulp.dest('public/js/modules'));
});
gulp.task('btc', ['build-template-cache'], function () {
// Shorthand for 'build-template-cache'
});
///////////////////////////
// MOVE TO PRODUCTION /////
///////////////////////////
gulp.task('production-copy', function() {
gulp.src(['app/**/*', 'public/**/*', 'server.js', 'package.json'], {base: '.'})
.pipe(gulp.dest('production'))
.on('error', handleError);
});
gulp.task('production-clean', function() {
del(['production/public/less',
'production/public/fonts',
'production/public/json',
'production/public/components',
'production/public/css/**/*', '!production/public/css/styles.min.css',
'production/public/js/**/*', '!public/js/lib/*.js',
'!production/public/js/code', '!production/public/js/code/code.min.js']);
});
gulp.task('production-remove', function() {
del(['production']);
});
// Default Task
gulp.task('default', ['js-copy', 'jscc', 'less-copy', 'lessc', 'btc', 'template-watcher']);