var gulp = require('gulp')
var replace = require('gulp-replace')
var ngAnnotate = require('gulp-ng-annotate')
var htmlmin = require('gulp-htmlmin')
var rename = require('gulp-rename')
var uglify = require('gulp-uglify')
var sass = require('gulp-sass')
var cssmin = require('gulp-cssmin')
var autoprefixer = require('gulp-autoprefixer')
var templateCache = require('gulp-angular-templatecache')
const babel = require('gulp-babel')
var header = require('gulp-header')
var fs = require('fs')
var runSequence = require('run-sequence')
var del = require('del')

gulp.task('build', function (cb) {
	runSequence('backup', 'clean', 'build-html', 'build-js', 'build-sass', 'banner', cb)
})

gulp.task('banner', function () {
	var pkg = require('./package.json');
	var banner = ['/**',
		' * <%= pkg.name %> - <%= pkg.description %>',
		' * @version v<%= pkg.version %>',
		' * @link <%= pkg.homepage %>',
		' * @license <%= pkg.license %>',
		' */',
		''].join('\n')

	return gulp.src('dist/**.{js,css}')
		.pipe(header(banner, {pkg: pkg}))
		.pipe(gulp.dest('dist'))
})

gulp.task('build-js', ['templateCache'], function () {
	return gulp.src('src/**.js')
		.pipe(replace("/*the place to place the generated angular templateCache*/", function () {
			return fs.readFileSync('.tmp/template.js', 'utf-8').toString()
		}))
		.pipe(babel({
			presets: ['es2015']
		}))
		.pipe(ngAnnotate())
		.pipe(gulp.dest('dist'))
		.pipe(uglify())
		.pipe(rename({suffix: '.min'}))
		.pipe(gulp.dest('dist'))
})

gulp.task('build-html', function () {
	return gulp.src('src/**.html')
		.pipe(htmlmin({
			collapseWhitespace: true,
			conservativeCollapse: true
		}))
		.pipe(gulp.dest('.tmp/'))
})

gulp.task('build-sass', function () {
	return gulp.src('src/**.scss')
		.pipe(sass({
			errLogToConsole: true,
			outputStyle: 'expanded'
		}))
		.pipe(autoprefixer('last 5 version', '> 1%', 'ie 9'))
		.pipe(gulp.dest('dist'))
		.pipe(cssmin())
		.pipe(rename({suffix: '.min'}))
		.pipe(gulp.dest('dist'))
})

gulp.task('backup', function () {
	return gulp.src('dist/**')
		.pipe(gulp.dest('backup/dist-' + Date.now()))
})

gulp.task('clean', function (cb) {
	del.sync('dist')
	del.sync('.tmp')
	cb()
})

gulp.task('templateCache', function () {
	return gulp.src('src/**.html')
		.pipe(htmlmin({
			collapseWhitespace: true,
			conservativeCollapse: true
		}))
		.pipe(templateCache({
			filename: 'template.js',
			root: 'template/',
			module: 'fa.directive.borderLayout'
		}))
		.pipe(gulp.dest('.tmp'))
})

@@ -767,24 +767,28 @@
 = %5B'/**',%0A%09%09
+%09%09  
 ' * %3C%25= pkg.
@@ -820,24 +820,28 @@
 tion %25%3E',%0A%09%09
+%09%09  
 ' * @version
@@ -861,24 +861,28 @@
 sion %25%3E',%0A%09%09
+%09%09  
 ' * @link %3C%25
@@ -903,16 +903,20 @@
  %25%3E',%0A%09%09
+%09
+%09  
 ' * @lic
@@ -943,16 +943,20 @@
  %25%3E',%0A%09%09
+%09%09  
 ' */',%0A%09
@@ -956,16 +956,20 @@
  */',%0A%09%09
+%09%09  
 ''%5D.join
@@ -1041,16 +1041,17 @@
 r, %7B
+ 
 pkg: pkg
 %7D))%0A
@@ -1046,16 +1046,17 @@
 pkg: pkg
+ 
 %7D))%0A%09%09.p
@@ -1395,16 +1395,34 @@
 nnotate(
+%7B%0A%09%09%09add: true%0A%09%09%7D
 ))%0A%09%09.pi
@@ -1469,32 +1469,33 @@
 %09%09.pipe(rename(%7B
+ 
 suffix: '.min'%7D)
@@ -1484,32 +1484,33 @@
 %7B suffix: '.min'
+ 
 %7D))%0A%09%09.pipe(gulp
@@ -1974,16 +1974,17 @@
 rename(%7B
+ 
 suffix: 
@@ -1989,16 +1989,17 @@
 : '.min'
+ 
 %7D))%0A%09%09.p

format build code