'use strict';

// Load plugins
const gulp = require('gulp'),
    clean = require('gulp-clean'),
    zip = require('gulp-zip');

let getYYYYMMDDHHMM = () => {
    let toXX = (input) => ((input < 10) ? ('0' + input) : input);

    let date = new Date();
    return `${date.getFullYear()}${toXX(date.getMonth() + 1)}${toXX(date.getDate())}${toXX(date.getHours())}${toXX(date.getMinutes())}`;
};

// 复制必要的文件
gulp.task('copyBower', ['clean'], ()=> {
    return gulp.src([
            //angular
            'bower_components/angular/angular.min.js',

            //bootstrap
            'bower_components/bootstrap/dist/**/*',

            //font-awesome
            'bower_components/font-awesome/css/**/*',
            'bower_components/font-awesome/fonts/**/*',

            //jquery
            'bower_components/jquery/dist/**/*',

            //ua-parser-js
            'bower_components/ua-parser-js/dist/**/*',

            //jquery-elastic
            'bower_components/jquery-elastic/jquery.elastic.source.js',
        ], {'base': '.'})
        .pipe(gulp.dest('dist/'));
});

// 复制必要的文件
gulp.task('copyNpm', ['clean'], ()=> {
    return gulp.src([
            //
            //'node_modules//**/*',

        ], {'base': '.'})
        .pipe(gulp.dest('dist/'));
});

// 复制必要的文件
gulp.task('copy', ['clean', 'copyBower'], ()=> {
    return gulp.src([
            'manifest.json',
            'html/*',
            'js/**/*',
            'css/*',
            //'bower_components/**/',
            'img/*'
        ], {'base': '.'})
        .pipe(gulp.dest('dist/'));
});


// Clean
gulp.task('clean', () => {
    return gulp.src([
            'dist/',
            'releases/'
        ], {read: false})
        .pipe(clean());
});

// zip
gulp.task('zip', ['clean', 'copy'], ()=> {
    return gulp.src('dist/**/*', {'base': '.'})
        .pipe(zip('SailFishRequester-' + getYYYYMMDDHHMM() + '.zip'))
        .pipe(gulp.dest('releases'));
});


// Build
gulp.task('build', ['clean', 'copy', 'zip']);

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

// Watch
gulp.task('watch', ['build'], () => {
    gulp.watch([
        //项目依赖单个文件
        'manifest.json',
        'gulpfile.js',

        //应用逻辑
        'css/**/*',
        'html/**/*',
        'img/**/*',
        'js/**/*',

        //依赖库
        'bower_components/**/*',
        //'node_modules/',
    ], ['build']);
});
@@ -1750,29 +1750,21 @@
 zip', %5B'
-clean', 'copy
+build
 '%5D, ()=%3E
@@ -1976,15 +1976,8 @@
 opy'
-, 'zip'
 %5D);%0A
@@ -2264,32 +2264,80 @@
   //%E4%BE%9D%E8%B5%96%E5%BA%93%0A        
+'bower.json',%0A        'package.json',%0A        //
 'bower_component

优化gulp 构建流程，降低功耗