Justin808
Justin808

Reputation: 21522

grunt-file-creator with a variable filename

I'm trying to use https://npmjs.org/package/grunt-file-creator to create a file, but I would like to have a variable filename...

grunt.initConfig({
  file-creator: {
    "test": {
      grunt.config('meta.revision') + "-test.txt": function(fs, fd, done) {
        fs.writeSync(fd, 'data');
        done();
      }
    }
  }
});

and

grunt.initConfig({
  file-creator: {
    "test": {
      "<%= grunt.config('meta.revision') %>-test.txt": function(fs, fd, done) {
        fs.writeSync(fd, 'data');
        done();
      }
    }
  }
});

don't seem to work. How can I have a variable filename? The idea is that I've set the git commit ID as the value of meta.revision.

Upvotes: 3

Views: 549

Answers (1)

Kyle Robinson Young
Kyle Robinson Young

Reputation: 13762

It is because grunt-file-creator implements their own API rather than utilizing the standard Grunt src/dest API. I'd recommend that task be rewritten using this.files rather than this.data but a simple fix the author doesn't want to use the standard API would be to change:

var filepath = item.key;

to

var filepath = grunt.template.process(item.key);

on line 34 of the task: https://github.com/travis-hilterbrand/grunt-file-creator/blob/master/tasks/file-creator.js#L34

Otherwise you would have to write a hacky workaround like this:

grunt.registerTask('fixed-file-creator', function() {
  var taskName = 'file-creator';
  var cfg = grunt.config(taskName);
  Object.keys(cfg).forEach(function(target) {
    var newcfg = {};
    Object.keys(cfg[target]).forEach(function(dest) {
      newcfg[grunt.template.process(dest)] = grunt.config([taskName, target, dest]);
    });
    grunt.config([taskName, target], newcfg);
  });
  grunt.task.run(taskName);
});

and then run grunt fixed-file-creator.

Upvotes: 2

Related Questions