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

flexiconfig

v2.0.0

Published

Consume library configuration from multiple places

Downloads

590

Readme

flexiconfig

Consume library configuration from multiple places

Travis npm

If you have a library that involves large configuration, you might want to give users multiple ways to load the configuration, such as:

  • Passing an object directly to your library's function
  • Loading from a file .kittensrc
  • Reading from a property of package.json

This library allows you to set those, in any preference order you want. The first configuration method that yields a result will be returned as an object.

Installation

npm install flexiconfig

Usage

In the below example, the getConfig() function will try to load first from the opts object passed to the function, then from a file called .kittensrc in the current working directory, and finally in package.json, under the top-level key "kittens". If none of those work, it will move up one folder and try again, and on and on.

const getConfig = require('flexiconfig');

module.exports = function getKittens(opts) {
  const options = getConfig([opts, '.kittensrc', 'package.json#kittens']);
}

API

getConfig(loaders[, options])

Produce a configuration object from the first source that yields one.

  • loaders (Array): sources to try. Each item can be:
    • An object: if the object has properties, then it's a match. If it has no properties, it's skipped.
    • A string: filename to load from the current working directory, to be parsed in its entirety. It can be JSON, YML, or a JavaScript file with module.exports.
    • A string with the format package.json#[KEY], where [KEY] is a top-level key on the package.json in the current working directory.
  • options (Object): lookup configuration.
    • cwd (String): directory to look for files in. Defaults to process.cwd().
    • travel (Boolean): if no configs are found in the current working directory, the next folder up will be searched next, and on and on until something is found. Defaults to true.

Returns the first config object found based on the order of loaders. Throws an error on any of these conditions:

  • No config object is found after trying every option.
  • A matching config file is found, but can't be parsed as valid JSON or YML.
  • A matching key on package.json is found, but the value is not an object.

Hot tip! If you're okay with the process silently failing, you can supply a default config object as the last item in the loaders parameter.

const getConfig = require('flexiconfig');

module.exports = function getKittens(opts) {
  const options = getConfig([opts, '.kittensrc', 'package.json#kittens', {
    breed: 'siamese',
    age: 12,
  }]);
}

Local Development

git clone https://github.com/spacedoc/flexiconfig
cd flexiconfig
npm install
npm test

License

MIT © Geoff Kimball