Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Gulp watch task when I type npm start

I have a gulp.js file that includes:

gulp.task('default', ['watch']);

Which starts up the watch task

gulp.task('watch', function(){
  gulp.watch(productionScripts, ['autoConcat']);
});

Then on any saved changes to files in productionScripts, the watch task will concat the files.

What I would like to do, is in my package.json, I would like to spool up this watch when I type npm start (this already starts my node server).

package.json

    "start": "node server.js",

UPDATE--------

Ben(b3nj4m.com), I tried what you stated. The watch and server start up. However, everything runs twice (probably due to the editor, not related), but I do lose my server log when I start it up with gulp.

[15:31:18] Starting 'autoConcat'...
[15:31:18] Finished 'autoConcat' after 147 ms
[15:31:19] Starting 'autoConcat'...
[15:31:19] Finished 'autoConcat' after 138 ms
[15:31:20] Starting 'autoConcat'...
[15:31:20] Finished 'autoConcat' after 127 ms
[15:31:23] Starting 'autoConcat'...

It's like there is a loop between the server restarting on a change, and the concatenated file changing.

like image 273
SoEzPz Avatar asked Jan 19 '15 17:01

SoEzPz


People also ask

How do I run a gulp task?

in the Before launch area and choose Run Gulp task from the list. In the Gulp task dialog that opens, specify the Gulpfile. js where the required task is defined, select the task to execute, and specify the arguments to pass to the Gulp tool. Specify the location of the Node.

What is gulp watch command?

The watch() API connects globs to tasks using a file system watcher. It watches for changes to files that match the globs and executes the task when a change occurs. If the task doesn't signal Async Completion, it will never be run a second time.


2 Answers

You could concatenate multiple tasks in your start in package.json using the package concurrently as such:

{
  "start": "concurrent \"node server.js\" \"gulp\" "
}

And run npm start from your terminal. This would execute all statements within start.

For references: https://www.npmjs.com/package/concurrently

EDIT:

As pointed out by @Josh in the comments, the CLI name now matches the package name. Hence, you could write the script as:

{
   "start": "concurrently \"node server.js\" \"gulp\" "
}
like image 153
nashcheez Avatar answered Oct 04 '22 03:10

nashcheez


You could run your server from your gulpfile:

var child = require('child_process');
var fs = require('fs');

gulp.task('default', ['server', 'watch']);

gulp.task('server', function() {
  var server = child.spawn('node', ['server.js']);
  var log = fs.createWriteStream('server.log', {flags: 'a'});
  server.stdout.pipe(log);
  server.stderr.pipe(log);
});

gulp.task('watch', function(){
  gulp.watch(productionScripts, ['autoConcat']);
});

Then change your npm start definition to look like:

"scripts": {
  "start": "gulp"
}
like image 24
Ben Avatar answered Oct 04 '22 04:10

Ben