nodejs templating yaml files

Dor*_*sku 5 yaml node.js

I looking for node module (or something else) that can parse in runtime parameters from my program into yaml files.

for example in kubernetes yamls

metadata:
  name: $PROJECT_NAME
  labels:
    service: $SERVICE_NAME
    system: $SYSTEM_ID
    app_version: $PROJECT_VERSION
    tier: app
Run Code Online (Sandbox Code Playgroud)

There is a nice way to build new yaml or change the exist one that contain all my parameters values?

Dor*_*sku 4

我决定使用Handlebars 模块, 只需为该函数提供一个包含我想要解析的参数的模板,该函数将创建包含我所有更改的新文件

const Handlebars = require('handlebars');
const source = fs.readFileSync(`${cwd}/${file}`).toString();
const template = Handlebars.compile(source);
const contents = template({ PROJECT_NAME: `${name}`, PROJECT_VERSION: `${version}`, DOCKER_IMAGE: `${image}` });
fs.writeFileSync(`${cwd}/target/${file}`, contents);
console.log(`${file} -- Finish parsing YAML.`);
Run Code Online (Sandbox Code Playgroud)

JSON 看起来像

spec:
  containers:
    - name: {{PROJECT_NAME}}:{{PROJECT_VERSION}}
      resources:
        limits:
          memory: "1Gi"
          cpu: "1"

      image: {{DOCKER_IMAGE}}
Run Code Online (Sandbox Code Playgroud)