0

I am trying to use js-yaml to import a yml file into a js file. The main yml file contains variables referenced in another file.
I am trying to find a way to get import the values of those extra references, but when I use safeLoad or load, the file loads in just the reference (not the actual value).

Below is what the files look like, the expected outcome, and the actual outcome.

Thanks for your help!

main.yml file

otherAttribute:
   bar: 'foo'

custom: ${file(otherFile.yml)}

RotherFile.yml file

key1: value
key2: value

js file

  yaml = require("js-yaml");
  var example = yaml.load(fs.readFileSync("./main.yml", "utf8"));
  console.log('value of example read in via yaml.load',example);

Desired Output

otherAttribute:
   bar: 'foo'

custom:
   key1: value
   key2: value

Actual Output

otherAttribute:
   bar: 'foo'

custom: ${file(otherFile.yml):${self:otherAttribute.bar}}
Sarah Ganci
  • 209
  • 1
  • 10
  • The actual output makes `:${self:otherAttribute.bar}` appear from seemingly nowhere, are you sure you did give the correct one? In any case, you seem to use the syntax of some kind of templating language but don't invoke a templating engine. `${…}` has nothing to do with YAML and a YAML parser simply loads it as string. You need to invoke whatever templating engine you're trying to use. – flyx Mar 05 '21 at 10:58

1 Answers1

0

The file referencing syntax you use in main.yml (${file(otherFile.yml)}) is a feature of the Serverless Framework and not YAML. YAML doesn't support any referencing or inclusion, see also this SO question.

One answer to the question I linked above suggests a Python version with PyYAML implementing an inclusion, it's available here. This could be easily modified to support ${file(...)} instead of !include ....

So unless you want to hack js-yaml to make it understand the ${file(...)} reference it would be probably easiest to follow the Python approach.

yvesonline
  • 4,609
  • 2
  • 21
  • 32