如何在gulp中运行bash命令?

时间:2014-01-15 03:52:45

标签: bash gulp

我想在gulp.watch函数末尾添加一些bash命令,以加快我的开发速度。所以,我想知道是否有可能。谢谢!

3 个答案:

答案 0 :(得分:76)

我会选择:

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


    });
});

这里没有什么真正的“吞咽” - 主要是使用子进程http://nodejs.org/api/child_process.html并将结果欺骗成花式日志

答案 1 :(得分:44)

使用https://www.npmjs.org/package/gulp-shell

  

gulp

的方便命令行界面

答案 2 :(得分:0)

最简单的解决方案就像:

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

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

它不会使用节点流和gulp管道,但它会完成工作。