Как сократить/улучшить js код?

169
24 февраля 2022, 14:10

Вечер добрый, помогите пожалуйста сократить/улучшить javascript код.

gulp.task('watch', function() { 
    /* html */ 
    gulp.watch(paths.watch.html, { usePolling: true }, gulp.series('html')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* styles */ 
    gulp.watch(paths.watch.styles, { usePolling: true }, gulp.series('styles')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* script */ 
    gulp.watch(paths.watch.script, { usePolling: true }, gulp.series('script')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            delete cached.caches.script[path.resolve(file)]; 
            remember.forget('script', path.resolve(file)); 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* pngSprites */ 
    gulp.watch(paths.watch.pngSprites, { usePolling: true }, gulp.series('pngSprites')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            delete cached.caches.pngSprites[path.resolve(file)]; 
            remember.forget('pngSprites', path.resolve(file)); 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* svgSprites */ 
    gulp.watch(paths.watch.svgSprites, { usePolling: true }, gulp.series('svgSprites')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            delete cached.caches.svgSprites[path.resolve(file)]; 
            remember.forget('svgSprites', path.resolve(file)); 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* svgSymbols */ 
    gulp.watch(paths.watch.svgSymbols, { usePolling: true }, gulp.series('svgSymbols')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            delete cached.caches.svgSymbols[path.resolve(file)]; 
            remember.forget('svgSymbols', path.resolve(file)); 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* copyImages */ 
    gulp.watch(paths.watch.copyImages, gulp.series('copyImages')).on('all', function(event, file, stats) { 
        if (event === 'change') { 
            console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
        } else if (event === 'add') { 
            console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
        } else if (event === 'unlink') { 
            console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
        } else { 
            console.log('else'); 
        } 
    }); 
    /* copyFonts */ 
    gulp.watch(paths.watch.copyFonts, gulp.series('copyFonts')); 
});

Answer 1

gulp.task('watch', function() { 
  const watchers = { 
    html: './app/templates/**/[^_]*.*', 
    styles: './app/scss/**/[^_]*.{scss,css}', 
    script: './app/js/**/[^_]*.js', 
    pngSprites: './app/img/sprites/pngSprites/*.png', 
    cssSvgSprites: './app/img/sprites/svgSprites/cssSvgSprites/*.svg', 
    stackSvgSprites: './app/img/sprites/svgSprites/stackSvgSprites/*.svg', 
    symbolSvgSprites: './app/img/sprites/svgSprites/symbolSvgSprites/*.svg', 
    copyImages: './app/img/content/**/[^_]*.{jpg,jpeg,png,gif,svg,webp}', 
    copyFonts: './app/fonts/*.{eot,svg,ttf,woff,woff2}', 
  }; 
  for (let key in watchers) { 
    gulp.watch(watchers[key], { 
      usePolling: true 
    }, gulp.series(key)).on('all', function(event, file, stats) { 
      if (event === 'change') { 
        console.log(chalk `{yellow.bold File modified} [{magenta ${file}}] file changed size to {cyan ${stats.size} byte}`); 
      } else if (event === 'add') { 
        console.log(chalk `{green.bold File added} [{magenta ${file}}]`); 
      } else if (event === 'unlink') { 
        if (key === 'script' || key === 'pngSprites' || key === 'cssSvgSprites') { 
          delete cached.caches[`${key}`][path.resolve(file)]; 
          remember.forget(`${key}`, path.resolve(file)); 
        } 
        console.log(chalk `{red.bold File deleted} [{magenta ${file}}]`); 
      } else { 
        console.log('else'); 
      } 
    }); 
  } 
});

READ ALSO
Prototype, call. Откуда берется второй undefined?

Prototype, call. Откуда берется второй undefined?

Изучаю прототипы и добавляю к ним методы [call, bind, apply] и столкнулся с такой фичейНе понимаю откуда берется undefined

89
Обобщенный список List

Обобщенный список List

Как правильно поступить, у меня есть интерфейс(ICommon<T>), есть его несколько реализаций (HelloInt:ICommon<int>, HelloDouble:ICommon<double>)Теперь я хотел...

168