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

seraphim

v0.1.0

Published

Configuration loader

Downloads

3

Readme

seraphim

Configuration loader

NPM version Build Status Dependency Status

NPM installation

Loading configuration files in Node.js have always been a very tedious task, especially when you need to merge objects in cascade, load files asynchronously, load data from external sources like a database, redis, etc., read the CLI options and the environment variables, etc.

This module brings to you a powerful API for loading and merging your configuration data in a few lines.

var seraphim = require ("seraphim");

seraphim.createVault ()
    .on ("error", function (error){
      console.error (error);
    })
    .on ("end", function (config){
      console.log (config);
      /*
      {
        web: {
          log: {
            type: "circular",
            backup: "1week"
          },
          hostname: "a.b.c",
          port: 1234
        }
      }
      */
    })
    //Default settings
    .load ("default.json")
    //NODE_ENV (development) settings
    .load (process.env.NODE_ENV + ".json")
    //Override/merge the previous settings with any object, eg: CLI options
    .load ({ web: { hostname: "a.b.c" } });
//default.json
{
  "web": {
    "log": {
      "type": "circular",
      "backup": "1week"
    }
  }
}
//development.json
{
  "web": {
    "hostname": "1.2.3.4",
    "port": 1234
  }
}

Check this complete example for further details.

Functions

Objects


module.createVault([options]) : Seraphim

Returns a new Seraphim instance.

Options are:

  • extensionError - Boolean
    Set it to false if unknown extensions shouldn't emit an error. Default is true.

Seraphim

Events

Methods


end

Arguments: config.

This event is emitted multiple times, when there are no more pending tasks to load.

config is the final merged object.

Look at the end-event.js example for further details.

error

Arguments: error.

Emitted when an error occurs.


Seraphim#extension(extension, fn) : Seraphim

Allows you to load files with an extension different from .json using the load() function.

extension is a string or an array of strings.

fn is the function that is called when the file to load has the same extension. It has two arguments: the path of the file and a callback. The callback must be called with two parameters: the error and the object with the configuration data.

seraphim.createVault ()
    .on ("error", function (error){
      console.error (error);
    })
    .on ("end", function (config){
      ...
    })
    .extension ([".yaml", ".yml"], function (p, cb){
      fs.readFile (p, { encoding: "utf8" }, function (error, data){
        if (error) return cb (error);
        var obj = parseFile (data);
        cb (null, obj);
      });
    })
    .load ("file1.yaml")
    .load ("file2.yml");

Seraphim#get() : Object

Returns the internal merged object.

Seraphim#load(resource[, onLoad]) : Seraphim

Loads and merges a resource. resource can be a string, object or function.

String

It must be a valid file path.

.load ("file.json");

Object

.load ({ a: { b: 1 } });

Function

Synchronous. Return the object to be merged. Errors thrown here are catched and forwarded to the error event. If a falsy value is returned (null, undefined, false, etc.) it won't be merged.

.load (function (){
  if (condition){
    return { a: 1 };
  }
});

Asynchronous. Use the callback to load the next resource. The first parameter is the error, the second is the object to be merged.

.load (function (cb){
  process.nextTick (function (){
    cb (null, { a: 1 });
  });
});

onLoad is a callback that is executed when load() finishes. It has two arguments: the object to be merged and a callback. The callback allows you to execute any asynchronous function between two load() calls. Please note that if you use the onLoad callback the object is not merged automatically and you'll need to merge it explicitly. This callback it's also try-catched, errors thrown inside the onLoad callback are redirected to the error event.

.load ("file.json", function (o, cb){
  //'o' is the json object
  var me = this;
  asyncFn (function (error, foo){
    //The error is forwarded to the "error" event
    if (error) return cb (error);
    if (foo) o.bar = "baz";
    me.merge (o);
    cb ();
  });
});

The onLoad function can be used to load files without an extension.

Seraphim#merge(o1[, o2]) : undefined | Object

If o2 is not used, o1 is merged with the internal object.
If o2 is used, o2 is merged with o1 and o1 is returned.

console.log (vault.merge ({ a: 1, b: 1 }, { a: 2 }));
//{ a: 2, b: 1 }