Я хочу добавить несколько команд bash в конце функции gulp.watch
, чтобы ускорить скорость разработки. Поэтому мне интересно, возможно ли это. Спасибо!
Как запустить команды bash в gulp?
Ответ 1
Используйте https://www.npmjs.org/package/gulp-shell.
Удобный интерфейс командной строки для gulp
Ответ 2
Я бы пошел с:
var spawn = require('child_process').spawn;
var gutil = require('gulp-util');
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;
gutil.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
gutil.log(gutil.colors.red(data));
gutil.beep();
});
child.on('close', function(code) {
gutil.log("Done with exit code", code);
gutil.log("You access complete stdout and stderr from here"); // stdout, stderr
});
});
});
Ничего действительно "gulp" здесь - в основном, используя дочерние процессы http://nodejs.org/api/child_process.html и подделывая результат в gulp -util log
Ответ 3
Простейшее решение так же просто, как:
var child = require('child_process');
var gulp = require('gulp');
gulp.task('launch-ls',function(done) {
child.spawn('ls', [ '-la'], { stdio: 'inherit' });
});
Он не использует потоки node и gulp, но он будет выполнять эту работу.