Skip to content

Commit

Permalink
removed global opts;
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Aug 1, 2024
1 parent f2b2ef6 commit ca368ee
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
8 changes: 1 addition & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ then create a config instance somewhere. For example in `config.ts` file.
```ts
import { ConfigMaker } from 'config-maker';

type UserOptions = {
token: string;
};

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

export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
export const config = new ConfigMaker<ProjectOptions>('myConfig', {
decoders: {
vv: {
decode: (v) => parseInt(v),
Expand Down Expand Up @@ -86,8 +82,6 @@ export const config = new ConfigMaker<ProjectOptions, UserOptions>('myConfig', {
```
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 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.
Expand Down
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.5",
"version": "0.0.6",
"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
35 changes: 3 additions & 32 deletions packages/config-maker/src/Configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import fs from 'fs';
import path from 'path';
import inquirer, { QuestionCollection } from 'inquirer';
import Conf from 'conf';
import AutoCompleteInputPrompt from '@/utils/AutoCompleteInputPrompt.js';
import { getEnvValue } from '@/common/envs.js';

Expand All @@ -29,16 +28,11 @@ type Coders<T> = {
};
};

type AllOptions<C, G> = {
type AllOptions<C> = {
options: Partial<C>;
globalOptions: Partial<G>;
};
export class ConfigMaker<
ConfigurationOptions extends Record<string, any>,
GlobalOptions extends Record<string, any>,
> {
export class ConfigMaker<ConfigurationOptions extends Record<string, any>> {
private options: Partial<ConfigurationOptions> = {};
private globalOptions = new Conf<GlobalOptions>({});
private projectPath = process.cwd();
constructor(
public configFileName: string,
Expand All @@ -56,23 +50,13 @@ export class ConfigMaker<
config?: {
autocomplete?: {
[P in keyof ConfigurationOptions]?: (
currentConfig: AllOptions<ConfigurationOptions, GlobalOptions>,
currentConfig: AllOptions<ConfigurationOptions>,
) => Promise<string[]>;
};
environment?: {
[P in keyof ConfigurationOptions]?: string;
};
};
global?: {
autocomplete?: {
[P in keyof GlobalOptions]?: (
currentConfig: AllOptions<ConfigurationOptions, GlobalOptions>,
) => Promise<string[]>;
};
environment?: {
[P in keyof GlobalOptions]?: string;
};
};
},
) {
this.projectPath = props?.pathToProject || this.projectPath;
Expand Down Expand Up @@ -187,7 +171,6 @@ export class ConfigMaker<
const result = await AutoCompleteInputPrompt(
await autocompleteFunction({
options: this.options,
globalOptions: this.globalOptions.store,
}),
{
message: optionsMessage || 'Autocomplete ' + key,
Expand All @@ -214,18 +197,6 @@ export class ConfigMaker<
return returnValue;
};

// get global options that are usually stored in users $HOME folder
getGlobalOptions = () => this.globalOptions;
getGlobalOptionsValue = <T extends keyof GlobalOptions>(k: T) =>
this.globalOptions.get(k);
setGlobalOptions = (opts: GlobalOptions) => this.globalOptions.set(opts);
updateGlobalOptions = (opts?: Partial<GlobalOptions>) => {
if (opts) {
this.globalOptions.set({ ...opts });
}
};
clearGlobalOptions = () => this.globalOptions.clear();
getGlobalOptionsPath = () => this.globalOptions.path;
serializeConfigAsString = () =>
Buffer.from(JSON.stringify(this.options)).toString('base64');
deserializeConfigFromString = (serialized: string) =>
Expand Down

0 comments on commit ca368ee

Please sign in to comment.