/* eslint global-require: ["off", { allow: ["warn"] }] */
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint import/no-dynamic-require: ["off", { allow: ["warn"] }] */

const browserSync = require('browser-sync').create();
const watch = require('node-watch');

// @ToDo: find a more generic approach
const rebuildHTML = require('./html.build').rebuild;
const rebuildCSS = require('./css.build').rebuild;
const rebuildJS = require('./javascript.build').rebuild;
const rebuildIMG = require('./image.build').rebuild;
const rebuildSTATIC = require('./static.build').rebuild;
const logger = require('./utils/logger');

const srcFolder = 'src';
const distFolder = 'app';
const tasks = ['html', 'css', 'javascript', 'image', 'static'];

function startBrowserSync() {
  logger.start('Browsersync');

  browserSync.init({
    server: { baseDir: distFolder },
    open: 'local',
  });
}

function startWatchTask() {
  // @ToDo: find a more generic approach

  // watch(srcFolder, {
  //   recursive: true,
  //   filter: /\.scss$/,
  // }, async (event, name) => {
  //   await rebuildCSS(event, name);
  //   browserSync.reload();
  // });

  browserSync.watch('src/**/*.scss', async (event, file) => {
    await rebuildCSS(event, file);
    browserSync.reload();
  });

  // watch(srcFolder, {
  //   recursive: true,
  //   filter: /\.pug$/,
  // }, async (event, name) => {
  //   await rebuildHTML(event, name);
  //   browserSync.reload();
  // });

  browserSync.watch('src/**/*.pug', async (event, file) => {
    await rebuildHTML(event, file);
    browserSync.reload();
  });

  // watch(srcFolder, {
  //   recursive: true,
  //   filter: /\.js$/,
  // }, async (event, name) => {
  //   await rebuildJS(event, name);
  //   browserSync.reload();
  // });

  browserSync.watch('src/**/*.js', async (event, file) => {
    await rebuildJS(event, file);
    browserSync.reload();
  });

  watch(srcFolder, {
    recursive: true,
    filter: /\.jpg$|\.png$|\.svg$|\.ico$/,
  }, async (event, name) => {
    await rebuildIMG(event, name);
    browserSync.reload();
  });

  watch(srcFolder, {
    recursive: true,
    filter: /\.eot$|\.woff$|\.woff2$|\.ttf$|\.json$/,
  }, async (event, name) => {
    await rebuildSTATIC(event, name);
    browserSync.reload();
  });
}

(async () => {
  await Promise.all(tasks.map(async (task) => {
    const startTime = new Date().getTime();
    logger.start(task);

    const { run } = require(`./${task}.build`);
    await run();

    const time = new Date().getTime() - startTime;
    logger.finish(task, time);
  }));

  startBrowserSync();
  startWatchTask();
})();

@@ -300,319 +300,8 @@
 h');
-%0A%0A// @ToDo: find a more generic approach%0Aconst rebuildHTML = require('./html.build').rebuild;%0Aconst rebuildCSS = require('./css.build').rebuild;%0Aconst rebuildJS = require('./javascript.build').rebuild;%0Aconst rebuildIMG = require('./image.build').rebuild;%0Aconst rebuildSTATIC = require('./static.build').rebuild;
 %0Acon
@@ -638,366 +638,289 @@
 %7B%0A  
-// @ToDo: find a more generic approach%0A%0A  // watch(srcFolder, %7B%0A  //   recursive: true,%0A  //   filter: /%5C.scss$/,%0A  // %7D, async (event, name) =%3E %7B%0A  //   await rebuildCSS(event, name);%0A  //   browserSync.reload();%0A  // %7D);%0A%0A  browserSync.watch('src/**/*.scss', async (event, file) =%3E %7B%0A    await rebuildCSS(event, file);%0A    browserSync.reload();%0A  %7D)
+const rebuildHTML = require('./html.build').rebuild;%0A  const rebuildCSS = require('./css.build').rebuild;%0A  const rebuildJS = require('./javascript.build').rebuild;%0A  const rebuildIMG = require('./image.build').rebuild;%0A  const rebuildSTATIC = require('./static.build').rebuild
 ;%0A%0A 
- //
  wat
@@ -926,39 +926,32 @@
 tch(srcFolder, %7B
-%0A  //  
  recursive: true
@@ -954,207 +954,70 @@
 true
-,%0A  //   filter: /%5C.pug$/,%0A  // %7D, async (event, name) =%3E %7B%0A  //   await rebuildHTML(event, name);%0A  //   browserSync.reload();%0A  // %7D);%0A%0A  browserSync.watch('src/**/*.pug', async (event, file) =%3E
+ %7D, async (event, name) =%3E %7B%0A    if (/%5C.pug$/.test(name))
  %7B%0A
+  
     
@@ -1045,157 +1045,57 @@
 nt, 
-file);%0A    browserSync.reload();%0A  %7D);%0A%0A  // watch(srcFolder, %7B%0A  //   recursive: true,%0A  //   filter: /%5C.js$/,%0A  // %7D, async (event, 
+name);%0A    %7D else if (/%5C.scss$/.test(
 name)
- =%3E
+)
  %7B%0A  
-//
+ 
    a
@@ -1098,33 +1098,34 @@
    await rebuild
-J
+CS
 S(event, name);%0A
@@ -1130,109 +1130,49 @@
 ;%0A  
-//   browserSync.reload();%0A  // %7D);%0A%0A  browserSync.watch('src/**/*.js', async (event, file) =%3E
+  %7D else if (/%5C.js$/.test(name))
  %7B%0A    
+  
 awai
@@ -1194,19 +1194,19 @@
 nt, 
-fil
+nam
 e);%0A    
 brow
@@ -1205,91 +1205,19 @@
     
-browserSync.reload();%0A  %7D);%0A%0A  watch(srcFolder, %7B%0A    recursive: true,%0A    filter: 
+%7D else if (
 /%5C.j
@@ -1245,40 +1245,25 @@
 co$/
-,%0A  %7D, async (event, 
+.test(
 name)
- =%3E
+)
  %7B%0A
+  
     
@@ -1301,91 +1301,19 @@
     
-browserSync.reload();%0A  %7D);%0A%0A  watch(srcFolder, %7B%0A    recursive: true,%0A    filter: 
+%7D else if (
 /%5C.e
@@ -1352,40 +1352,25 @@
 on$/
-,%0A  %7D, async (event, 
+.test(
 name)
- =%3E
+)
  %7B%0A
+  
     
@@ -1399,24 +1399,31 @@
 ent, name);%0A
+    %7D%0A%0A
     browserS

simplify watch task