如何在 gulp 中运行 bash 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21128812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run bash commands in gulp?
提问by houhr
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!
我想在gulp.watch
函数末尾添加一些bash命令来加快我的开发速度。所以,我想知道是否有可能。谢谢!
采纳答案by Erik
Use https://www.npmjs.org/package/gulp-shell.
使用https://www.npmjs.org/package/gulp-shell。
A handy command line interface for gulp
一个方便的 gulp 命令行界面
回答by Mangled Deutz
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.htmland spoofing the result into fancy-log
这里没有真正“吞咽” - 主要使用子进程http://nodejs.org/api/child_process.html并将结果欺骗到fancy-log
回答by David Lemon
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.
它不使用节点流和吞咽管道,但它会完成工作。