我正在尝试使用 https://npmjs.org/package/grunt-file-creator创建一个文件,但我想要一个可变的文件名...
grunt.initConfig({
file-creator: {
"test": {
grunt.config('meta.revision') + "-test.txt": function(fs, fd, done) {
fs.writeSync(fd, 'data');
done();
}
}
}
});
和
grunt.initConfig({
file-creator: {
"test": {
"<%= grunt.config('meta.revision') %>-test.txt": function(fs, fd, done) {
fs.writeSync(fd, 'data');
done();
}
}
}
});
似乎不起作用。我怎样才能有一个可变的文件名?我的想法是,我将 git commit ID 设置为 meta.revision
的值。
最佳答案
这是因为 grunt-file-creator
实现了自己的 API,而不是利用标准的 Grunt src/dest
API。我建议使用 this.files
而不是 this.data
重写任务,但作者不想使用标准 API 的一个简单修复是更改:
var filepath = item.key;
至
var filepath = grunt.template.process(item.key);
任务第 34 行:https://github.com/travis-hilterbrand/grunt-file-creator/blob/master/tasks/file-creator.js#L34
否则你将不得不编写一个像这样的黑客解决方法:
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);
});
然后运行gruntfixed-file-creator
。
关于gruntjs - 具有可变文件名的 grunt-file-creator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235840/