2013-07-11 61 views
54

是否可以同時運行兩個監視任務?如何同時運行兩個grunt監視任務

我明白,我可以有任意數量我想裏面觀看設置,只需啓動咕嚕手錶的任務,它會看所有的人,像這樣的

... 
watch: { 
    A: { 
     files: "js/dev/**/*.coffee", 
     tasks: ["coffee", "requirejs"] 
    }, 
    B: { 
     files: "js/dev/**/*.coffee", 
     tasks: ["coffee"] 
    }, 
    C: { 
     files: "js/dev/**/*.html", 
     tasks: ["copy"] 
    } 
} 
... 

...但我不需要這個。我只想爲開發和生產製定不同的任務。如您所知,A(產品)與(開發)之間的唯一區別是縮小與拼接。我不需要同時啓動AB任務。

首先我來到這個想法

grunt.registerTask("prod", ["watch:A", "watch:C"]); 
grunt.registerTask("dev", ["watch:B", "watch:C"]); 

但這並沒有工作。只是第一個觀看任務正在工作(C從來沒有工作)。有可能做我想做的事嗎?

回答

71

我使用grunt-concurrent作品中:

concurrent: { 
    options: { 
    logConcurrentOutput: true 
    }, 
    prod: { 
    tasks: ["watch:A", "watch:C"] 
    }, 
    dev: { 
    tasks: ["watch:B", "watch:C"] 
    } 
} 

然後:

grunt.registerTask("prod", ["concurrent:prod"]); 
grunt.registerTask("dev", ["concurrent:dev"]); 
+3

正是,謝謝! –

+7

這不適合我。當有兩個手錶同時運行時,我遇到了'致命錯誤:端口35729已被另一個進程使用'。任何線索? – jmcollin92

+1

最好的和唯一的工作解決方案在那裏:https://npmjs.org/package/grunt-focus – jmcollin92

18

編輯:並發現在有一個logConcurrentOutput選擇!更多的信息在這裏:https://github.com/sindresorhus/grunt-concurrent#logconcurrentoutput

手錶是一個奇怪的併發但阻塞任務,所以您必須具有創造性才能獲得類似多任務功能的功能。

併發丟失了所有來自手錶任務的輸出,這並不理想。

嘗試動態地自定義任務編寫的配置對象:

grunt.registerTask('watch:test', function() { 
    // Configuration for watch:test tasks. 
    var config = { 
    options: { 
     interrupt: true 
    }, 
    unit: { 
     files: [ 
     'test/unit/**/*.spec.coffee' 
     ], 
     tasks: ['karma:unit'] 
    }, 
    integration: { 
     files: [ 
     'test/integration/**/*.rb', 
     '.tmp/scripts/**/*.js' 
     ], 
     tasks: ['exec:rspec'] 
    } 
    }; 

    grunt.config('watch', config); 
    grunt.task.run('watch'); 
}); 
+0

如果您不想使用其他插件,非常好的解決方案。 – jmav

+0

這就是解決方案。謝謝。 –

+0

「併發失去監視任務的所有輸出」:Concurrent有一個'logConcurrentOutput'選項,可以防止這一點。 – hashchange

10

最好的和唯一的工作解決方案是存在的:https://npmjs.org/package/grunt-focus 添加這個插件,然後:

