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?
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.