Skip to content

aexol-studio/config-maker

Repository files navigation

config maker idea Vector 902 (Stroke) (1) Vector 902 (Stroke) (1)

Config Maker is a config manager to use for your interactive CLI.


📋  Features

  • Making and consuming JSON config files
  • Inputing the value in four different ways:
    • as a CLI option
    • as user input
    • as an autocomplete prompt
    • as an environment variable

📤  How Values Are Fetched

flowchart TD
    A[Option from the Command Line] --Present--> R[Return Value]
    A --Not Present--->
    D[Environment Variable] --Present--> R
    D --Not Present--->
    E[Variable In Config File] --Present-->   R
    E --Not Present--->
    F[Prompt User for Input] --> R

    style R fill:#244d0e
Loading

📖  Step-by-Step Guide to Using Config Maker

Important

Before you start, you need to set up a few things.


First, install the config-maker package:

npm i config-maker

Second, you need to create a config instance.

You can create the config instance anywhere. For example, that can be done in the config.ts file.


See the full code below:

import { ConfigMaker } from 'config-maker';

type ProjectOptions = {
  urlOrPath: string;
  vv: number;
};

export const config = new ConfigMaker<ProjectOptions>('myConfig', {
  decoders: {
    vv: {
      decode: (v) => parseInt(v),
      encode: (v) => v + '',
    },
  },
  // messages to be used for prompts
  prompts: {
    vv: {
      message: 'Package version',
    },
    urlOrPath: {
      message: 'Provide url or path to the file',
    },
  },
  // default initial values
  defaultValues: {
    vv: 1,
  },
  config: {
    // Autocomplete functions returns possible options
    autocomplete: {
      urlOrPath: async (p) => {
        // if the property vv is already set
        if (p.options.vv === 1) {
          return ['https://aexol.com', 'https://space.com'];
        }
        return ['https://github.com', 'https://news.hacker.com'];
      },
    },
    environment: {
      // check if this env value exists
      urlOrPath: 'URL_PATH',
    },
  },
});

Follow the step-by-step tutorial for more details:

1. The first generic paremeter you will be dealing with is ProjectOptions. It will be stored inside the config json file in the project folder while using your CLI. myConfig is the config file name. It will be stored in the users folder for those who use the CLI with the setting config-maker.

import { ConfigMaker } from 'config-maker';

type ProjectOptions = {
  urlOrPath: string;
  vv: number;
};

2. Decoders

They are only needed when a value is of a different type than that string, but we want to encode it in the config.

  decoders: {
    vv: {
      decode: (v) => parseInt(v),
      encode: (v) => v + '',
    },
  },

3. Prompts

They are are optional messages that are used in text and/or in autocomplete prompts.

  // messages to be used for prompts
  prompts: {
    vv: {
      message: 'Package version',
    },
    urlOrPath: {
      message: 'Provide url or path to the file',
    },
  },
  // default initial values
  defaultValues: {
    vv: 1,
  },

4. Autocomplete

These functions are used to return an array of strings to be used inside autocomplete.

  config: {
    // autocomplete functions returns possible options
    autocomplete: {
      urlOrPath: async (p) => {
        // if the property vv is already set
        if (p.options.vv === 1) {
          return ['https://aexol.com', 'https://space.com'];
        }
        return ['https://github.com', 'https://news.hacker.com'];
      },
    },
    environment: {
      // check if this env value exists
      urlOrPath: 'URL_PATH',
    },
  },
});

Using the Values from the Config

The config object has two ways of retrieving values from the config.


  • OPTION 1:

The getValue function retrieves the value by its key.

Tip

As a reminder, it will be resolved this way:

  1. Get from CMD line option if it exists
  2. Get from environment variable if provided
  3. Get from current config if exists in it
  4. Get from text or autocomplete input if provided
  5. If still no value - return undefined

  • OPTION 2:

The getValueOrThrow function works the same as getValue but additionally throws an error if a value is not provided.

// import your created config
import {config} from './config.js'
// get type-safe; if no input is provided, the value type defaults to: value type or undefined
const value = config.getValue('url')

About

Manage configuration for your CLI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •