如何在gulp中运行bash命令?

问题描述 投票:0回答:3

我想在

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

bash gulp
3个回答
77
投票

我会选择:

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并将结果欺骗到fancy-log


45
投票

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

一个方便的 gulp 命令行界面


1
投票

最简单的解决方案如下:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.