'use strict'
let less = require('gulp-less')
  , gulp = require('gulp')
  , path = require('path')
  , concat = require('gulp-concat')
  , rename = require('gulp-rename')
  , minifyCss = require('gulp-minify-css')
  , sass = require('gulp-sass')
  , exec = require('child_process').exec
  , execFile = require('child_process').execFile
  ;

var scssPath = [
  '../web_content/assets/scss/skin.scss',
  '../web_content/app/**/*.scss',     '../web_content/assets/scss/theme.scss',
  '../web_content/app/**/*.css'
];

gulp.task('scss', function () {
  return gulp.src([
     '../web_content/assets/scss/theme.scss',
    '../web_content/assets/scss/skin.scss',
    '../web_content/app/**/*.scss',
    '../web_content/app/**/*.css'
  ])
    .pipe(sass.sync().on('error', sass.logError))
    .pipe(concat('all.css'))
    .pipe(gulp.dest('../web_content/assets/css/'));
});


gulp.task('minifyjs', function () {
  return gulp.src(
    [
      '../modules/app_cache.js',
      '../modules/sys_config.js',
      '../modules/wfs_gateway.js',
      '../modules/down_folder.js',
      '../modules/upload.js',

      '../modules/window_load/complete.event.js',
      '../modules/window_load/drag.js',
      '../modules/window_load/init.task.js',
      '../modules/window_load/tray.js',

      '../web_content/app/app.module.js',
      '../web_content/app/app.config.js',
      '../web_content/app/app.router.js',
      '../web_content/app/app.service.js',
      '../web_content/app/app.filter.js',
      '../web_content/app/app.factory.js',
      '../web_content/app/app.main.js',
      '../web_content/app/**/*.js'
    ])
    .pipe(concat('all.js'))
    .pipe(rename({ suffix: '.min' }))
    .pipe(gulp.dest('./'));
});


gulp.task('login', function () {
  return gulp.src(
    [
      '../modules/app_cache.js',
      '../modules/sys_config.js',
      '../modules/init.js',
      '../web_content/login.js'
    ])
    .pipe(concat('login.js'))
    .pipe(rename({ suffix: '.min' }))
    .pipe(gulp.dest('./'));
});

gulp.task('sso', function () {
    return gulp.src(
        [
        '../modules/app_cache.js',
        '../modules/sys_config.js',
        '../modules/init.js',
         '../web_content/sso.js'
        ])
        .pipe(concat('sso.js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest('./'));
});

gulp.task('watch', function () {
  gulp.watch(scssPath, ['scss']);
});

gulp.task('jswatch', function () {
  gulp.watch(['../web_content/app/**/*.js'], ['default1']);
});



gulp.task('default1', ['scss', 'minifyjs', 'login','sso'], function () {
  exec(`start.bat`, (error, stdout, stderr) => {
    if (error) {
      console.error(`start.bat error: ${error}`);
      return;
    }
    console.log('start.bat Finish', Date.now());
  });
  // exec(`powershell.exe ../nwjc login.min.js ../sign.dll`, (error, stdout, stderr) => {
  //   if (error) {
  //     console.error(`login.min.js error: ${error}`);
  //     return;
  //   } 
  // });

});


gulp.task('default', ['jswatch', 'watch', 'default1']);

