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

json-confdir

v2.1.1

Published

Loads directory with .json files into one configuration object

Downloads

5

Readme

JSON Configuration Directory reader

This library is intended to assemble a configuration object from multiple directories and chunks.

Written in TypeScript 3 and compatible with any Node.js version starting from 8.x (generated with "ES6" TS compile target).

Installation

$ yarn add json-confdir

or

$ npm install --save json-confdir

Async Usage

Library exports a class ConfDirReader which is pretty simple to use. It exposes load() method which returns Promise.

// import a class from library
const ConfDirReader = require("json-confdir");

// create reader instance
const reader = new ConfDirReader(false); // First argument is the `throwOnError` flag which is set to `false` by default

// ask reader to load JSON-files from a directory
// Method signature is: `ConfDirReader.load(String | [String]): Promise<any>`
reader.load("/path/to/config/dir").then(cfg => {
    // You configuration object is in cfg variable.
    // Do whatever your want...
}).catch(err => {
    console.error(`Cannot load config ${err.stack || err.message}`);
});

Sync Usage (new in 2.1.x)

// import a class from library
const SyncDirReader = require("json-confdir").SyncDirReader;

// create reader instance
const reader = new SyncDirReader(true); // First argument is the `throwOnError` flag which is set to `false` by default

try {
    // ask reader to load JSON-files from a directory
    // Method signature is: `SyncDirReader.load(String | [String]): any`
    const cfg = reader.load("/path/to/config/dir");
    // You configuration object is in cfg variable.
    // Do whatever your want...
} catch (err) {
    console.error(`Cannot load config ${err.stack || err.message}`);
}

Confuguration files

Library loads all files in the directories in the order supplied be the fs.readdir().

Every latter file contents overrides (but NOT replaces) previously loaded JSON-files. It is a good practice to prefix configuration filenames with zero-padded number like 00-default.json or 999-local_dev.overrides.json.

JSON-files are parsed with the json5 library. So your configuration files could have keys without quotes ("), comments (//) and trailing commas.

Some examples

Contents of test/1/00-test.json:

{
    "option1": "blablabla",
    "option2": 123
}

Contents of test/1/10-test.json:

{
    option3: {
        key: "value",
        second: [],
    },
    option2: 321
}

Contents of test/2/another.json:

{
    "option4": [1,2,3],
    // option5: "showld be commented out =)"
    "option1": "foorbarbaz",
    option3: {
        second: "replaced",
        addition: 1000
    }
}

These are perfectly correct configuration files which used for this library testing.

TODO

  • Add tests for throwOnError mode
  • Generalize lib with a superclass to avoid ambiguity
  • Add more complex tests
  • Add schema validation for loaded data (via joi or something like it)

Contacts

Feel free to file issues on Github.

Friend me on Facebook: https://fb.com/sitnin

Contact via Telegram: https://t.me/sitnin

License

MIT