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

config-prompt

v1.1.0

Published

Configuration store with a cli prompt and JSON Schema validation

Downloads

10

Readme

config-prompt

Configuration store with a cli prompt and JSON Schema validation. Currently supports strings, booleans and numbers. Extends configstore, which synchronously saves the configuration to ~/.config/configstore/[module-name].json on every change.

npm status Dependency status

example

The basic API is the same as configstore, but the constructor takes a name (optional) and schema (see api below).

const config = require('config-prompt')({
  a: { type: 'string', required: true },
  b: { type: 'boolean', default: true },
  c: { type: 'boolean', required: true }
})

config.set('a', 'beep')

let a = config.get('a') // beep
let all = config.all // { a: 'beep', b: true }

console.log(a, all)

Prompt the user for entries that are required and missing, which is only c continuing the example above:

config.prompt(err => {
  console.log(config.get('c'))
})

To prompt for all entries, not just the missing:

config.prompt({ all: true }, callback)

usage with gulp

Put these tasks in a config.js file.

const gulp = require('gulp')

const config = require('config-prompt')({
  myString: { type: 'string', required: true },
  myFlag:   { type: 'boolean', default: true }
})

// Show all config entries
gulp.task('config:print', done => config.print(done))

// Move the config file to trash. Asks for confirmation.
gulp.task('config:trash', done => config.trash(done))

// Prompt for missing config entries. Run this before any other task.
gulp.task('config:prompt', done => config.prompt(done))

// Allow other tasks to consume config
module.exports = config

Say some task needs the configuration. Simply require() your config.js and add config:prompt as a task dependency.

const gulp = require('gulp')
const config = require('./config')

gulp.task('build', ['config:prompt'], (done) => {
  if (config.get('myFlag') === true) {
    // something
  }

  done()
})

Then, when a user runs gulp build, the user will be asked to provide any missing configuration entries.

api

config = configPrompt([name][, schema])

The name defaults to the name of the module (in package.json) that required config-prompt - i.e. the module parent. If no schema is provided, it will guess based on current configuration entries. E.g. a string value is added to the in-memory schema as { type: 'string', required: true }.

config.set(key, value)

Set a value. Same as configstore.set(), but the key must be present in the schema. Note it does not validate on set.

config.get(key)

Get a value. Same as configstore.get().

config.all

Getter and setter for all entries. Same as configstore.all.

config.validate()

Validate current entries according to the schema. Returns an array of errors in the form of { field: 'data.myKey', message: 'is required' }

config.envify([override])

Returns an object with all entries as CONSTANT_CASE environment variables. Add extra entries in the override argument (not saved to the config). There's no enforced format for the keys. Snake, param, camel or pascal case, it's all good.

// Returns { MY_KEY: 'foo', MY_NUM: 23, SOMETHING_ELSE: 'else' }
config.set('my-key', 'foo')
config.envify({ myNum: 23, something_else: 'else' })

config.print([options,] callback)

Prints configuration to console.

options

  • env (bool, default false): print entries as CONSTANT_CASE environment variables, like config.envify()

config.prompt([options,] callback)

Prompts to overwrite or fill missing entries. Runs validation after all questions have been answered, then prompts again for invalid entries. Will be silent if there are no questions to ask.

options

  • all (bool, default false): prompt for all entries, not just those that are required and missing.
  • nodeEnv (bool, default true): prompt to set process.env.NODE_ENV to either "development" or "production", if not set.

(!) If you happened to notice the additional options in the source: those are likely to be refactored out and should not be relied upon.

config.trash(callback)

Moves the configuration file to trash after confirmation.

cli

You can manage multiple config stores with the included CLI. Install config-prompt globally and run config-prompt without any arguments to get started.

install

With npm do:

npm install config-prompt

license

MIT © ironSource