Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run an external file within Gulp?

At the moment I'm using the "gulp-run" plugin to run a .bat file. That plugin has now been deprecated and I'm looking for the best way to execute the .bat now.

Current code:

var gulp = require('gulp');
var run = require('gulp-run');

module.exports = function() {

    run('c:/xxx/xxx/runme.bat').exec();

};

Solution as per @cmrn suggestion:

var exec = require('child_process').exec;
var batchLocation = 'c:/xxx/xxx/runme.bat';
gulp.task('task', function (cb) {
  exec(batchLocation, function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})
like image 487
Tim Avatar asked Dec 01 '15 02:12

Tim


People also ask

What does pipe do in gulp?

pipe() method attaches a Writable stream to the readable, causing it to switch automatically into flowing mode and push all of its data to the attached Writable.

How do you write a gulp task?

If you just want a task that runs some function, then just do that: gulp. task('my-custom-task', function () { myCustomFunction('foo', 'bar'); });

What was used by the gulp file js or JSON?

The gulp tasks are writted in javascript file called gulpfile. js which will be executed by Gulp tool. Gulp can be installed by using npm (Node Package Manager) tool either machine wide or locally to the application.


1 Answers

If you need to run the script as part of a gulp stream (i.e. in pipe()) you can use gulp-exec. If not, you can just use child_process.exec as described in the gulp-exec README, copied below.

var exec = require('child_process').exec;

gulp.task('task', function (cb) {
  exec('ping localhost', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})
like image 99
cmrn Avatar answered Oct 14 '22 03:10

cmrn