import gulp from 'gulp';
import gulpif from 'gulp-if';
import { obj as combine } from 'stream-combiner2';
import through2 from 'through2';
import sourcemaps from 'gulp-sourcemaps';
import babel from 'gulp-babel';
import { resolve, relative } from 'path';
import gutil from 'gulp-util';
import del from 'del';

export function ifJsSource(...then) {
  return gulpif(
    file => {
      if (file.path.match(/[\\\/]templates[\\\/]/)) return false;
      if (file.path.match(/\.jsx?$/)) return true;
      return false;
    },
    ...then
  );
}

export function outputPath(file) {
  return resolve(__dirname, 'lib', file.relative);
}

export function ifUnlinkEvent(...then) {
  const unlinkEvent = ({ event }) => event === 'unlink' || event === 'unlinkDir';
  return gulpif(unlinkEvent, ...then);
}

export function log(...preamble) {
  return through2((file, enc, cb) => {
    if (file.isDirectory()) return cb();
    gutil.log(...preamble, relative(process.cwd(), outputPath(file)));
    cb(null, file);
  });
}

export function removeOutput() {
  return combine([
    through2.obj((file, enc, cb) => {
      del(outputPath(file))
      .then(() => cb(null, file))
      .catch(err => cb(err));
    }),
    log(gutil.colors.red('d'))
  ]);
}

export function transpile() {
  return combine([
    sourcemaps.init(),
    babel(),
    sourcemaps.write(),
    toLib(),
  ]);
}

export function toLib() {
  return gulp.dest('lib');
}

export function reTranspile() {
  return combine([
    transpile(),
    log(gutil.colors.green('✔'))
  ]);
}

@@ -106,24 +106,33 @@
 %0Aimport 
+%7B obj as 
 through2
  from 't
@@ -123,16 +123,18 @@
 through2
+ %7D
  from 't
@@ -1084,12 +1084,8 @@
 ugh2
-.obj
 ((fi

fix log helper