最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - create a custom grunt task for processing files - Stack Overflow

programmeradmin4浏览0评论

I am trying to write a grunt task that would go over a set of input files and run a transformation on each file. let's assume the input files are given by *.in and for each of them the task will create an .out file.

From what I read, it seems that the config should look something like this

grunt.initConfig({
    my_task: {
        src: 'C:/temp/*.in',
        dest: 'C:/temp/output/*.out'
    }
});

and the task registration should be:

grunt.registerTask('my_task', 'iterate files', function() {
    //iterate files.
});

I cannot figure out how to make grunt send me the list of files and iterate over them.

Any idea how to do it?

I am trying to write a grunt task that would go over a set of input files and run a transformation on each file. let's assume the input files are given by *.in and for each of them the task will create an .out file.

From what I read, it seems that the config should look something like this

grunt.initConfig({
    my_task: {
        src: 'C:/temp/*.in',
        dest: 'C:/temp/output/*.out'
    }
});

and the task registration should be:

grunt.registerTask('my_task', 'iterate files', function() {
    //iterate files.
});

I cannot figure out how to make grunt send me the list of files and iterate over them.

Any idea how to do it?

Share Improve this question edited Sep 16, 2013 at 11:06 Denim Datta 3,8024 gold badges28 silver badges53 bronze badges asked Sep 16, 2013 at 8:59 elewinsoelewinso 2,5216 gold badges23 silver badges28 bronze badges 3
  • Basic tasks don't read the configuration. You want to create a multi-task. Have a read through here: gruntjs.com/creating-tasks#multi-tasks – badsyntax Commented Sep 16, 2013 at 10:45
  • @Panther Thanks for the edit. I new to SO and I would like to understand why the downvote in order to improve my future submissions – elewinso Commented Sep 17, 2013 at 9:25
  • @elewinso : Avoid comment for "thanks/sorry etc". And I'm not sure why this downvote. – Denim Datta Commented Sep 17, 2013 at 11:37
Add a comment  | 

1 Answer 1

Reset to default 20

This is what I ended doing and what solved my problem. for the task configuration I did the following:

  grunt.initConfig({
    convert_po: {
      build: {
        src: 'C:/temp/Locale/*.po',
        dest: 'C:/temp/Locale/output/'
      }
    }
  });

and this is the task's implementation:

  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);
    });
  });
});
});
发布评论

评论列表(0)

  1. 暂无评论