gulpfile.js 3.56 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: 'images/yohood/mobile',
  font: dist_assets_dir + '/fonts'
};


gulp.task('default', function() {
  gulp.watch(['sass/**', '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/**'], ['compass-dist']);
});

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

//编译部署compass
gulp.task('compass-dist', function() {
  gulp.src('sass/**')
    .pipe(compass({
      css: 'css',
      sass: 'sass',
      image: 'images/yohood/mobile',
      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("将文件编译到本地!!!!");
});