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

configlet

v0.1.0

Published

JSON-Schema-based Configuration System

Downloads

2

Readme

Configlet - JSON-Schema-based Configuration Loader

Configlet loads your configuration from your config files, environment variables, and commandline arguments (in that order), merge them, and then validate the results against the JSON-Schema you specified, via schemalet.

Install

npm install configlet

Usage

var Config = require('configlet')

var config = Config.parseSync(<your JSON schema here>); // a parse option can be added, see below 

Configuration Files

By default, Configlet looks for the configuration files in the ./config/ directory, relative to process.cwd() (both are parse options that can be changed).

The configuration files should hold values that are valid according to your JSON-Schema. They do not have to hold every single value as they can be merged in via a subsequently loaded configuration file, the environment variables, or the command line arguments.

The configuration files are loaded in the following order (this can also be configured):

default.<ext>
<environment>.<ext> // from NODE_ENV || ENV || 'development'
<hostname>.<ext> // from os.hostname()
local.<ext>

The formats supported by default are JSON and YAML for the extensions .json, .yml, .yaml, and it can be extended as well via parse options.

Environment Variables

By default, Configlet reads your environment variable based on the paths of your JSON-Schema object.

For example, let's say that we have a schema for the following object:

{
  database: {
    username: <string>,
password: <string>,
host: <string>,
port: <integer>
  },
}

Then Configlet would read from the following fields:

DATABASE_USERNAME
DATABASE_PASSWORD
DATABASE_HOST
DATABASE_PORT

Commandline Arguments

Similarly, Configlet reads your commandline arguments based ont he paths of your JSON-Schema object.

For the same object above, Configlet would look for the following commandline arguments to read from:

--database.username
--database.password
--database.host
--database.port

API

.parseSync(<schema>, <parseOptions> = {});

The schema used here is the schema recognized by schemalet. It means that you can generate a class-based object via configlet, instead of just generating plain objects.

Since this is a sync-version, it means that it blocks until the files are read. This is fine for the starting phase of the program, since the program cannot continue until the config files are loaded. There is an async version as well.

Parse Options

The following are the parse options to pass into .parseSync. The ones that have defaults are listed with = <default value>.

rootPath = process.cwd()

This can be used to change the default location of the configuration files. This can be changed to say the $HOME environment variable to read from user's home directory.

basePath = './config/'

This is the folder + filename prefix to look for within the rootPath, for example, the default is ./config, which when combined with rootPath as well as loadOrder, we would look for the following:

<rootPath><basePath><loadOrderItem>

i.e.

$PWD/config/default
$PWD/config/<env>
$PWD/config/<hostname>
$PWD/config/local

This can be used to add a prefix to load a different set of config files, for example, if we specify ./config/foo-, we would then be loading the following:

$PWD/config/foo-default
$PWD/config/foo-<env>
$PWD/config/foo-<hostname>
$PWD/config/foo-local

This is useful for loading a secondary set of configuration files that is completely different from the primary configuration files, i.e.

var defaultConfig = Configlet.parseSync(<schema1>, {
  basePath: './config'
});

var nextConfig = Configlet.parseSync(<fooSchema>, {
  basePath: './config/foo-'
});

For files that share the same schema, use loadOrder options to control their loading instead.

loadOrder = function () { return [ < list of file names> ]; }

This is a function that returns the list of the filenames to be used for loading. By default it's list function:

function () {
  return [
    'default',
process.env.NODE_ENV || process.env.ENV || 'development',
os.hostname(),
'local'
  ];
}

You can replace it with your own custom load order function:

var res = Configlet.parseSync(<schema>, {
  loadOrder: function () { return [ ... ]; }
});

extMap = { <extname>: <parser>, ... }

If you want to support additional config extension formats (like json5), you can introduce it via configExpMap as follows:

var JSON5 = require('json5');
var res = Configlet.parseSync(<schema>, {
  extMap: {
    'json5': JSON5.parse
  }
});

The added formats would be merged with the existing formats, so you can still use .json or .yaml.

argv = process.argv

Instead of using process.argv, you can supply your own commandline argument compatible arguments here. This comes in handy when you want to have custom commandline arguments.

Although the first two arguments of process.argv are the program (like node, or coffee) and the script (the main file invoked), they are removed as part of the default argument process. You do not need to pass such two arguments in if you use this option.

env = process.env

Instead of using process.env, you can supply your own environment variables here. This comes in handy when you want to have custom environment variables.