INFOSYS
INFOSYS

Reputation: 1585

Convert the following npm script to gulp task

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

Upvotes: 1

Views: 590

Answers (1)

Vipin Kumar
Vipin Kumar

Reputation: 6546

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

enter image description here

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

Upvotes: 2

Related Questions