/*!
 * @filename gulpfile.js
 * @author watonyweng(github.com/watonyweng)
 * @copyright 2016 yunmind.cn
 * @version 0.1.0
 */

'use strict';

var gulp = require('gulp'),
    path = require('path'),
    less = require('gulp-less'),
    autoprefixer = require('gulp-autoprefixer'),
    filter = require('gulp-filter'),
    rename = require("gulp-rename");

const del = require('del');

// 声明Less任务
gulp.task('less', function() {
    gulp.src(['less/*.less'])
        .pipe(less())
        .pipe(gulp.dest('app/less'));
});

// 声明Autoprefixer任务
gulp.task('autoprefixer', ['less'], function() {
    return gulp.src(['app/less/*.css'])
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(gulp.dest('app/css'));
});

// 声明CopyCss任务
gulp.task('copyCssFiles', ['autoprefixer'], function() {
    var gulpFilter = filter(['**']);
    return gulp.src(['app/css/*.css']).pipe(gulpFilter)
        .pipe(gulp.dest('public/stylesheets'));
});

// 声明CopyFont任务
gulp.task('copyFontsFiles', function() {
    var gulpFilter = filter(['fonts/**']);
    return gulp.src(['app/**/*']).pipe(gulpFilter)
        .pipe(gulp.dest('public'));
});

// 声明构建任务
gulp.task('build', ['copyCssFiles', 'copyFontsFiles'], function() {
    return gulp.src(['app/js/*.js', 'app/js/modules/*.*', 'app/js/**/*.*'])
        .pipe(rename({
            dirname: ''
        }))
        .pipe(gulp.dest('public/javascripts'));
});

gulp.task('clean', function() {
    del(['public/javascripts/*.*', 'public/stylesheets/*.*', 'public/fonts/*.*']).then(function() {
        console.log('Finished gulp cleanTask');
    });
});

gulp.task('test', function() {
    console.log('Finished gulp testTask');
});
