2017-06-12 23 views
1

當執行grunt自定義任務我迭代循環,在循環內我調用一個grunt任務,同時調用我設置值使用當前循環值的任務,但是當循環執行頭文,它總是設置數組的最後一個值。任務正在執行的最後一個值只有在咕嚕自定義任務

var productionUrl = "http://www.abc.de"; 
var phantomPagesPath = { 
    "index": "/index.php", 
    "search": "/Suche?searchString=test", 
    "warenkorb": "/warenkorb", 
    "product": "/4-ecksofa", 
    "cms": "/content/25-service", 
    "category": "/bestellung", 
    "pageNotFound": "/404" 
}; 

grunt.initConfig({`phantomas: { 
     prod: { 
      options: { 
       options: { 
        'timeout': 30 
       }, 
       buildUi: true 
      } 
     } 
    }`});` 

grunt.registerTask('fantomas', 'Custome Phantomas task', function() {   

    var done = this.async(); 
    var pageUrl = ''; 
    var location = ''; 
    for (var page in phantomPagesPath) {    
     pageUrl = ''; 
     location = ''; 
     if (phantomPagesPath.hasOwnProperty(page)) { 
      pageUrl = productionUrl + phantomPagesPath[page]; 
      location = './public/phantomas/' + page + "/"; 
      console.log("process started for: " + pageUrl); 
      console.log("location: " + location); 
      grunt.config.set('phantomas.options.url', pageUrl); 
      grunt.config.set('phantomas.options.indexPath', location); 
      grunt.task.run('phantomas');     
     }    
    } 
    done(); 
}); 

現在輸出我得到

過程開始了:http://www.abc.de/index.php 位置:./public/phantomas/index/

過程開始了:http://www.abc.de/Suche?searchString=test 位置:./public/phantomas/搜索/

流程開始:http://www.abc.de/warenkorb 位置:./public/phantomas/warenkorb/

過程開始了:http://www.abc.de/4-ecksofa 位置:./public/phantomas/product/

過程開始了:http://www.abc.de/content/25-service 位置:./public/phantomas/cms/

過程開始了:http://www.abc.de/bestellung 位置:./public/phantomas/category/

過程開始了:http://www.abc.de/404 位置:./public/phantomas/pageNotFound/

運行「phantomas:prod」(phantomas)任務 PHANTOMAS執行(S)開始。

任務越來越全部數量的條目執行,但任務發送的數據是從環「pageNotFound」中的最後一項,

我的意思是這個過程是工作7倍,但在每一個過程,它正在最後循環的值。

回答

1

搜索了很多後,我想出了inspite調用

grunt.task.run('phantomas'); 

在foreach循環的解決方案,它應該被稱爲環,這意味着該任務是要爲一個時間執行外只。因此,對於這一點,我們需要增加使任務中的任務/目標,我沒有下面的代碼:

grunt.loadNpmTasks('grunt-phantomas'); 
var phantomPagesPath = { 
    "index": "/index.php", 
    "search": "/Suche?searchString=test", 
    "warenkorb": "/warenkorb", 
    "product": "/4-ecksofa", 
    "cms": "/content/25-service", 
    "category": "/bestellung", 
    "pageNotFound": "/404" 
}; 
    grunt.initConfig({ 
    phantomas: {    
     //For Executing this task run: grunt fantomas 
    }, 

});

// Register customer task for phantomas 
grunt.registerTask('fantomas', 'Custome Phantomas task', function() { 
    var done = this.async(); 
    var pageUrl = ''; 
    var location = ''; 
    for (var page in phantomPagesPath) { 
     pageUrl = productionUrl + phantomPagesPath[page]; 
     location = './public/phantomas/' + page + "/"; 
     grunt.config.set('phantomas.'+page+'.options.url', pageUrl); 
     grunt.config.set('phantomas.'+page+'.options.indexPath', location); 
     grunt.config.set('phantomas.'+page+'.options.buildUi', true); 
     grunt.config.set('phantomas.'+page+'.options.options.timeout', 30); 
     var pageUrl = ''; 
     var location = ''; 
    } 
    grunt.task.run('phantomas'); 
    done(); 
});  

現在運行

grunt fantomas