/* eslint no-console: ["error", { allow: ["log"] }] */
const gulp = require('gulp');
const connect = require('gulp-connect');
const gopen = require('gulp-open');
const runSequence = require('run-sequence');

const buildKsCore = require('./build-ks-core.js');
const buildKsVue = require('./build-ks-vue.js');
const buildKsReact = require('./build-ks-react.js');

const buildCoreJs = require('./build-core-js.js');
const buildCoreLess = require('./build-core-less.js');
const buildCoreComponents = require('./build-core-components.js');

const buildPhenome = require('./build-phenome.js');
const buildVue = require('./build-vue');
const buildReact = require('./build-react');

const env = process.env.NODE_ENV || 'development';

// Tasks
gulp.task('ks-core', buildKsCore);
gulp.task('ks-vue', buildKsVue);
gulp.task('ks-react', buildKsReact);
gulp.task('core-js', buildCoreJs);
gulp.task('core-less', buildCoreLess);
gulp.task('core-components', buildCoreComponents);
gulp.task('phenome', buildPhenome);

gulp.task('build-core', ['core-js', 'core-less', 'core-components']);
gulp.task('build-react', buildReact);
gulp.task('build-vue', buildVue);

// Watchers
const watch = {
  all() {
    gulp.watch(['./src/core/**/*.js'], () => runSequence('core-js', 'core-components', 'ks-react', 'ks-vue'));
    gulp.watch('./src/core/**/*.less', () => runSequence('core-less', 'core-components'));
    gulp.watch(['./src/phenome/**/*.js', './src/phenome/**/*.jsx'], () => runSequence('phenome', 'build-react', 'build-vue', 'ks-react', 'ks-vue'));
    gulp.watch(['./react/kitchen-sink/src/**/*.js', './react/kitchen-sink/src/**/*.jsx'], () => runSequence('ks-react'));
    gulp.watch(['./vue/kitchen-sink/src/**/*.js', './vue/kitchen-sink/src/**/*.jsx'], () => runSequence('ks-vue'));
  },
  core() {
    gulp.watch(['./src/core/**/*.js'], () => runSequence('core-js', 'core-components'));
    gulp.watch('./src/**/**/*.less', () => runSequence('core-less', 'core-components'));
  },
  react() {
    gulp.watch(['./src/core/**/*.js'], () => runSequence('core-js', 'core-components', 'ks-react'));
    gulp.watch('./src/core/**/*.less', () => runSequence('core-less', 'core-components'));
    gulp.watch(['./src/phenome/**/*.js', './src/phenome/**/*.jsx'], () => runSequence('phenome', 'build-react', 'ks-react'));
    gulp.watch(['./react/kitchen-sink/src/**/*.js', './react/kitchen-sink/src/**/*.jsx'], () => runSequence('ks-react'));
  },
  vue() {
    gulp.watch(['./src/core/**/*.js'], () => runSequence('core-js', 'core-components', 'ks-vue'));
    gulp.watch('./src/core/**/*.less', () => runSequence('core-less', 'core-components'));
    gulp.watch(['./src/phenome/**/*.js', './src/phenome/**/*.jsx'], () => runSequence('phenome', 'build-vue', 'ks-vue'));
    gulp.watch(['./vue/kitchen-sink/src/**/*.js', './vue/kitchen-sink/src/**/*.jsx'], () => runSequence('ks-vue'));
  },
};

// Server
function server() {
  connect.server({
    root: ['./'],
    livereload: false,
    port: '3000',
  });
}
gulp.task('server', () => {
  if (env === 'development') watch.all();
  server();
  gulp.src('./kitchen-sink/core/index.html').pipe(gopen({ uri: 'http://localhost:3000/kitchen-sink/core/' }));
});
gulp.task('server-core', () => {
  if (env === 'development') watch.core();
  server();
  gulp.src('./kitchen-sink/core/index.html').pipe(gopen({ uri: 'http://localhost:3000/kitchen-sink/core/' }));
});
gulp.task('server-react', () => {
  if (env === 'development') watch.react();
  server();
  gulp.src('./kitchen-sink/react/index.html').pipe(gopen({ uri: 'http://localhost:3000/kitchen-sink/react/' }));
});
gulp.task('server-vue', () => {
  if (env === 'development') watch.vue();
  server();
  gulp.src('./kitchen-sink/vue/index.html').pipe(gopen({ uri: 'http://localhost:3000/kitchen-sink/vue/' }));
});


@@ -1539,38 +1539,32 @@
  gulp.watch(%5B'./
-react/
 kitchen-sink/src
@@ -1552,32 +1552,38 @@
 './kitchen-sink/
+react/
 src/**/*.js', '.
@@ -1575,38 +1575,32 @@
 rc/**/*.js', './
-react/
 kitchen-sink/src
@@ -1588,32 +1588,38 @@
 './kitchen-sink/
+react/
 src/**/*.jsx'%5D, 
@@ -1661,36 +1661,32 @@
  gulp.watch(%5B'./
-vue/
 kitchen-sink/src
@@ -1674,32 +1674,36 @@
 './kitchen-sink/
+vue/
 src/**/*.js', '.
@@ -1695,36 +1695,32 @@
 rc/**/*.js', './
-vue/
 kitchen-sink/src
@@ -1708,32 +1708,36 @@
 './kitchen-sink/
+vue/
 src/**/*.jsx'%5D, 
@@ -1721,35 +1721,35 @@
 nk/vue/src/**/*.
-jsx
+vue
 '%5D, () =%3E runSeq
@@ -2314,22 +2314,16 @@
 tch(%5B'./
-react/
 kitchen-
@@ -2319,32 +2319,38 @@
 './kitchen-sink/
+react/
 src/**/*.js', '.
@@ -2350,22 +2350,16 @@
 js', './
-react/
 kitchen-
@@ -2355,32 +2355,38 @@
 './kitchen-sink/
+react/
 src/**/*.jsx'%5D, 
@@ -2763,20 +2763,16 @@
 tch(%5B'./
-vue/
 kitchen-
@@ -2768,32 +2768,36 @@
 './kitchen-sink/
+vue/
 src/**/*.js', '.
@@ -2797,20 +2797,16 @@
 js', './
-vue/
 kitchen-
@@ -2810,16 +2810,20 @@
 en-sink/
+vue/
 src/**/*
@@ -2815,35 +2815,35 @@
 nk/vue/src/**/*.
-jsx
+vue
 '%5D, () =%3E runSeq

Fix KS tasks