Grunt:更新YAML文件中的值?

时间:2014-09-13 19:30:46

标签: javascript gruntjs yaml

我正在处理我的构建脚本,我需要更新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文件中的值?

1 个答案:

答案 0 :(得分:6)

例如:

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

你可以这样做:

YAML = require('yamljs');
grunt.file.write('config.yml', YAML.stringify(conf));
相关问题