import gulp from 'gulp'
import babel from 'gulp-babel'
import watch from 'gulp-watch'
import log from 'fancy-log'
import fs from 'fs'
import path from 'path'
import { exec } from 'child_process'
import mjml2html from 'mjml'
import { registerComponent } from 'mjml-core'
import MjLayout from './components/MjLayout'
import MjImageText from './components/MjImageText'
import MjBasicComponent from './components/MjBasicComponent'
registerComponent(MjBasicComponent)
registerComponent(MjImageText)
registerComponent(MjLayout)

// Import and register your components here

const compile = () => {
  gulp.src(path.normalize('components/**.js'))
    .pipe(babel())
    .on('error', log)
    .pipe(gulp.dest('lib'))
    .on('end', () => {
      fs.readFile(path.normalize('./index.mjml'), 'utf8', (err, data) => {
        if (err) throw err
        const result = mjml2html(data)
        fs.writeFileSync(path.normalize('./index.html'), result.html)
      })
    })
}

gulp.task('build', compile)

gulp.task('watch', () => {
  compile()
  return watch([
    path.normalize('components/*.js'),
    path.normalize('index.mjml'),
  ], compile)
})

@@ -155,45 +155,8 @@
 th'%0A
-import %7B exec %7D from 'child_process'%0A
 impo
@@ -230,289 +230,313 @@
 re'%0A
-import MjLayout from './components/MjLayout'%0Aimport MjImageText from './components/MjImageText'%0Aimport MjBasicComponent from './components/MjBasicComponent'%0AregisterComponent(MjBasicComponent
+%0Aconst walkSync = (dir, filelist = %5B%5D) =%3E %7B%0A  fs.readdirSync(dir).forEach(file =%3E %7B%0A    filelist = fs.statSync(path.join(dir, file)).isDirectory()%0A      ? walkSync(path.join(dir, file), filelist)%0A      : filelist.concat(path.join(dir, file))%0A  %7D
 )%0A
+ 
 re
-gisterComponent(MjImageText)%0AregisterComponent(MjLayout)%0A%0A// Import and register your 
+turn filelist%0A%7D%0A%0Aconst watchedComponents = walkSync('./
 comp
@@ -545,13 +545,10 @@
 ents
- here
+')
 %0A%0Aco
@@ -609,16 +609,18 @@
 nents/**
+/*
 .js'))%0A 
@@ -706,24 +706,262 @@
 d', () =%3E %7B%0A
+      watchedComponents.forEach(compPath =%3E %7B%0A        const fullPath = path.join(process.cwd(), compPath.replace(/%5Ecomponents/, 'lib'))%0A        delete require.cache%5BfullPath%5D%0A        registerComponent(require(fullPath).default)%0A      %7D)%0A%0A
       fs.rea
@@ -1295,16 +1295,19 @@
 onents/*
+*/*
 .js'),%0A 

fix #14 watch on components, remove the need to register them manually