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

ut-config

v7.10.0

Published

Reusable configuration `load` and `edit` methods.

Downloads

218

Readme

ut-config

Reusable configuration load and edit methods.

Starting

ut-config assumes the application is being run with a command like

node index {app} {method} {env}

or

UT_APP=server UT_METHOD=debug UT_ENV=dev node index

Configuration

In addition to using environment configuration files within the application filesystem, the following additional options are available, which will override the configuration

  • Configuration file
  • Command line parameters
  • Environment variables

The algorithm of how these are applied is described in the rc package, here. This is adapted from rc package readme:

  • command line arguments, parsed by minimist (e.g. --foo baz, also nested: --foo.bar=baz)
  • environment variables prefixed with ut_${impl}_${env}_
    • or use "__" to indicate nested properties (e.g. ut_${impl}_${env}_foo__bar__baz => foo.bar.baz)
  • if you passed an option --config file then from that file
  • a local .ut_${impl}_${env}rc or the first found looking in ./ ../ ../../ ../../../ etc.
  • $HOME/.ut_${impl}_${env}rc
  • $HOME/.ut_${impl}_${env}/config
  • $HOME/.config/ut_${impl}_${env}
  • $HOME/.config/ut_${impl}_${env}/config
  • /etc/ut_${impl}_${env}rc
  • /etc/ut_${impl}_${env}/config
  • the object taken from environment configuration file within {app} / UT_APP folder (dev.js[on], test.js[on], etc.)
  • the object taken from method configuration file within {app} / UT_APP folder (debug.js[on], install.js[on])
  • the object taken from common.js[on] configuration file within {app} / UT_APP folder

All configuration sources that were found will be flattened into one object, so that sources earlier in this list override later ones.

File based configuration sources outside the application folder can be in ini, json or yaml format.

${impl} is implementation identifier taken from environment configuration file, ${env} is the environment passed through command line or UT_ENV environment variable, or 'dev' (by default)

Environment configuration files

These files are part of the application and define the defaults for each environment. Environment configuration files can be either .json or .js file. If it is a .js file, it must export an object. If a file named common.js or common.json exists, it is used as a base object for all environments where the actual environment configuration is merged. When using .js file, more complex configuration is possible - like having functions or other objects, not supported by JSON. Minimal environment file dev.json may look like:

{
    "implementation": "impl-test",
    "service": "admin"
}

Templating

After all configurations have been loaded and merged into a common object it gets processed through a template engine. For more info check ut-function.template and in particular the provided recursive rendering functionalities.

E.g.

const obj = require('ut-config').load({
    config: {
        x: 'normal string',
        y: '${hello("Test1")}',
        z: '${hello(test)}'
    },
    context: {
        hello: who => 'Hello ' + who + ' !!!',
        test: 'Test2'
    }
});

// obj.y would be equal to 'Hello Test1 !!!'
// obj.z would be equal to 'Hello Test2 !!!'

Everything that is passed in the context object is accessible as a context in the config through the template engine.

The context is additionally enriched with encrypt and decrypt methods (this is only in case UT_MASTER_KEY is available as environment variable which is currently being set by Jenkins). So if you have the correct master key you can securely define encrypted passwords and other sensitive data in the config.

E.g.

/*
  note that the context is not mandatory if you need to use
  only the built-in encrypt/decrypt methods
*/
const obj = require('ut-config').load({
    config: {
        pass: '${decrypt("3569bf662a23fad6eb2069e09e8da490dff37a84e69a5eb82a1efecf9f8fcdb2")}'
    }
});