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

flatconfig

v0.4.4

Published

Simple node.js config file and arguments parser.

Downloads

3

Readme

Flat Config Parser

The goal of this project is to provide a simple module that can load a JSON defaults file, read a flat configuration file and parse command line arguments.

Synopsis

Usually an application needs a basic defaults structure, some config files that override defaults and command line arguments that override the resulting object.

The usual scenario is when Flat Config loads a JSON file for defaults, then loads a key-subkey=value style configuration file and mixes that with command line arguments.

You may also provide Flat Config with any existing javascript object and it will work. You can also provide an array of configuration entries instead of the config file if you feel more comfortable with that.

API

A basic example, the defaults:

{
    "app": {
        "mainmodule": "lib/main", 
        "port": 8080
    }, 
    "limit": {
        "connections": 30,
        "entries": 4096
    },
    "quiet": true,
    "test": true
}

The config file (/etc/myapp/myconfig):

# a comment
app-port=3080
# another comment
limit-connections=20
entries=1024

The application loader (say app.js):

// Initialize flatconfig
var flatconfig = require('flatconfig');

// Load the configuration
var args = flatconfig.parseArgs(process.argv.slice(2)),
    config = flatconfig.loadConfig(
              path.resolve(__dirname, 'cfg.json'), 
              path.resolve(process.cwd(), args['config']),
              args),

var app = require(config.app.mainmodule);
app.init(config);

The invocation:

node app --config=/etc/myapp/myconfig --no-test --no-quiet --app-port=4080

The arguments/config file parser

The parser is based on traditional unix 'long-style' arguments, something like --a-key=a\ value.

Every hyphen in the config/argument name causes the parser will attempt to descend to the child object.

If the argument name starts with --no-* the parser will set the value to false.

If the value is not given (--argument) the parser will set the value to true.

See the example above.

flatconfig.loadConfig(defaults, [config, [args]])

Loads or uses the defaults, configuration and arguments. Returns the resulting config object.

Parameters are:

  • {Object|String} defaults: the base object to load
    • if an object it passed it will modify it
    • if a string is passed it will attempt to load a JSON file
  • {Object|String} config: the configuration to mixin
    • a object listing the configuration directives is expected, or
    • a path pointing to the configuration file (absolute)
    • the argument is optional, empty values will be ignored
  • {Array} args: the command line arguments
    • the command line arguments
    • if not given the default value is process.argv.slice(2)

flatconfig.parseArgs(args, [flatobj])

Parses the arguments to a flat hash.

Parameters:

  • {Array} args: an argument list array
  • {Object} flatobj: optional existing object to fill

flatconfig.flatten(obj, [args, [prefix]])

This is a helper function to flatten an object to a flat parameter hash.

Might be helpful to create a basic config file by listing all parameters within the defaults file.

Parameters:

  • {Object} obj: object to flatten
  • {Array} args: an optional object to build.
  • {String} prefix: an optional prefix to start with (default: '--')

flatconfig.deflatten(args, [obj])

This is a helper function to deflatten a flat parameter hash to an object.

Might be helpful to create a basic config file by listing all parameters within the defaults file.

Parameters:

  • {Object} obj: object to flatten
  • {Array} args: an optional object to build.

Todo

Some thing still need work, especially:

  • array handling (currently not even tested)
  • loading multiple config files (currently achievable using parseArgs)
  • better comments within the config files

License

(The MIT License)

Copyright (c) 2012 Michał Czapracki [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.