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

connor-base-config

v4.3.0

Published

Basic configuration boilerplate used by Connor/Makeshift for his packages

Downloads

7

Readme

CircleCI Libraries.io dependency status for GitHub repo GitHub release Dependent repos (via libraries.io)

Connor's Base Config Package

I tend to use this boilerplate package for configuration. It adds a basic config object with some useful data in it, which you can then add to to have a nice global repository store. Designed to work with my logging boilerplate package.

This uses a modified version of convict but completely destroys its core principles by making the schema dynamic. I wanted this so I could stack packages on top of each other and add new schemas for each package.

The config is stored as a global symbol. Every time you require connor-base-config it will be referencing the same config variable. This means that no matter where you load config in your project, it will be available to every module that requires it.

Features

  • Follows Convict's schemas and maintains its API - Should be a drop-in replacement for most.
  • JSON5 by default
  • Stackable schema
  • Stored as a globally consistent singleton
  • Can load config from various sources (env vars, arguments, in-line)
  • Optional validation
  • Optionally load config before loading the schema
  • Proper schema support for arrays
  • Booleans work from env vars now

Install

npm install connor-base-config

Boilerplate snippet

// This can be copied into new projects as boilerplate for config
// config.js

const config = require('connor-base-config');

config.load({
    "environment.level": "production",
    "metadata.stack": {
        MyItem: true
    }
});

//Add additional values to the schema
config.addToSchema({
   test: {
     name: {
      doc: "What's the name of the task we're doing?",
      format: "String",
      default: "None",
      env: "JOB_NAME"
    }
   }
})

//Load additional config to the newly updated schema
config.load({
  test: {
      name: "Testing"
  }
})

//console.log(config.getProperties());
console.log(config.get("test.name"))

module.exports = config;

Or as a shortened example:

const config = require('connor-base-config')
               .addToSchema(require('./job_schema.json5'))
               .addToSchema(require('./task_schema.json5'))
               .set("task.name", "My Awesome Task")

How should I lay out my config?

I haven't enforced any particular layout, but my go-to is to essentially use namespaces. Each package that adds its own schema on top gets its own namespace, so you end up with something like this:

{
    metadata: {...},
    task: { //new "namespace"
        name: "Convert a file",
        format: "csv",
        csv: { //new "namespace"
            delimiter: ",",
            escapecharacter: "\""
        }
    }
}

Array schemas

You can put schemas in arrays now to make sure every item conforms to the schema:

    config.addToSchema({
        sources: {
            doc: 'A collection of data sources.',
            format: 'Array',
            default: [],
            children: {
                type: {
                    doc: 'The source type',
                    format: ['git', 'hg', 'svn'],
                    default: null
                },
                url: {
                    doc: 'The source URL',
                    format: 'String',
                    default: null
                },
                test: {
                    doc: "Test",
                    default: "Test"
                }
            }
        }
    });

    config.load({
        'sources': [
            {
                'type': 'git',
                'url': 'https://github.com/mozilla/node-convict.git'
            },
            {
                'type': 'git',
                'url': 'https://github.com/github/hub.git'
            }
        ]
    })
    
    
    console.log(config.getProperties())
    /*
        Will return:
          sources: [
            {
              type: 'git',
              url: 'https://github.com/mozilla/node-convict.git',
              test: 'Test'
            },
            {
              type: 'git',
              url: 'https://github.com/github/hub.git',
              test: 'Test'
            }
          ]
    
     */

Extra Commands

For the full list of commands and how schemas work, check Convict's repo. This fork adds the following two functions to the config prototype:

addToSchema: Takes a JSON/JSON5 blob and appends it to the current schema, re-applying all config values on top of the new schema. This can also be used to overwrite parts of the old current schema, but that isn't recommended behaviour.

updateSchema: Takes a JSON/JSON5 blob and replaces the entire current schema with it, re-applying all config values on top of the new schema.

addFormat: The same as Convict's one, except it works on an already-initialised config object.

This fork also modifies Convict's getSchema and getSchemaString to no longer transform them, so you'll get back exactly what you put in.