Giter Club home page Giter Club logo

configa's Introduction

๐Ÿ”ง Configa

Application configuration: DRY, flexible and type-safe. Pick any three.


โš ๏ธ Deprecated

This project is deprecated and no longer maintained.

Configa was originally factored out of another project, Sparkla, and intended to be a library for application configuration using TypeScript and JSON Schema. We envisaged that it would be reused for our CLI tools such as Encoda and Executa.

However, at the time of writing, we have focussed on developing command line and configuration management interfaces using Rust in the main Stencila repo (and keeping TypeScript CLIs very simple). Much of the functionality of Configa is provided by the Rust crates structopt and validate. There are probably TypeScript analogues to those if you prefer that as a development environment.


๐Ÿ“ฆ Install

npm install --save @stencila/configa

โšก Quick start

1. Define a configuration class

Configa uses Typescript classes to define configuration options. Create a file config.ts with a single class defining your application configuration e.g.

import { minimum, maximum } from '@stencila/configa/dist/define'

/**
 * myapp ${version}: ${description}
 */
export class Config {
  /**
   * An option that can be a boolean of a string
   */
  optionA: boolean | string = 'default-value'

  /**
   * An option that is not required but has additional validations
   */
  @minimum(1)
  @maximum(10)
  optionA?: number
}

2. Generate configuration schema

Generate the JSON Schema that will be used at run time to validate and document your application's options:

configa schema

3. Use your configuration in your application code

import { collectConfig, helpUsage } from '@stencila/configa/dist/run'

// App config as Typescript for compile time type-checking
import { Config } from './config'

// App config as JSON Schema for run time type-checking and help generation
import configSchema from './config.schema.json'

// Generate a typed configuration object
const { args = [], config } = collectConfig<Config>('myapp', configSchema)

// Generate help from the JSON Schema
if (args.includes('help')) console.log(helpUsage(configSchema))

4. Generate configuration documentation

In your README.md add comments to indicate where to insert documentation e.g.

<\!-- CONFIGA-TABLE-BEGIN -->
<\!-- CONFIGA-TABLE-END -->

Then run,

configa readme

๐Ÿš€ Usage

All configuration options can be set, in descending order of priority, by:

  • a command line argument e.g. --<value> <value>
  • an environment variable prefixed with CONFIGA_ e.g. CONFIGA_<option>=<value>
  • a .json or .ini configuration file, set using the --config option, or .configarc by default
Name Description Type Validators Default
appName The name of the application.1 string undefined
configPath Path to the configuration file to be parsed.2 string undefined
jsonSchemaPath Path to the JSON Schema file to be generated.3 string undefined
readmePath Path to the README file to be updated. string "README.md"
  1. Determines the expected prefix on the names of config files and environment variables. If undefined then parse the name from the package name in ./package.json.
  2. If undefined, then will search for a file config.ts in the current directory and its subdirectories.
  3. If undefined, then will be the path of the config file with extension .json.schema instead of .ts.

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.