How to run bash commands in gulp?

你离开我真会死。 提交于 2019-12-18 10:15:11

问题


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!


回答1:


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

A handy command line interface for gulp




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/21128812/how-to-run-bash-commands-in-gulp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!