50

I want to add some bash commands at the end of gulp.watch function to accelerate my development speed. So, I am wondering if it is possible. Thanks!

houhr
  • 579
  • 1
  • 5
  • 9

3 Answers3

77

I would go with:

var spawn = require('child_process').spawn;
var fancyLog = require('fancy-log');
var beeper = require('beeper');

gulp.task('default', function(){

    gulp.watch('*.js', function(e) {
        // Do run some gulp tasks here
        // ...

        // Finally execute your script below - here "ls -lA"
        var child = spawn("ls", ["-lA"], {cwd: process.cwd()}),
            stdout = '',
            stderr = '';

        child.stdout.setEncoding('utf8');

        child.stdout.on('data', function (data) {
            stdout += data;
            fancyLog(data);
        });

        child.stderr.setEncoding('utf8');
        child.stderr.on('data', function (data) {
            stderr += data;
            fancyLog.error(data));
            beeper();
        });

        child.on('close', function(code) {
            fancyLog("Done with exit code", code);
            fancyLog("You access complete stdout and stderr from here"); // stdout, stderr
        });


    });
});

Nothing really "gulp" in here - mainly using child processes http://nodejs.org/api/child_process.html and spoofing the result into fancy-log

nirazul
  • 3,928
  • 4
  • 26
  • 46
Mangled Deutz
  • 11,384
  • 6
  • 39
  • 35
  • 3
    After doing some research on child processes, I'v achieved my goal by using `child_process.exec` at last. Thanks for your node resource! – houhr Jan 15 '14 at 14:10
  • 27
    Thank you for actually addressing his question instead of responding with a link to a plugin. – Andy Ferra Oct 19 '14 at 03:29
  • 8
    While I can understand how you might not want to use a plugin, I believe my answer still addressed the question. – Erik Dec 24 '14 at 23:43
  • I had the same question and your answer better met my needs than a re-engineering of this proverbial wheel (not to detract anything from Mangled's answer). – Adrian Günter Aug 03 '15 at 06:14
45

Use https://www.npmjs.org/package/gulp-shell.

A handy command line interface for gulp

Erik
  • 7,479
  • 8
  • 62
  • 99
  • 6
    While there are alternative ways (like gulp-exec and gulp-spawn), the gulp-shell prints the output from the command immediately - which is often wanted. – jmu Aug 27 '14 at 05:29
  • 4
    There is now also [`gulp-run`](https://www.npmjs.com/package/gulp-run), which has a cleaner and more intuitive interface, IMO. – Adrian Günter Aug 03 '15 at 07:44
  • 14
    gulp-shell is blacklisted, see https://github.com/gulpjs/plugins/blob/master/src/blackList.json. – Nico Schlömer Oct 26 '15 at 20:37
  • 2
    It is "blacklisted" because there is some overlap with `gulp-exec` -- not that there is anything wrong with either plugin. https://github.com/sun-zheng-an/gulp-shell/issues/1 – Erik Oct 26 '15 at 20:38
  • gulp-run is also blacklisted. Taken reference from same link so option are gulp-exec or gulp-spawn only. – Mr. Noddy Mar 04 '16 at 03:56
  • 4
    One doesn't need any plugins to run bash commands from gulp. Just use node's `child_process` directly. This why those plugins get blacklisted. – demisx Mar 19 '16 at 02:10
  • 1
    Regarding the comment about gulp-shell printing the output from the command immediately: `child_process.spawn` also prints out the output immediately, **if** you add the event handlers as shown in the answer from @Mangled Deutz. – Sunil D. Jun 15 '16 at 04:40
1

The simplest solution is as easy as:

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

gulp.task('launch-ls',function(done) {
   child.spawn('ls', [ '-la'], { stdio: 'inherit' });
});

It doesn't use node streams and gulp pipes but it will do the work.

David Lemon
  • 1,560
  • 10
  • 21