0.0.4 • Published 8 years ago

spiny-config v0.0.4

Weekly downloads
4
License
ISC
Repository
github
Last release
8 years ago

Spiny Config

Environment-base configuration loader.

Install

npm i spiny-config --save

Usage

const conf = new Config({
  basePath: BASE_PATH,
  env: 'dev'
});

conf.load('path/to/config1', 'path/to/config2').then(() => {
   console.log('Loaded config : %j', conf);  // will invoke conf.toJSON()

   let value = conf.get('some.config.key');

   // ...
}).catch(err => {
  console.error(err.stack || err);
});

API

  • constructor(options)

    Create a new Config instance.

    • basePath : the working directory to use as base path when loading from relative paths.
    • env : defines the environment value when loading configuration. This value may specify a partial environment value. For example, prod may load files such as something.stagin3.production.json, or another.prod.rc.json, etc. However, files such as prod.json or production.json would not work as the environment value may not be the first token in the file name.
  • load(...dirs) {Promise}

    Load the specified configuration paths, and merge the results. The first token of the file name act as a configuration key. In as much, each directory name also act as configuration keys. Such that ./foo/bar/buz/meh.json should produce the configuration object

    {
      "foo": {
        "bar": {
          "buz": {
            "meh": {
              // content of meh.json
            }
          }
        }
      }
    }

    Note: comments are allowed in configuration files.

    Subsequent tokens in the file name, before the extension (i.e. .json) defines the configuration environment.For example, given that an env value of dev is provided, the following configuration file would not be loaded : foo.stag.prod.json, but this one would : foo.production.development.json.

  • get(key) {any}

    Get the given configuration key and return the associated value.

  • toJSON() {object}

    Return the full configuration object. Modifying this object should have no impact whatsoever to the Config instance, as the returned object is a cloned of the original loaded data.

Contribution

All contributions welcome! Every PR must be accompanied by their associated unit tests!

License

Copyright (c) 2016, Yanick Rochon

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

0.0.4

8 years ago

0.0.3

8 years ago