Giter Site home page Giter Site logo

config's Introduction

@ig3/config

This packages loads configuration from command line, environment and a set of configuration files.

Install

$ npm install @ig3/config

Usage

const config = require('@ig3/config')();
console.log("config: ", JSON.stringify(config, null, 2));

The package exports a single function that takes an options argument and returns a configuration object.

Configuration is read from defaults, one or more configuration files, environment variables beginning with the name of the program and command line arguments. The same parameter may appear more than once in which case the later appearance overrides previous appearances.

Configuration

Options may be passed to the factory:

const options = {};
const config = require('@ig3/config')(options);

debug (false)

If truthy, print debug messages to console.debug.

options.debug = true;

defaults ({})

Default configuration parameters.

options.defaults = {
  option1: 'value1',
  option2: 'value2',
  option3: true,
  option4: 1234
};

argv (minimist)

Parsed arguments from process.argv, or wherever you like.

If not set, minimist is used to parse the arguments.

  require('minimist')(process.argv.slice(2), {
    string: ['config'],
    boolean: ['debug'],
    alias: {
      config: ['C'],
      debug: ['d']
    }
  });

For example, to use nopt to process command line arguments:

options.argv = nopt(knownOpts, shortHands, process.argv, 2);

name (path.basename(process.argv[1], '.js'))

The name of the program.

options.name = 'myapp';

config (undefined)

The path of a config file to load. This file, if it exists, overrides all other config files from opts.paths.

options.config = '/tmp/test/config.json';

paths

The paths to search for config files.

Default paths are:

  • /etc/<name>
  • /etc/<name>/config
  • /usr/local/etc/<name>
  • /usr/local/etc/<name>/config
  • ~/.config/<name>
  • ~/.config/<name>/config
  • ~/.<name>
  • ~/.<name>/config
  • .<name>
  • <name>

The path starting with '~' are dependent on the environment variable HOME being set. If it is not set, these paths will not be checked.

options.paths = [
  '/etc/myapp.json',
  path.join(process.env.HOME, '.myapp.json'),
  '.myapp.json'
];

The order of paths is significant. If multiple files are found their contents are merged. If the same parameter appears in more than one file, the value from files later in the list will override values from files earlier in the list. in the list of paths will override those earlier in the list.

parsers The config file parsers.

Parsers for config files are selected by filename extension. If there is no extension, the parser for the last extension in option extensions is used.

options.parsers = {
  '.json5': JSON5.parse,
  '.json': JSON.parse,
  '.ini': ini.parse
};

extensions

An array of extensions to try if a path does not end with an extension.

options.extensions = [
  '.ini',
  '.xml',
  '.json'
];

### Methods

None.

## License

MIT, see [LICENSE.md](http://github.com/ig3/agiloft-script/blob/master/LICENSE.md) for details.

## Change Log

### 0.2.2 - 20220331

Update all dependencies.

### 0.2.1 - 20220331

Fix the precedence of config option: command line; environment; options

### 0.2.0
Remove strop-json-comments and add JSON5.

### 0.1.0
Add missing dependencies

### 0.0.1

Initial release

### 0.2.3 - WIP
 * Update dependencies
 * Fix tests

config's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.