【发布时间】:2014-01-15 03:52:44
【问题描述】:
我想在gulp.watch函数的末尾添加一些bash命令来加快我的开发速度。所以,我想知道这是否可能。谢谢!
【问题讨论】:
我想在gulp.watch函数的末尾添加一些bash命令来加快我的开发速度。所以,我想知道这是否可能。谢谢!
【问题讨论】:
我会选择:
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
});
});
});
这里没有什么真正的“gulp”——主要是使用子进程http://nodejs.org/api/child_process.html并将结果欺骗到fancy-log中
【讨论】:
child_process.exec 实现了我的目标。感谢您的节点资源!
使用https://www.npmjs.org/package/gulp-shell。
一个方便的 gulp 命令行界面
【讨论】:
gulp-run,界面更简洁直观,IMO。
gulp-exec 有一些重叠——并不是说这两个插件都有什么问题。 github.com/sun-zheng-an/gulp-shell/issues/1
child_process即可。这就是这些插件被列入黑名单的原因。
最简单的解决方案很简单:
var child = require('child_process');
var gulp = require('gulp');
gulp.task('launch-ls',function(done) {
child.spawn('ls', [ '-la'], { stdio: 'inherit' });
});
它不使用节点流和 gulp 管道,但它会完成工作。
【讨论】: