我希望在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中。
gulp-exec
存在一些重叠--并不是说这两个插件有任何问题。https://github.com/sun-zheng-an/gulp-shell/issues/1 - Erikchild_process
。这就是为什么那些插件被列入黑名单的原因。 - demisx
var child = require('child_process');
var gulp = require('gulp');
gulp.task('launch-ls',function(done) {
child.spawn('ls', [ '-la'], { stdio: 'inherit' });
});
done
从未被调用。如果其他脚本在它之前或之后应该运行,那么这难道不会打乱脚本的执行顺序吗? - undefined
child_process.exec
达成了我的目标。感谢你提供的 Node 资源! - houhr