Alok k
Alok k

Reputation: 19

Is there a way to use "js-yaml" to add one YAML file to another YAML file in nodejs unix environment

I have two YAML files. I want to add the second YAML file inside first YAML file using "js-yaml". Basically my first YAML file continues to grow by adding multiple YAML files.

I have tried to add the new file directly to first YAML file, and it has added, but when I try it again, it is replacing the current data, not adding it:

try {
  var filename = path.join(__dirname, "first.yaml"),
    contents = fs.readFileSync(filename, "utf8"),
    first = yaml.load(contents);

  var filename2 = path.join(__dirname, "second.yaml"),
    contents2 = fs.readFileSync(filename2, "utf8"),
    second = yaml.load(contents2);

  var fruits = first.fruits ;
  var newfruits = second.fruits;
  first.fruits= newfruits;

  console.log(util.inspect(first, false, 10, true));
  fs.writeFile("first.yaml", yaml.dump(first), "utf8", err => {
    if (err) console.log(err);

the above code is replacing fruits always not adding it. I want to stack it when ever I want it to add.

Upvotes: 1

Views: 1490

Answers (1)

Avinash Pathrol
Avinash Pathrol

Reputation: 11

Well, I had a look on you code and there is not much to do to obtain the desired result.

Use:

first = first + second; 

or:

first = second;

Upvotes: 1

Related Questions