npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yaml-env-defaults

v2.0.5

Published

Read yaml file with environment variables substitution

Downloads

14,812

Readme

Read yaml file with environment variables substitution

This is an wrapper under js-yaml library which can render environment variables values with defaults support in yml file. (like Java Spring application.yaml).

Install module for Node.js

npm install yaml-env-defaults

# or

yarn add yaml-env-defaults

Usage

Import

// ES
import { readYamlEnvSync, readYamlEnv } from 'yaml-env-defaults';

// require
const { readYamlEnvSync, readYamlEnv } = require('yaml-env-defaults');

Reading yaml

import { readYamlEnvSync, readYamlEnv } from 'yaml-env-defaults';

// Read one yaml file synchronysly
const config = readYamlEnvSync('../path/to/yaml-file.yml');

// Read many yaml files synchronysly
const joinedYamls = readYamlEnvSync(['first.yml', 'second.yml', 'third.yaml']);

// Read yaml files with custom properties provider fn
const propertiesMap = {
  ENV_VAR_NAME: 'My value',
  USERS_COUNT: 4
};

const getProperty = (key) => {
  // without default value will be throwed error in case of missing 
  return propertiesMap[key] || 'Default value';
}

const config = readYamlEnvSync('../path/to/yaml-file.yml', getProperty);

// Custom properties for js-yaml
const config = readYamlEnvSync('../path/to/yaml-file.yml', undefined, {
  jsYaml: {
    onWarning: (yaml, e) => {
    }
    //...
  }
})

  // the same for readYamlEnv as async 
  (async () => {
    // Read one yaml file asynchronysly
    const config = await readYamlEnv('../path/to/yaml-file.yml');
  });
//or
readYamlEnv('../path/to/yaml-file.yml').then(config => config);

Example

config-env.yml

foo: 1
bar: 'text-${ENV_VAR_MY}-text-${ENV_VAR_YOUR}-text'

someFoo:
  someBar: ['${ENV_VAR_MY}', '${ENV_VAR_YOUR}']
  yaml-array:
    - ${ENV_VAR_MY}
    - second

someStrange: Thats/${ENV_VAR_MY}/${NAMESPACE:cloud-or-not}/${ENV_VAR_YOUR}

escapedValue:
  value: \$\{ENV_VAR_MY\}

JS output

process.env.ENV_VAR_MY = 'Some value 2';
process.env.ENV_VAR_YOUR = 'Another Value';

const config = readYamlEnvSync(path.resolve(__dirname, 'config-env.yml'));
// Object {
//   "bar": "text-Some value 2-text-Another Value-text",
//   "escapedValue": Object {
//     "value": "\\\\$\\\\{ENV_VAR_MY\\\\}",
//   },
//   "foo": 1,
//   "someFoo": Object {
//     "someBar": Array [
//       "Some value 2",
//       "Another Value",
//     ],
//     "yaml-array": Array [
//       "Some value 2",
//       "second",
//     ],
//   },
//   "someStrange": "Thats/Some value 2/cloud-or-not/Another Value",
// }

More examples you can find in ./src/__fixtures__/ and ./src/__snapshots/ from tests snapshot results.

Tests coverage report

---------------------|---------|----------|---------|---------|-------------------
File                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------------|---------|----------|---------|---------|-------------------
All files            |   98.46 |    83.02 |     100 |   98.11 |                   
 read-yaml-config.ts |   98.46 |    83.02 |     100 |   98.11 | 40                
---------------------|---------|----------|---------|---------|-------------------

Used Dependencies:

Author

👤 Valerii Nosikov

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator