gulpfile.js 3.73 KB
var gulp = require('gulp');
var gutil = require('gulp-util');
var ftp = require('gulp-ftp');
var fs = require('fs');
var uglify = require('gulp-uglify'),
    rename = require('gulp-rename');
var concat = require('gulp-concat');
var exec = require('child_process').exec;
var compass = require('gulp-compass');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');

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

var dist_assets_dir = 'dist/'+config.name+'/assets';
var dist_seajs_dir='dist/'+config.name+'/'+config.version+"/min";
var dist_js_dir='dist/'+config.name+'/'+config.version;
var source_js_dir='js/';

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

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

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

//compass的watch
gulp.task('cw',function(cb)
{
    /*exec('compass watch', function(err) 
    {
        if (err) return cb(err); 
        cb(); 
    });*/
    gulp.watch(['sass/scss/**','js/**'],['compass-dist','build']);
});

//js代码验证
gulp.task('lint',function()
{
     return gulp.src(['js/*.js','js/**/*.js'])
        .pipe(jshint())
        .pipe(jshint.reporter(stylish));
});

//编译部署compass
gulp.task('compass-dist',['assets'],function()
{  
    gulp.src('sass/scss/**')
    .pipe(compass(
    {
      css: dist_dir.css,
      sass: 'sass/scss',
      image: dist_dir.image,
      font: dist_dir.font,

      style:'compressed'
    }))
    .on('error', function(error) 
    {
      console.log(error);
      this.emit('end');
    });
});


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

gulp.task('js',['seajs','copy','concat'],function(){
	console.log("压缩合并完成");
});

gulp.task('seajs',function(){
	gulp.src('js/util/seajs_dev.js')
    	.pipe(uglify())
    	.pipe(rename('seajs.min.js'))
    	.pipe(gulp.dest(dist_seajs_dir));
});
gulp.task('copy',function(){
	gulp.src(dist_js_dir+'/index.js')
    	.pipe(rename('z_index.js'))
    	.pipe(gulp.dest(dist_seajs_dir));
  gulp.src(source_js_dir+'/yas.js')
      .pipe(gulp.dest(dist_js_dir));
});

gulp.task('concat',function(){
	gulp.src(dist_seajs_dir+'/*.js')
    	.pipe(concat('index.min.js'))
    	.pipe(gulp.dest(dist_js_dir));
});

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


});

//spm build
gulp.task('build', function() {
    exec('spm build --include all', function() {
        exec('gulp build-debug');
        exec('gulp build-production');
    });
});

//generate all-debug.js for development ENV
gulp.task('build-debug', function() {
    gulp.src(['js/util/seajs.js', 'dist/yohogirls-frontend-web/0.0.1/index-debug.js'])
        .pipe(concat('all-debug.js'))
        .pipe(gulp.dest('dist/yohogirls-frontend-web/0.0.1'))
});

//generate all.js for production ENV
gulp.task('build-production', function() {
    gulp.src(['js/util/seajs.js', 'dist/dist/yohogirls-frontend-web/0.0.1/index.js'])
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist/yohogirls-frontend-web/0.0.1'))
});

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

//发布到CDN
gulp.task('comp',['compass-dist','js'], function () 
{
	//编译到本地
	console.log("将文件编译到本地!!!!");
});