1

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
INFOSYS
  • 1,465
  • 9
  • 23
  • 50
  • 1
    Try passing arguments as an array: `spawn('./node_modules/.bin/lb-sdk', ['server/server.js ./client/src/app/shared/sdk']);` – ninhjs.dev Dec 13 '17 at 08:07
  • actually i need to run both these process one after another first sdk and then server – INFOSYS Dec 13 '17 at 08:42
  • 1
    I'm saying that your `spawn` statement call is wrong. That is why you got `Error: spawn ./node_modules/.bin/lb-sdk server/server.js ./client/src/app/shared/sdk ENOENT`. The arguments should be passed as an array. – ninhjs.dev Dec 13 '17 at 09:50

1 Answers1

2

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']);
});
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25
  • actually i need to run both these process one after another first sdk and then server – INFOSYS Dec 13 '17 at 08:42
  • Yes, they are running in the order as per requirement. Please see screenshot attached. Also I've attached full gulpfile in answer. – Vipin Kumar Dec 13 '17 at 08:44
  • *address in use error* is comming because some other process is using your port number. Kill other process and start again. – Vipin Kumar Dec 13 '17 at 08:45
  • the processing now are running but the sdk has no effect it is not creating the files it is supposed to ? – INFOSYS Dec 13 '17 at 11:25
  • what kind of files your are expecting and what is it generating? – Vipin Kumar Dec 13 '17 at 11:26
  • the problem is that the sdk commands prompts a change Y/n in the console i always need to give Y is there a way to do it that is the reason the files are not getting developed – INFOSYS Dec 13 '17 at 11:28
  • add `-q` arguments at the end of `lb-sdk` command. Updated the answer. – Vipin Kumar Dec 13 '17 at 11:32
  • Please see updated answer. And you can check the documentation of `lb-sdk` by typing `./node_modules/.bin/lb-sdk` in your terminal. – Vipin Kumar Dec 13 '17 at 11:34