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

confirge

v0.4.0

Published

Flexible configuration!

Downloads

19

Readme

confirge

NPM Version Linux Build Windows Build Test Coverage Dependency Status

Flexible configuration!

Confirge aims to make configuration of modules easy and flexible. It supports different ways of creating config objects or reading config files. To make things even more flexible you can make use of a simple 'variable template system' to replace often used strings (eg. directories/names) in your config objects.

Installation

npm install --save confirge

How to use

var confirge = require('confirge');

// basic usage, load from a file (YAML or JSON)
var config = confirge('config.yml');

// load from a file, returned by a function
config = confirge(function()
{
    return 'config.json';
});

// extend objects
config = confirge.extend(config, {
    'example': '%var1% and %var2%'
});

// will replace vars inside the config obj, eg. %var1%, %var2%
// this will result in { 'example': 'value1 and value2' }
config = confirge.replace(config, {
    'var1': 'value1',
    'var2': 'value2'
});

API


confirge(source)

Handles a string (file path), function, or object source and returns an object.

argument | type | description ---------|------|------------ source | string, function or object | The source to read from.

When passing a string, it is assumed it is the path to a file. When not absolute, the path will be used relative from process.cwd(). A function will be executed and it's result will be used. This result can be one of the accepted values, string, function or object. Objects are just returned the same as they came in.


confirge.read(file, extensions)

Reads a file and returns an object. Returns false on failure. When a function is passed, it is assumed it returns the path to a file wich should be read.

argument | type | description ---------|------|------------ file | string or function | The source to read from. extensions | array | Optional alternative file extensions to read.

When passing a string, it is assumed it is the path to a file. When not absolute, the path will be used relative from process.cwd(). A function will be executed and it's result will be used. This result can be one of the accepted values, string or function.

When reading the main file failes and the optional parameter extensions is passed, the function will try to read alternative files with the specified extensions from this array. Example:

confirge.read('.awesome-config', ['json', 'yml', 'js']);

When .awesome-config does not exist, it will try to read a file with any of the alternative file extensions, in order of the values in the array: .awesome-config.json then .awesome-config.yml and finally .awesome-config.js. The first file that succeeds will be returned.


confirge.replace(source, vars)

Loops through all (nested) source values and replaces any found variables.

argument | type | description ---------|------|------------ source | object or array | The function will loop through the values and replace any found vars (eg. %dir%) for their values. Multilevel objects and arrays are supported. vars | object | An object with variables. Multilevel objects are supported.

var source = {
    'config-option':   '%some-var%',
    'config-option2':  '%another.var%',
    'other-option':    true,
    'supported-types': ['object', '%types.a%']
};

var vars = {
    'some-var':    'some-value',    // %some-var%
    'another.var': 'another value', // %another.var%
    'types':       { 'a': 'array' } // %types.a%
};

var result = confirge.replace(source, vars);

// the result will be:
result = {
    'config-option':   'some-value',
    'config-option2':  'another value',
    'other-option':    true,
    'supported-types': ['object', 'array']
};

confirge.extend(source...)

Extend a base object with the given sources. These sources are handled by the main confirge function and are only used if objects are returned.

argument | type | description ---------|------|------------ source | string, function or object | A base object. ... | string, function or object | A source to extend the base object with.