您可以使用以下方式处理问题:
https://github.com/dylang/grunt-prompt
这是一个很好的小插件,可以完成一项工作并且做得很好。它将您在命令行中输入的任何值放入变量中:(示例)
prompt: {
target: {
options: {
questions: [
{
config: 'key', // arbitrary name or config for any other grunt task
type: 'input', // list, checkbox, confirm, input, password
message: 'What is your API key?',
default: '', // default value if nothing is entered
when: function(answers) { return !grunt.file.exists('config.yml'); } // only ask this question when this function returns true
}
]
}
}
}
然后您可以使用Grunt.file 函数将这些值写入文件:
http://gruntjs.com/api/grunt.file#grunt.file.write
要编排它,您需要创建一个自定义任务:(示例)
grunt.registerTask("my_config_task", function (arg) {
var key = arg || grunt.config('key');
grunt.file.write("config.yml", key);
});
grunt.registerTask('build', ['prompt', 'my_config_task']);
写作可能需要改进,我猜你需要替换值并组织为yml 文件或json 对象等......
在查看grunt-bump 的来源时找到了一种可能的解决方案。他们在做什么是将配置文件解析为 JSON 对象:
https://github.com/darsain/grunt-bumpup/blob/master/tasks/bumpup.js#L128
替换他们需要的任何值(如 JSON)并用字符串化的对象覆盖文件:
https://github.com/darsain/grunt-bumpup/blob/master/tasks/bumpup.js#153
看起来效果不错。