简体   繁体   English

用babel gulp监视然后pm2重新启动

[英]gulp watch with babel then pm2 restart

\\Hey guys I'm totally stuck with this one. \\嘿,我完全被这个困扰。

Basically I want on my local dev to be able to have gulp watch my src js files files and transform them with babel and output them to my dist folder and then after that's done have pm2 restart node to load the latest changes. 基本上,我希望本地开发人员能够让gulp看我的src js文件文件,并用babel对其进行转换,然后将它们输出到我的dist文件夹中,然后完成后,请让pm2 restart节点加载最新更改。

The problem I'm having is I can't for the life of me figure out how to add a callback to watch so that the call to restart pm2 only happens after babel has done its magic transforming the files. 我遇到的问题是我一辈子都无法弄清楚如何添加一个要监视的回调,以便仅在babel完成魔术转换文件后才发生重新启动pm2的调用。

 var gulp = require("gulp"); var babel = require("gulp-babel"); var pm2 = require("pm2"); var watch = require("gulp-watch"); var plumber = require("gulp-plumber"); var SRC = "src/**/*js"; var DIST = "dist/"; function restartPM2() { //restart pm2 code in here } gulp.task("default", function () { return gulp.src(SRC) .pipe(watch(SRC)) .pipe(plumber()) .pipe(babel()) .pipe(plumber.stop()) .pipe(gulp.dest(DIST)); // somewhere in here need a call back after babel has transformed // the code and saved it to dist/ to then call restartPM2 }); 

Any help would be greatly appreciated! 任何帮助将不胜感激!

First, you're not watching the right way. 首先,您没有以正确的方式观看。 Then, you should keep things separated. 然后,您应该将事情分开。 That's how I'd do: 那就是我要做的:

var paths = {
  babel: './somedir'
}

//basic babel task
gulp.task('babel', function() {
  return gulp.src(paths.babel)
  .pipe(babel())
  .pipe(gulp.dest('./'))
})

//see below for some links about programmatic pm2
gulp.task('pm2', function(cb) {
  pm2.connect(function() {
    pm2.restart('echo', function() { 
      return cb()
    })
  })
})

gulp.task('default', ['babel']) //I don't restart pm2 with the default task but you could

//the watch task
gulp.task('watch', function() {
  //their could be more watchers here ofc
  gulp.watch(paths.babel, ['babel', 'pm2'])
})

If you launch gulp watch , it'll watch the paths.babel and, on change, execute both tasks (babel, pm2). 如果启动paths.babel gulp watch ,它将监视paths.babel并在更改时执行这两个任务(babel,pm2)。 If you only execute gulp (or gulp babel in this example), it'll launch the appropriate task. 如果仅执行gulp (或本例中的gulp babel ),它将启动适当的任务。 You'd be able to launch gulp pm2 too. 您也可以启动gulp pm2

Ressources: 资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM