Grunt:更新YAML文件中的值?

Sve*_*ven 1 javascript yaml gruntjs

我正在处理我的buildscript,我需要更新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);
});
Run Code Online (Sandbox Code Playgroud)

我想我的评论非常清楚我想要做什么 - 更新配置设置.

[ 'object Object' ]记录的原因是因为它实际写入该文件.这意味着我不能简单地做grunt.file.write('config.yml', conf);,我需要像JSON.stringifyYAML 一样的东西.这样的事情存在吗?如何在Grunt中更新yml文件中的值?

Laj*_*res 6

例如:

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

你可以这样做:

YAML = require('yamljs');
grunt.file.write('config.yml', YAML.stringify(conf));
Run Code Online (Sandbox Code Playgroud)