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

hot-config

v2.1.0

Published

A node.js config loader is designed to be able to hot-reload

Downloads

48

Readme

HOT-CONFIG

A node.js config loader is designed to be able to hot-reload.

FEATURES

  • Load recursively config files that matches a given pattern.
  • Built-in asynchronous YAML and JSON parsers.
  • Support environment-specific (profile-specific) configs.
  • Hot-reloadable.

USAGE

# ./configs/generic.yaml
# ------------------------------------------------------------------------------
default:
  debug: true
production:
  debug: false
# ./configs/db/mongo.yaml
# ------------------------------------------------------------------------------
default:
  host: 'localhost'
  port: 27017
production:
  host: '12.12.12.12'
# ./configs/movie-catalog.yaml
# ------------------------------------------------------------------------------
default:
  cache: 0
production:
  cache: -1
// ./index.js
// ----------------------------------------------------------------------------- 
const config = require('hot-config');
const app = require('src/app');

config.load('configs/', (err) => {
  if (err) {
    throw err;
  }
  app.start();
});
// ./src/app.js
// -----------------------------------------------------------------------------
const config = require('hot-config').store;

function start() {
  if (!process.env.NODE_ENV) {
    expect(config.debug).to.equal(true);
    expect(config.db.mongo.host).to.equal('localhost');
    expect(config.db.mongo.port).to.equal(27017);
    expect(config.movieCatalog.cache).to.equal(0);
  }

  if (process.env.NODE_ENV === 'production') {
    expect(config.debug).to.equal(false);
    expect(config.db.mongo.host).to.equal('12.12.12.12');
    expect(config.db.mongo.port).to.equal(27017);
    expect(config.movieCatalog.cache).to.equal(-1);
  }
}

module.exports = {start};

APIs

config.store

The config store.

config.filePatterns

Built-in file patterns, yaml and json.

config.fileParsers

Built-in file parsers, yaml and json.

config.ParserError

A Error-based class provides information of a parser error.

config.load(dir, opts, done)

Load config files recursively in a directory.

/**
 * Load configs in a directory.
 * @param {String} dir - The directory path.
 * @param {Object} [opts] - Optional options.
 * @param {RegExp} [opts.filePattern] - Config file pattern. Default: Built-in YAML file pattern.
 * @param {Function} [opts.fileParser] - Config file parser. Default: Built-in YAML parser.
 * @param {Function} [opts.pathNormalizer] - Config path normalizer. Default: lodash.camelCase
 * @param {String} [opts.defaultProfile] - Default profile name. Default: 'default'
 * @param {String} [opts.profile] - Loaded profile name. Default: process.env.NODE_ENV
 * @param {Boolean} [opts.dryRun] - Load for testing only, don't update the store. Default: false
 * @param {Function} done - The callback.
 */
function load (dir, opts, done) {}

config.clear()

Clear the config store.

HOT-RELOADABLE

The key of hot-reloading is the only config store instance.

Take a look at:

After all config files are parsed and merged, the config store will be cleared and updated synchronously but not replaced by a new instance, this makes all existing references of the config store are still working after reloaded.

But keep in mind that this feature only works if you're keeping references of the store, not with references of config values. For example:

const config = require('hot-config').store; // this is hot-reloadable!
setTimeout(() => console.log(config.mongo), 1000);

const mongoConfig = require('hot-config').store.mongo; // this is not hot-reloadable!
setTimeout(() => console.log(mongoConfig), 1000);

PREFER JSON

If you prefer JSON, you need to do some extra works:

const config = require('hot-config');

const opts = {
  filePattern: config.filePatterns.json,
  fileParser: config.fileParsers.json
}

config.load('configs', opts, (err) => {
})

DRY RUN

To load configs for testing, you can use the loading opts dryRun.

const config = require('hot-config');

const opts = {
  dryRun: true
}

config.load('configs', opts, (err, testStore) => {
  // `testStore` has new configs, the `store` has no change!
})