While executing grunt custom task I am iterating a loop, inside the loop I am calling a grunt task while calling I set values using current loop value to the task but when loop execute for alliteration it is setting last value of array always.
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();
});
Now Output I am getting
process started for: http://www.abc.de/index.php location: ./public/phantomas/index/
process started for: http://www.abc.de/Suche?searchString=test location: ./public/phantomas/search/
process started for: http://www.abc.de/warenkorb location: ./public/phantomas/warenkorb/
process started for: http://www.abc.de/4-ecksofa location: ./public/phantomas/product/
process started for: http://www.abc.de/content/25-service location: ./public/phantomas/cms/
process started for: http://www.abc.de/bestellung location: ./public/phantomas/category/
process started for: http://www.abc.de/404 location: ./public/phantomas/pageNotFound/
Running "phantomas:prod" (phantomas) task PHANTOMAS EXECUTION(S) STARTED.
Task are getting executed for all number of entries but the data sent for task is the last entry from loop "pageNotFound",
I mean this process is working 7 times but on each process, it is taking last value of loop.
答案 0 :(得分:1)
经过大量搜索,我想出了一个解决方案,尽管打电话
grunt.task.run('phantomas');
在foreach循环中,应该在循环外部调用它,这意味着任务将仅执行一次。因此,为此,我们需要在代码之后的任务中增加make tasks / target:
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