2014-05-14 40 views
21

也許是有毛病我的做法,但我有以下情況:運行一飲而盡任務從另一個gulpfile.js

  1. 我有一個component-a具有gulpfile。它的一個任務(如構建)構建組件並在dist文件夾中創建組合的js文件
  2. 我有一個component-b,它有一個大文件。它的一個任務(如構建)構建組件,並在dist文件夾中創建一個組合的js文件。
  3. 我有一個使用這兩個組件的項目。這個項目有一個gulpfile以及和它,我想編寫一個任務:
    • 執行從/components/component-a/gulpfile.js
    • 執行任務建立從/組件/成分 - 建立任務b/gulpfile.js
    • concats /components/component-a/dist/build.js和/components/component-b/dist/build.js(我知道如何做到這一點)

我不知道的是如何從/components/component-?/gulpfile.js執行構建任務。甚至有可能,否則我應該處理這種情況呢?

+0

能否請您接受沃爾特·羅曼的答案,因爲這是正確的,工作? – thomaux

回答

17

require('child_process')。spawn;

Node's child_process#spawn module運行不同目錄中的Gulp文件非常簡單。

試着將以下您的需要:

// Use `spawn` to execute shell commands with Node 
const { spawn } = require('child_process') 
const { join } = require('path') 

/* 
    Set the working directory of your current process as 
    the directory where the target Gulpfile exists. 
*/ 
process.chdir(join('tasks', 'foo')) 

// Gulp tasks that will be run. 
const tasks = ['js:uglify', 'js:lint'] 

// Run the `gulp` executable 
const child = spawn('gulp', tasks) 

// Print output from Gulpfile 
child.stdout.on('data', function(data) { 
    if (data) console.log(data.toString()) 
}) 

一飲而盡,突突

雖然使用gulp-chug是去這個問題,it has been blacklisted by gulp's maintainers爲是一種方式......

「太複雜,只是使用吞噬作爲一個球體「

official blacklist美國...

「沒有理由存在,使用要求,所有的模塊或節點的要求」