Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Mar 29, 2024
1 parent ed33feb commit 60eb032
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
48 changes: 47 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
encode: (v) => v + '',
},
},
// messages to be used for prompts
prompts: {
vv: {
message: 'Package version',
Expand All @@ -59,12 +60,57 @@ export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
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',
},
},
});


```
Lets go throught this step by step. First generic parameter which in our case is `ProjectOptions` is what will be held inside config json file in the project folder using your CLI.

Second parameter `UserOptions` is used to store global options inside users `$HOME` folder.

Then `myConfig` is the config file name. It will be stored in users who is using
Then `myConfig` is the config file name. It will be stored in users who is using the CLI that uses `config-maker`

**`decoders`** - are only needed when a value is different type that string, but we want to encode it in the config.

**`prompt`** - are optional messages that are used in text and/or in `autocomplete` prompts.

**`autocomplete`** - functions returning an array of strings to be used inside autocomplete

Then to use the value from the config you can use two functions of the config object.

**`getValue`** - get the value by key. Just to remind - it will be resolved this way:
1. Get from CMD line option if exist
2. Get from environment variable if provided
3. Get from current config if exist in
4. Get from text or autocomplete input if provided
5. If still now value - return `undefined`

```ts
//Import your created config
import {config} from './config.js'
//Get type safe value type is value type or undefined if user won't provide any input
const value = config.getValue('url')

```

**`getValueOrThrow`** - same as `getValue` but throws an error if value is not provided
2 changes: 1 addition & 1 deletion packages/config-maker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "config-maker",
"version": "0.0.1",
"version": "0.0.2",
"description": "Make interactive config for CLIs. Fetch values from command line, file, user options or environment variable.",
"main": "lib/index.js",
"author": "Artur Czemiel",
Expand Down
21 changes: 21 additions & 0 deletions packages/sandbox/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
encode: (v) => v + '',
},
},
// messages to be used for prompts
prompts: {
vv: {
message: 'Package version',
Expand All @@ -24,4 +25,24 @@ export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
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',
},
},
});

0 comments on commit 60eb032

Please sign in to comment.