gulpfile.js 2.21 KB
var gulp = require('gulp');
var gutil = require('gulp-util');
var ftp = require('gulp-ftp');
var fs = require('fs');
var exec = require('child_process').exec;
var compass = require('gulp-compass');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
var concat = require('gulp-concat');
var clean = require('gulp-clean');

//获取package.json的配置参数
var config = JSON.parse(fs.readFileSync('./package.json').toString());

var dist_assets_dir = 'dist/'+config.name+'/assets';

var dist_dir = {
    js: 'dist/'+config.name+'/'+config.version,
    css: 'dist/'+config.name+'/'+config.version,
    image: dist_assets_dir+'/images',
    font: dist_assets_dir+'/fonts'
};

var ftpConfig = {
    host: '58.213.133.26',
    user: 'php',
    pass: 'yoho9646'
};

//js代码验证
gulp.task('lint',function() {
     return gulp.src(['js/*.js','js/**/*.js','!js/plugin/nicescroll.js'])
        .pipe(jshint({
             "asi": true,
             "newcap": true
         }))
        .pipe(jshint.reporter(stylish));
});

//编译部署compass
gulp.task('compass-dist',['assets'],function() {
    gulp.src('sass/**/*.scss')
        .pipe(compass({
            css: dist_dir.css,
            sass: 'sass',
            image: dist_dir.image,
            font: dist_dir.font,
            http_path:'/',
            style:'compressed'
        }))
        .on('error', function(error) {
            console.log(error);
            this.emit('end');
        });
});

//拷贝assets到发布目录
gulp.task('assets',function() {
    gulp.src('assets/**')
        .pipe(gulp.dest(dist_assets_dir));
});

//打包seajs
gulp.task('spm',['build'],function(cb) {
    gulp.src(['js/lib/seajs.js',dist_dir.js+'/index.js'])
        .pipe(concat('index.min.js'))
        .pipe(gulp.dest(dist_dir.js));
});


//spm build
gulp.task('build',function (cb) {
    exec('spm build --include standalone', function(err) {
        if (err) return cb(err);
        cb();
    });
});

//发布到CDN
gulp.task('dist',['compass-dist','spm'], function () {
    var ftpstream = ftp(ftpConfig);
    return gulp.src('dist/**')
        .pipe(ftpstream)
        .pipe(gutil.noop());
});

// watch打包js
gulp.task('watch',function () {
    gulp.watch(['js/**'],['build']);
});