gulpfile.js 5.09 KB
/**
 * GULP ENTRY
 * author: xuqi(qi.xu@yoho.cn)
 * date: 2015/6/29
 */

var gulp = require('gulp'),
    fs = require('fs'),
    ftp = require('gulp-ftp'),
    gutil = require('gulp-util'),
    concat = require('gulp-concat'),
    compass = require('gulp-compass'),
    exec = require('child_process').exec,
    mkdirp = require('mkdirp'),
    uglify = require('gulp-uglify'),
    Package = require('father').SpmPackage,
    transport = require('gulp-spm'),
    request = require('request'),
    // md5 = require("gulp-md5"),
    rmdir = require('rmdir')
    server = require('gulp-develop-server');

gulp.task('default', ['server', 'server:restart', 'compass-watch', 'compass']);

// start express server
gulp.task('server', function() {
    server.listen({
        path: 'app.js'
    });
});

// restart server if app.js changed
gulp.task('server:restart', function() {
    gulp.watch(['app.js', 'views/controller/*.js', 'public/js/data.js'], server.restart);
});

// compass watch
gulp.task('compass-watch', function() {
    gulp.watch('public/sass/**/*.scss', ['compass']);
});

//compass
gulp.task('compass', function() {
    gulp.src('public/sass/**/*.scss')
        .pipe(
            compass({
                config_file: 'config.rb',
                css: 'public/css',
                sass: 'public/sass'
            })
        )
});

//PRODUCTION
//cdn
var config = JSON.parse(fs.readFileSync('./package.json').toString()),
    distRoot = 'dist/yohobuy-mobile/',
    distDir = {
        js: distRoot + config.name + '/' + config.version,
        css: distRoot + config.name + '/' + config.version,
        image: distRoot + config.name + '/assets/images',
        fonts: distRoot + config.name + '/assets/fonts'
    },
    cdnDomain = 'http://cdn.yoho.cn/',
    ftpConfig = {
        host: '58.213.133.26',
        user: 'php',
        pass: 'yoho9646'
    };

gulp.task('dist', function() {
    var ftpstream = ftp(ftpConfig);
    return gulp.src('dist/**/')
        .pipe(ftpstream)
        .pipe(gutil.noop());
});

//拷贝fonts+images到发布目录
gulp.task('assets', function() {
    gulp.src('public/img/**')
        .pipe(gulp.dest(distDir.image));
    gulp.src('public/fonts/*')
        .pipe(gulp.dest(distDir.fonts));
});

//compass整合所有css到index后发布到发布目录
gulp.task('compass-production', function() {
    gulp.src('public/sass/*.scss')
        .pipe(
            compass({
                css: distDir.css,
                sass: 'public/sass',
                image: distDir.image,
                font: distDir.fonts,
                http_path: '/',
                style: 'compressed'
            })
        )
        .on('error', function(error) {
            console.log(error);
            this.emit('end');
        });
});


//JS build
gulp.task('build', ['libs-build', 'index-build']);

gulp.task('libs-build', ['pre-libs', 'concat-libs', 'min-libs', 'clear-libs']);

gulp.task('index-build', ['libs-build'], function() {
    var pkg = new Package(__dirname);
    return gulp.src(pkg.main)
        .pipe(transport({
            pkg: pkg
        }))
        .pipe(concat('index-debug.js'))
        .pipe(gulp.dest(distDir.js))
        .pipe(uglify())
        .pipe(concat('index.js'))
        .pipe(gulp.dest(distDir.js));
});

//SPM 打包库文件
gulp.task('pre-libs', function() {
    libPkgPre();
    var pkg = new Package(__dirname);
    return gulp.src(pkg.main)
        .pipe(transport({
            pkg: pkg
        }))
        .pipe(gulp.dest(distDir.js));
});

//合并库文件
gulp.task('concat-libs', ['pre-libs'], function() {
    return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
        .pipe(concat('libs-debug.js'))
        .pipe(gulp.dest(distDir.js));
});

//压缩库文件
gulp.task('min-libs', ['pre-libs'], function() {
    return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
        .pipe(concat('libs.js'))
        .pipe(uglify())
        // .pipe(md5())
        .pipe(gulp.dest(distDir.js));

});

//清除过程文件
gulp.task('clear-libs', ['concat-libs', 'min-libs'], function() {
    fs.renameSync('package_bak.json', 'package.json'); //恢复原包配置文件
    fs.unlinkSync('./libs.js'); //删除入口文件
    // fs.unlinkSync(distDir.js + '/libs.js');
});


//库文件的入口文件和过程文件的生成
function libPkgPre() {
    var obj = {
            name: config.name,
            version: config.version,
            spm: config.spm
        },
        libCon, libsjs = '',
        key,
        libDir = distDir.js + '';
    obj.spm.main = 'libs.js';
    obj.spm.buildArgs = '--idleading {{}} --include all';

    libCon = JSON.stringify(obj);
    fs.renameSync('package.json', 'package_bak.json');
    fs.writeFileSync('package.json', libCon); //生成库文件pkg配置
    var packages = [];
    for (key in obj.spm.dependencies) {
        if (obj.spm.inside && obj.spm.inside[key]) {
            packages.push(obj.spm.inside[key]);
        } else {
            packages.push(key);
        }
    }
    for (var i = 0; i < packages.length; i++) {
        libsjs += "require('" + packages[i] + "');";
    }

    fs.writeFileSync('libs.js', libsjs); //写入口文件
    return libDir;
}