我正在处理我的构建脚本,我需要更新 YAML(准确地说是 .yml
)文件中的值。
为了更容易开发,我简单地将它定义为我的默认任务:
grunt.registerTask('default', function() {
var conf = grunt.file.readYAML('config.yml');
// Shows correct contents of config.yml
console.log(conf);
// Changing the value of key 'deploy'
conf['deploy'] = 'Hello World';
// Trying to write the updated data back to file
grunt.file.write('config.yml', conf);
// Re-reading the new file
var conf2 = grunt.file.readYAML('config.yml');
// logs [ 'object Object' ]
console.log(conf2);
});
我认为我的评论非常清楚我正在尝试做什么——更新配置设置。
[ 'object Object' ]
被记录的原因是因为它实际上被写入了那个文件。这意味着我不能简单地执行 grunt.file.write('config.yml', conf);
,我需要类似 JSON.stringify
的东西,但对于 YAML。这样的东西存在吗?如何在 Grunt 中更新 yml 文件中的值?
最佳答案
例如:
https://www.npmjs.org/package/yamljs
你可以这样做:
YAML = require('yamljs');
grunt.file.write('config.yml', YAML.stringify(conf));
关于javascript - 咕噜声 : Updating value in YAML file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25826871/