将以下npm脚本转换为吞噬任务 [英] Convert the following npm script to gulp task

查看:148
本文介绍了将以下npm脚本转换为吞噬任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following npm script i want to convert to gulp task

  "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然后节点。

Now i want a gulp task to have the same functionality as npm start . but i am not able to club the tasks as it will run the npm run build:sdk and then node .

这是我迄今为止所做的。

Here is what i have done so far .

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');
});

堆栈跟踪

Stack trace

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


推荐答案

代码中有两个问题。首先,您将参数传递给派生进程作为数组。

There are two problems in your code. First, You have pass arguments to spawned process as an array.

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

其次,您正在使用 gulp.run('sdk'); / code>,这是depricated。解决上述问题时,您必须在控制台中看到此信息。为此,您必须将依赖关系传递给您的默认任务,并且也传递给 gulp.watch()

Second, you are using gulp.run('sdk');, which is depricated. You must be seeing this in your console, when you fix above problem. For that you have to pass dependecies to your default task and also to the gulp.watch()

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

完整的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']);
});

这篇关于将以下npm脚本转换为吞噬任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