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

@justsml/auto-config

v1.1.3

Published

A Unified Config & Arguments Library for Node.js. Featuring support for environment variables, command line arguments, and JSON files!

Downloads

3

Readme

@justsml/auto-config

CI Status NPM version GitHub stars

Intro

A Unified Config & Arguments Library for Node.js!

Featuring support for environment variables, command line arguments, and (soon) JSON/YAML/INI files!

Why

There are so many config libraries, do we really need another??? Well, possibly!

No existing library I tried met my requirements.

My goals & requirements include:

  • Enable dynamic app config. See '12 Factor App' on Config
  • TypeScript support.
  • Portable pattern (not filesystem-locked, browser support.)
  • Simple, memorable & terse config format.

Table of Contents

Install

npm install @justsml/auto-config

yarn add @justsml/auto-config

Example

// `./src/config.ts`
import { autoConfig } from '@justsml/auto-config';

export default autoConfig({
  databaseUrl: {
    help: 'The Postgres connection string.',
    args: ['--databaseUrl', '--db', 'DATABASE_URL'],
    required: true,
  },
  port: {
    help: 'The port to start server on.',
    args: ['--port', '-p', 'PORT'],
    type: 'number',
    required: true,
  },
  debugMode: {
    help: 'Debug mode.',
    args: ['--debug', '-D'],
    type: 'boolean',
    default: false,
  },
});
// `./src/app.js`
import config from './config';
console.log(config);

Configuring your App Dynamically

Command line arguments

node ./src/app.js \
  --port 8080 \
  --databaseUrl 'postgres://localhost/postgres' \
  --debug
# { port: 8080, databaseUrl: 'postgres://localhost/postgres', debug: true }

Mix of environment and command arguments

DATABASE_URL=postgres://localhost/postgres \
  node ./src/app.js \
    --port 8080 \
    --debug
# { port: 8080, databaseUrl: 'postgres://localhost/postgres', debug: true }

Single-letter flag arguments

node ./src/app.js \
  -D \
  --port 8080 \
  --databaseUrl 'postgres://localhost/postgres'
# { port: 8080, databaseUrl: 'postgres://localhost/postgres', debug: true }

Error on required fields

node ./src/app.js \
  --port 8080
# Error: databaseUrl is required.

CLI Help Output

node ./src/app.js --help
╭───────────────────────────┬────────────────────────────────────────────┬──────────────────────────────────────────────────────────╮
│                           │                                            │                                                          │
│  Name                     │  Help                                      │  CLI Args, Env Name(s)                                   │
│                           │                                            │                                                          │
├───────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│databaseUrl*               │The Postgres connection string.             │--databaseUrl, DATABASE_URI, DATABASE_URL                 │
├───────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│port*                      │The port to serve content from.             │-p, --port                                                │
├───────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│[debugMode] = false        │Debug mode.                                 │-D, --debug                                               │
├───────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│help                       │Show this help.                             │--help                                                    │
├───────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────┤
│version                    │Show the current version.                   │--version                                                 │
╰───────────────────────────┴────────────────────────────────────────────┴──────────────────────────────────────────────────────────╯

TODO

  • [ ] Enum support.
  • [ ] Inverting boolean flags with --no-debug versus --debug.
  • [x] Auto --help output.
  • [x] --version output.
  • [x] default values.
  • [x] required values.
  • [x] Zod validators for optional, min, max, gt, gte, lt, lte.

Credit and References

Projects researched, with any notes on why it wasn't a good fit.

  • yargs - like the fluent API, and command syntax. Could use as base library. Env vars could be handled via default helper function to check for env keys. Or we could transform yargs config into overlapping format.
  • commander - like the many ways to configure arguments. Could probably be used as underlying library, however initial attempt was slower than starting from scratch.
  • cosmiconfig - focused too much on disk-backed config.
  • rc - focused on 'magically' locating disk-backed config.
  • node-convict - great pattern, but limited TypeScript support.
  • nconf - setter & getter, plus the hierarchy adds extra layers.
  • conf - too opinionated (writing to disk.) Interesting use of JSON Schemas, Versioning, and Migrations.
  • gluegun - great design, focused on opinionated design of CLI apps though.
  • configstore - replaced by conf.