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

@rainbow-industries/rainbow-config

v3.1.0

Published

A YAML config file loader

Downloads

81

Readme

Rainbow Config

YAML config files made simple for TypeScript and JavaScript using ESM.

Changelog

Version 3.x

  • The path is now passed to the load() method instead of the constructor
  • The load() method has now one argument: the path
  • The library was re-implemented using TypeScript and exports types
  • The library can still be used using JavaScript

Example config file

The DBs host and password are loaded either from a secrets file or from environment variables.

config/development.yml

db:
    main
        port: 5432
        host: ${DB_HOST}
        password: ${MAIN_DB_PASS}
myKey: myValue
anArray
    - itemOne
    - itemTwo

secrets.development.yml

DB_HOST: l.dns.porn
MAIN_DB_PASS: soSecureICantBelieveIt

Environments

The config loader decides based on the environment which config file to load. The following default environments are available (alternative names):

  • development (dev)
  • testing (test)
  • integration (int)
  • production (prod)

The environment can either be set by passing it as parameter to the application (e.g. --develpment) or by defining it in the RAINBOW_ENV or NODE_ENV environment variable.

Based on the configured environment the config file is loaded. If the development environment is active, the loader tries to load the config/development.yml file.

Example

It is based on the example config above.

import RainbowConfig from '@rainbow-config/RainbowConfig';
import { URL } from 'url';
import path from 'path';

const config = new RainbowConfig();

// you may add custom environments
config.addEnvironment('extra-env');

// you may load the config from a custom directory, default is config
config.setConfigDir('conf');

// get the directory where the config filder is located in
const rootdir = path.join(path.dirname(new URL(import.meta.url).pathname, '../');

// load the config file (the secrets dir parameter is optional)
await config.load(rootdir, secretsDir);

// get the full config object
const allKeys = config.get();

// get the db password
const dbPassword = config.get('db.main.password');

// prints: soSecureICantBelieveIt
console.log(dbPassword);

API

Constructor: instantiate the config class

The constructor accepts optionaly the environment name asparameter 1

const config = new RainbowConfig();

Add an environment: config.addEnvironment(name: string)

You may optionally add an environment. You may also specify an alternative name that maps to the environment name i.e. int for integration.

The follwoing envormnents are available (alternative names):

  • development (dev)
  • testing (test)
  • integration (int)
  • production (prod)
config.addEnvironment('extra-env', 'alternative-name-for-extra-env');

Change the config directory: config.setConfigDir(relativePath: string)

You may change the directory the config files are located in. This defaults to config.

config.setConfigDir('conf');

Load the confguration: config.load(rootDir: string, secretsDir?: string)

In order to load the config, you have to call the load method. This will load the config from the ${rootPath}/config directory. This will load the config file an fill all variables that are either set in the environment or the secrets file. If a variable is not found in an env variable, it is assumed, that it shall be loaded from the secrets file, which is located in the rootPath passed to the load(rootPath: string) method. The secrets file has the name secrets.${environment}.yaml. If the secretsDir is passed to the load method, that directory will be used to load the secrets.

const rootdir = path.join(path.dirname(new URL(import.meta.url).pathname, '../');
await config.load(rootdir);

Get a config value: config.get(path: string | undefined)

Get the complete config object.

const configData = config.get(undefined);

Get the a partial config object

const configData = config.get(`db.main`);

Get the a specific key

const configData = config.get(`db.main.password`);