gulpfile.js 4.59 KB
/**
 * GULP-FILE
 * author: xuqi(qi.xu@yoho.cn)
 * date: 2015/9/30
 */

var gulp = require('gulp'),
    compass = require('gulp-compass'),
    cp = require('child_process');

var fs = require('fs'),
    ftp = require('gulp-ftp'),
    gutil = require('gulp-util'),
    uglify = require('gulp-uglify'),
    Package = require('father').SpmPackage,
    transport = require('gulp-spm'),
    concat = require('gulp-concat');

var config = JSON.parse(fs.readFileSync('./package.json').toString());

var rootDist = 'dist/myohobuy/',
    ftpConfig = {
        host: '218.94.75.58',
        user: 'php',
        pass: 'yoho9646'
    };

var distDir = {
    js: rootDist + config.version,
    css: rootDist + config.version,
    img: rootDist + 'assets/img',
    font: rootDist + 'assets/font'
};

gulp.task('default', ['compass', 'compass-watch', 'spm-doc']);

// compass
gulp.task('compass', function() {
    gulp.src('sass/**/*.scss')
        .pipe(
            compass({
                config_file: 'config.rb',
                css: 'css',
                sass: 'sass'
            })
        ).on('error', function(error) {
            console.log(error);
            this.emit('end');
        });
});

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

// start spm server
gulp.task('spm-doc', function() {
    var sd = cp.exec('spm doc');

    // sd.stdout.on('data', function(data) {
    //     console.log(data);
    // });

    sd.stderr.on('data', function(data) {
        console.log(data);
    });

    sd.on('exit', function(code) {
        console.log('process spm doc exit with code ' + code);
    });
});

//生成发布目录,可用于上传测试机
gulp.task('ge', ['assets', 'compass-production', 'build']);

//发布
gulp.task('dist', ['assets', 'compass-production', 'build'], function() {
    var ftpstream = ftp(ftpConfig);

    return gulp.src('dist/**/')
        .pipe(ftpstream)
        .pipe(gutil.noop());
});

//font+img->dist/assets
gulp.task('assets', function() {
    gulp.src('img/**')
        .pipe(gulp.dest(distDir.img));
    gulp.src('font/*')
        .pipe(gulp.dest(distDir.font));
});

//compass
gulp.task('compass-production', ['assets'], function() {
    gulp.src('sass/index.scss')
        .pipe(
            compass({
                css: distDir.css,
                sass: 'sass',
                image: distDir.img,
                font: distDir.font,
                http_path: '/',
                style: 'compressed'
            })
        )
        .on('error', function(error) {
            console.log(error);
            this.emit('end');
        });
});

//spm build
gulp.task('build', ['libs', 'business']);

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

gulp.task('pre-libs', function() {
    var obj = {
        name: '',
        version: config.version,
        spm: config.spm
    };
    var packages = [],
        libsjs = '';

    var libCon, key, i;

    var pkg;

    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);

    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 (i = 0; i < packages.length; i++) {
        libsjs += 'require("' + packages[i] + '");';
    }

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

    pkg = new Package(__dirname);

    return gulp.src(pkg.main)
        .pipe(transport({
            pkg: pkg
        }))
        .pipe(gulp.dest(distDir.js));
});

gulp.task('concat-min-libs', ['pre-libs'], function() {
    var path = distDir.js + '/libs.js';
    var jsStr = fs.readFileSync(path).toString();

    fs.writeFileSync(path, jsStr.substr(jsStr.indexOf('});') + 4));

    return gulp.src(['js/plugin/sea.js', distDir.js + '/libs.js'])
        .pipe(concat('lib.js'))
        .pipe(uglify())
        .pipe(gulp.dest(distDir.js));
});

gulp.task('clear-libs', ['concat-min-libs'], function() {
    fs.renameSync('package.bak.json', 'package.json');
    fs.unlinkSync('./libs.js');
    fs.unlinkSync(distDir.js + '/libs.js');
});

gulp.task('business', ['libs'], 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));
});