focus: { 
      sources: { 
       include: ['js', 'html', 'css', 'grunt'] 
      }, 
      testu: { 
       include: ['js', 'html', 'css', 'testu', 'grunt'] 
      }, 
      testi: { 
       include: ['js', 'html', 'css', 'testu', 'testi', 'grunt'] 
      } 
     }, 
     watch: { 
      js: { 
       files: paths.js, 
       tasks: ['jshint'], 
       options: { 
        livereload: true 
       } 
      }, 
      html: { 
       files: paths.html, 
       options: { 
        livereload: true 
       } 
      }, 
      css: { 
       files: paths.css, 
       tasks: ['csslint'], 
       options: { 
        livereload: true 
       } 
      }, 
      testu: { 
       files: ['test/**/*.js', 'test/**/*.css'], 
       tasks: ['mochaTest'], 
       options: {} 
      }, 
      testi: { 
       files: ['test/**/*.js', 'test/**/*.css'], 
       tasks: ['exec:cleanTestDB', 'protractor_webdriver', 'protractor'], 
       options: {} 
      }, 
      grunt: { 
       files: ['Gruntfile.js', 'server/config/env/*.js'], 
       options: { 
        reload: true 
       } 
      } 
     } 

然後你使用重點:來源或重點:testu作爲您的方便。

JM。

+3

grunt-concurrent不適用於同一端口有多個livereload的手錶。這個伎倆,謝謝! –

+0

'grunt-focus'很棒,因爲你可以定義除了一個(或某些)任務之外的其他所有任務,像這樣:'focus:{dev:{exclude:['test']}}'這對我來說很棒。 – Oliver

+0

爲我完美工作,謝謝! – Tom

7

grunt-concurrent或grunt-focus都是很好的解決方案,但它們都打破livereload的功能。

我的解決方案是動態組合手錶配置,假設您不會同時運行兩種配置。

你可以做這樣的事情

grunt.config.merge({ 
 
    watch: { 
 
    options: { 
 
     livereload: true 
 
    }, 
 
    C: { 
 
     files: "js/dev/**/*.html", 
 
     tasks: ["copy"] 
 
    } 
 
    } 
 
}); 
 

 
grunt.registerTask('watch-forProd', function() { 
 
    grunt.config.merge({ 
 
    watch: { 
 
     A: { 
 
     files: "js/dev/**/*.coffee", 
 
     tasks: ["coffee", "requirejs"] 
 
     } 
 
    } 
 
    }); 
 

 
    grunt.task.run('watch'); 
 
}); 
 

 
grunt.registerTask('watch-forDev', function() { 
 
    grunt.config.merge({ 
 
    watch: { 
 
     B: { 
 
     files: "js/dev/**/*.coffee", 
 
     tasks: ["coffee"] 
 
     } 
 
    } 
 
    }); 
 

 
    grunt.task.run('watch'); 
 
}); 
 

 
grunt.registerTask("prod", ["watch-forProd"]); 
 
grunt.registerTask("dev", ["watch-forDev"]);

+0

其實grunt-foces不會破壞livereload。您必須在每個手錶目標中包含「livereload」選項。 –

0

我知道這並不直接回答這個問題,但我的解決辦法是使用現在咕嘟咕嘟而不是步兵。 使用Gulp代碼,不僅可以配置。所以你更自由地做你想做的事情。

JM。

+0

如果你喜歡寫所有這些「服務器」實用工具,那麼吞下就好了。我們做了一個實驗,用Gulp將我們的Grunt功能轉換爲一週,而我們無法完成任務。我仍然在籬笆的Grunt一側。 – augurone

+2

這讓我不到一天的時間就完成了一口氣遷移。非常容易理解,全功能插件噸,我不回去。 – jmcollin92

+0

引發你的實驗比我們的更好。我沒有親自嘗試這樣做,也許這對我來說會更容易。 – augurone

-1

併發工作正常,我

concurrent: { 
      options: { 
       logConcurrentOutput: true 
      }, 
      set1: ['watch:html', 'watch:styles'], 
     }, 

grunt.registerTask('default', 'watch'); 
grunt.registerTask('htmlcss', ['concurrent:set1']); 
+0

您能否詳細解釋一下,爲什麼它可以幫助其他人理解,您是如何編寫解決方案的? –

+0

這不會增加任何東西給接受的答案,給兩年前... –

-1

只需更改端口地址和端口livereload。 例如。如果端口是9000,將其更改爲8000,然後從35729重新加載到36729