import * as gulp from 'gulp';
import * as util from 'gulp-util';
import * as chalk from 'chalk';
import * as gulpLoadPlugins from 'gulp-load-plugins';
import { join } from 'path';
import * as _runSequence from 'run-sequence';

const TASKS_PATH = join('tools', 'tasks');

function task(taskName: string, option?: string) {
  util.log('loading task', chalk.yellow(taskName, option || ''));
  return require(join('..', 'tasks', taskName))(gulp, gulpLoadPlugins(), option);
}

function registerTask(taskName: string, fileName?: string, option: string = ''): void {
  gulp.task(taskName, task(fileName || taskName, option));
}

function scanDir(root: string, cb: (taskName: string) => void) {
  if (!existsSync(root)) return;

  walk(root);

  function walk(path) {
    let files = readdirSync(path);
    for (let i = 0, j = files.length; i < j; i+=1) {
      let file = files[i];
      let curPath = join(path, file);
      if (lstatSync(curPath).isFile() && /\.ts$/.test(file)) {
        let taskName = file.replace(/(\.ts)/, '');
        cb(taskName);
      }
    }
  }
}

export function runSequence(...sequence: any[]) {
  let tasks = [];
  let _sequence = sequence.slice(0);
  sequence.pop();

  scanDir(TASKS_PATH, taskName => tasks.push(taskName));

  sequence.forEach(task => {
    if (tasks.indexOf(task) > -1) {
      registerTask(task);
    }
  });

  return _runSequence(..._sequence);
}

// ---------------
// Copy SCSS
gulp.task('copy', done => {
  console.log('-------- COPY TASK ------');
  // runSequence('copy.assets', done);
});
@@ -1,1541 +1,337 @@
-import * as gulp from 'gulp';%0Aimport * as util from 'gulp-util';%0Aimport * as chalk from 'chalk';%0Aimport * as gulpLoadPlugins from 'gulp-load-plugins';%0Aimport %7B join %7D from 'path';%0Aimport * as _runSequence from 'run-sequence';%0A%0Aconst TASKS_PATH = join('tools', 'tasks');%0A%0Afunction task(taskName: string, option?: string) %7B%0A  util.log('loading task', chalk.yellow(taskName, option %7C%7C ''));%0A  return require(join('..', 'tasks', taskName))(gulp, gulpLoadPlugins(), option);%0A%7D%0A%0Afunction registerTask(taskName: string, fileName?: string, option: string = ''): void %7B%0A  gulp.task(taskName, task(fileName %7C%7C taskName, option));%0A%7D%0A%0Afunction scanDir(root: string, cb: (taskName: string) =%3E void) %7B%0A  if (!existsSync(root)) return;%0A%0A  walk(root);%0A%0A  function walk(path) %7B%0A    let files = readdirSync(path);%0A    for (let i = 0, j = files.length; i %3C j; i+=1) %7B%0A      let file = files%5Bi%5D;%0A      let curPath = join(path, file);%0A      if (lstatSync(curPath).isFile() && /%5C.ts$/.test(file)) %7B%0A        let taskName = file.replace(/(%5C.ts)/, '');%0A        cb(taskName);%0A  
+'use strict';%0A%0Aconst gulp = require('gulp');%0Aconst path = require('path');%0Aconst sass = require(%22gulp-sass%22);%0A%0Agulp.task('copy', () =%3E %7B%0A  return gulp.src(%5B%0A    path.join('src/lib/card', '**', '*.scss'),%0A    '!' + path.join('src/lib', '**', '*.ts')%0A
     
-%7D
+%5D)
 %0A    
-%7D%0A  %7D%0A%7D%0A%0Aexport function runSequence(...sequence: any%5B%5D) %7B%0A  let tasks = %5B%5D;%0A  let _sequence = sequence.slice(0);%0A  sequence.pop();%0A%0A  scanDir(TASKS_PATH, taskName =%3E t
+.pipe(sass().on('error', s
 as
-k
 s.
-push(taskName));%0A%0A  sequence.forEach(task =%3E %7B%0A    if (tasks.indexOf(task) %3E -1) %7B%0A      registerTask(task);%0A    %7D%0A  %7D);%0A%0A  return _runSequence(..._sequence);%0A%7D%0A%0A// ---------------%0A// Copy SCSS%0Agulp.task('copy', done =%3E %7B%0A  console.log('-------- COPY TASK ------');%0A  // runSequence('copy.assets', done
+logError))%0A    .pipe(gulp.dest('dist/card')
 );%0A%7D

add gulp CSS task (needs to refactor later)