Convert the following npm script to gulp task

非 Y 不嫁゛ 提交于 2019-12-11 10:38:03

问题


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"
  },

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

回答1:


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

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

Complete Gulp File

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


来源:https://stackoverflow.com/questions/47788084/convert-the-following-npm-script-to-gulp-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!