我正在尝试编写一个繁琐的任务,该任务将遍历一组输入文件并对每个文件运行转换。 假设输入文件由*.in
给出,并且对于每个输入文件,任务将创建一个.out
文件。
从我读到的内容来看,配置似乎应该看起来像这样
grunt.initConfig({
my_task: {
src: 'C:/temp/*.in',
dest: 'C:/temp/output/*.out'
}
});
并且任务注册应为:
grunt.registerTask('my_task', 'iterate files', function() {
//iterate files.
});
我不知道如何让咕噜咕噜向我发送文件列表并迭代它们。
知道怎么做吗?
这就是我结束做的事情,也是解决我的问题的原因。对于任务配置,我执行以下操作:
grunt.initConfig({
convert_po: {
build: {
src: 'C:/temp/Locale/*.po',
dest: 'C:/temp/Locale/output/'
}
}
});
这是任务的实现:
grunt.registerMultiTask('convert_po', 'Convert PO files to JSON format', function() {
var po = require('node-po');
var path = require('path');
grunt.log.write('Loaded dependencies...').ok();
//make grunt know this task is async.
var done = this.async();
var i =0;
this.files.forEach(function(file) {
grunt.log.writeln('Processing ' + file.src.length + ' files.');
//file.src is the list of all matching file names.
file.src.forEach(function(f){
//this is an async function that loads a PO file
po.load(f, function(_po){
strings = {};
for (var idx in _po.items){
var item = _po.items[idx];
strings[item.msgid] = item.msgstr.length == 1 ? item.msgstr[0] : item.msgstr;
}
var destFile = file.dest + path.basename(f, '.po') + '.json';
grunt.log.writeln('Now saving file:' + destFile);
fs.writeFileSync(destFile, JSON.stringify(strings, null, 4));
//if we processed all files notify grunt that we are done.
if( i >= file.src.length) done(true);
});
});
});
});