2014-09-13 127 views
1

我正在使用我的buildscript,我需要更新YAML中的值(准确地说是.yml)文件。Grunt:更新YAML文件中的值?

为了便于开发,我只是把它定义为我的默认任务:

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文件中的值?

回答

5

比如这个:

https://www.npmjs.org/package/yamljs

你可以这样做:

YAML = require('yamljs'); 
grunt.file.write('config.yml', YAML.stringify(conf)); 
+0

哇,这是比较容易,我认为。谢谢! – Sven 2014-09-13 19:40:24