Wurzelseppi
Wurzelseppi

Reputation: 117

Typescript - Read variables from yaml and replace tokens in another file with them

does anyone know if there is a node library that does this:

  1. Read structure from yaml file (string values)
  2. Replace tokens in a string with them (read from a file e.g)

Example:

yaml:

properties:
  instance_name: "myInstanceName"
  instance_type: "myInstanceType"

template-string:

This is the instancename: ${properties.instance_name} and its type is ${properties.instance_type}

desired output:

This is the instancename: myInstanceName and its type is myInstanceType

Just wanted to ask if there is something out there to save my time :-)

Thanks guys!

Upvotes: 1

Views: 3576

Answers (1)

Wurzelseppi
Wurzelseppi

Reputation: 117

This one helped me now:

https://www.npmjs.com/package/interpolate-json

https://www.npmjs.com/package/yaml

import * as YAML from 'yaml'

const { interpolation } = require('interpolate-json');
...

const cFile = fs.readFileSync('/config/cfg.yaml', 'utf-8')    
const userData = fs.readFileSync('/config/uData.sh', 'utf-8')
const config = YAML.parse(configFile,{
      prettyErrors:true
    })
....

let interpolatedUserData = interpolation.expand(userData, config)

and viola....

Upvotes: 1

Related Questions