将以下npm脚本转换为gulp任务

时间:2017-12-13 07:57:23

标签: javascript node.js npm gulp

我有以下npm脚本我想转换为gulp任务

  "scripts": {
    "lint": "eslint .",
    "start": "npm run build:sdk && node .",
    "posttest": "npm run lint && nsp check",
    "build:sdk": "./node_modules/.bin/lb-sdk server/server.js ./client/src/app/shared/sdk"
  },

现在我希望gulp任务具有与npm start相同的功能。但我无法完成任务,因为它将运行npm run build:sdk然后运行节点。

这是我到目前为止所做的。

gulp.task('default', function() {
  gulp.run('sdk');
  gulp.run('server');
  gulp.watch(['./common/models/*.js'], function() {
    gulp.run('server');
  });
});

gulp.task('server', function() {
  if (node) node.kill();
  node = spawn('node', ['server/server.js'], {stdio: 'inherit'});
  node.on('close', function(code) {
    if (code === 8) {
      gulp.log('Error detected, waiting for changes...');
    }
  });
});

gulp.task('sdk', function() {
  spawn('./node_modules/.bin/lb-sdk server/server.js ./client/src/app/shared/sdk');
});

堆栈跟踪

Error: spawn ./node_modules/.bin/lb-sdk server/server.js ./client/src/app/shared/sdk ENOENT
    at exports._errnoException (util.js:1050:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:367:16)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
    at Module.runMain (module.js:607:11)
    at run (bootstrap_node.js:423:7)
    at startup (bootstrap_node.js:147:9)
    at bootstrap_node.js:538:3

1 个答案:

答案 0 :(得分:2)

您的代码中存在两个问题。首先,您将生成的进程传递给数组作为数组。

gulp.task('sdk', function() {
      spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q']);
});

其次,您正在使用gulp.run('sdk');,这是已删除的。当您解决上述问题时,您必须在控制台中看到此信息。为此,您必须将依赖项传递到default任务,并传递到gulp.watch()

gulp.task('default', ['sdk', 'server'] , function() {
  gulp.watch(['./common/models/*.js'], ['server']);
});

enter image description here

完整的Gulp文件

var gulp = require('gulp');
var {spawn} = require('child_process');

var node = null;

gulp.task('default', ['sdk', 'server'], function() {
  gulp.watch(['./common/models/*.js'], ['server']);
});

gulp.task('server', function() {
  if (node) node.kill();
  node = spawn('node', ['server/server.js'], {stdio: 'inherit'});
  node.on('close', function(code) {
    if (code === 8) {
      gulp.log('Error detected, waiting for changes...');
    }
  });
});

gulp.task('sdk', function() {
  spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q']);
});