用babel然后pm2重新启动gulp手表

Oke*_*oke 1 javascript watch gulp pm2 babeljs

嘿伙计们,我完全被这个人困住了.

基本上我想在我的本地开发人员能够看到我的src js文件文件并用babel转换它们并将它们输出到我的dist文件夹然后在完成之后有pm2重启节点加载最新的更改.

我遇到的问题是我不能为我的生活弄清楚如何添加一个回调来监视,以便重启pm2的调用只发生在babel完成魔法转换文件之后.

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
});
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

soy*_*uka 6

首先,你没有看正确的方式.然后,你应该把事情分开.我就是这样做的:

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'])
})
Run Code Online (Sandbox Code Playgroud)

如果你启动gulp watch,它会观察paths.babel并且在更改时执行两个任务(babel,pm2).如果您只执行gulp(或gulp babel在此示例中),它将启动相应的任务.你也可以发射gulp pm2.

Ressources: