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

simple-config-loader

v0.12.0

Published

Simple configuration loader module

Downloads

13

Readme

simple-config-loader

Simple configuration loader module for Node.js applications.

Getting Started

Install the module with: npm install simple-config-loader.

This module will load all javascript files found in a config directory, and merge them into a single object. It will use the files name as it's key.

By default, this module loads the package.json file and makes it available under the package key. It also provides an environment key that takes the value of process.env.NODE_ENV.

Consider a file config/app.js with the following exports:

module.exports = {
    name: '${package.name}',
    version: '${package.version}',
};

This will be available in the app key of the final configuration object.

It also supports interpolating values that contain references to other values in the final configuration object.

So, the expression '${package.name}' will be resolved by looking up the name attribute of the package key.

NOTE You can use envset to manage your environment variables.

Example

If you have three files inside a config directory:

app.js

module.exports = {
    name: '${package.name}',
    version: '${package.version}',
};

orm.js

module.exports = {
    database: '${app.name}',
    redis:{
        port: 27012,
        host: process.env.NODE_REDIS_HOST,
        auth: process.env.NODE_REDIS_PORT
    }
};

amqp.js

module.exports = {
    topic: '${app.name}.updates',
    exchange: '${app.name}.${environment}',
    url: process.env.NODE_AMQP_URL
};

module.exports.afterSolver = function(config){
    config.set('amqp.amqp', require('amqp'));
};
var config = require('..')({
    basepath: __dirname,
    packagePath: '../package'
});

//remove noise...
delete config.data.package;

console.log(JSON.stringify(config.data, null, 4));

The output you get:

{
    "environment": "production",
    //package:... deleted
    "amqp": {
        "topic": "simple-config-loader.updates",
        "exchange": "simple-config-loader.production",
        "url": "amqp://localhost:34579/xhflmar"
    },
    "app": {
        "name": "simple-config-loader",
        "version": "0.0.0"
    },
    "orm": {
        "database": "simple-config-loader",
        "redis": {
            "port": 27012,
            "host": "192.168.23.3",
            "auth": "admin"
        }
    }
}

Options

var DEFAULTS = {
    strict: true,
    logger: console,
    autoinitialize: true,
    config: {
        environment: process.env.NODE_ENV || 'production'
    },
    packagePath: '../package',
    configsPath: './',
    basepath: _resolve(_join(__dirname, '../../../')),
    dirname: 'config',
    globOptions: {
        matchPatterh: '**.js',
        ignorePattern: 'index.js'
    },
    getConfigFiles: function(){
        var target = _join(this.basepath, this.dirname, this.globOptions.matchPatterh);
        var ignore = _join(this.basepath, this.dirname, this.globOptions.ignorePattern);
        var files = glob.sync(target, {ignore: ignore});

        if(!files || files.length === 0){
            var msg = 'Not config files found at ' + target;
            if(this.strict) throw new Error(msg);
            else console.log(msg);
        }
        return files;
    },
    getPackage: function(data, path){
        try{
            data.package = require(path);
            delete data.package.readme;
        } catch(e) {
            this.logger.error('Error loading package');
            data = {};
        }
        return data;
    }
};

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2016-07-26 - V5.1: Pass section name to afterSolver
  • 2016-10-06 - V6.1: Updated dependencies

License

Copyright (c) 2016 goliatone
Licensed under the MIT license.