2014-01-15 216 views

回答

45

使用https://www.npmjs.org/package/gulp-shell

一个方便的命令行界面一饮而尽

+6

虽然还有其他方法(如gulp-exec和gulp-spawn),但gulp-shell立即打印命令的输出 - 这是通常需要的。 – jmu

+4

现在还有['gulp-run'](https://www.npmjs.com/package/gulp-run),它有一个更清洁和更直观的界面IMO。 –

+14

gulp-shell被列入黑名单,请参阅https://github.com/gulpjs/plugins/blob/master/src/blackList.json。 –

74

我会去:

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 
     }); 


    }); 
}); 

真的没什么“大口”,在这里 - 主要是利用子进程http://nodejs.org/api/child_process.html和结果欺骗成花式登录

+3

在对子进程进行了一些研究之后,我终于通过使用'child_process.exec'实现了我的目标。感谢您的节点资源! – houhr

+26

谢谢你真正解决他的问题,而不是回应一个插件的链接。 –

+7

尽管我可以理解你可能不想使用插件,但我相信我的答案仍然解决了这个问题。 – Erik

0

最简单的办法是那么容易,因为:

var child = require('child_process'); 
var gulp = require('gulp'); 

gulp.task('launch-ls',function(done) { 
    child.spawn('ls', [ '-la'], { stdio: 'inherit' }); 
}); 

它不使用节点流和咽管,但它会做的工作。