Sven
Sven

Reputation: 13275

Grunt: Updating value in YAML file?

I am working on my buildscript and I need to update a value inside a YAML (.yml to be precise) file.

For easier development, I simply defined it as my default task:

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);
});

I think my comments make pretty clear what I am trying to do – updating a configuration setting.

The reason [ 'object Object' ] is being logged is because that is actually written to that file. That means I can't simply do grunt.file.write('config.yml', conf);, I need something like JSON.stringify but for YAML. Does something like this exist? How to update a value inside a yml file in Grunt?

Upvotes: 1

Views: 1333

Answers (1)

Lajos Veres
Lajos Veres

Reputation: 13725

For example with this:

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

You can do that:

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

Upvotes: 6

Related Questions