Config Maker is a config manager to use for your interactive CLI.
- 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
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
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.
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',
},
},
});
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',
},
},
});
The config object has two ways of retrieving values from the config.
The getValue
function retrieves the value by its key.
Tip
As a reminder, it will be resolved this way:
- Get from CMD line option if it exists
- Get from environment variable if provided
- Get from current config if exists in it
- Get from text or autocomplete input if provided
- If still no value - return
undefined
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')