@meltstudio/config-loader 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -76
- package/dist/index.d.ts +5 -24
- package/dist/index.js +52 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @meltstudio/config-loader
|
|
2
|
+
|
|
2
3
|
> ⚠️ **WARNING**: This project is in beta, so some features may change in the future. Use at your own discretion
|
|
3
4
|
|
|
4
5
|
## Project Description
|
|
@@ -6,6 +7,7 @@
|
|
|
6
7
|
The Config Loader package is a powerful and user-friendly tool that simplifies the process of retrieving and collecting variables from one or multiple files for your project. It provides an efficient way to extract specific information from files and access those variables in your code. The result is a JSON object, making it easy to work with in various applications.
|
|
7
8
|
|
|
8
9
|
## Features
|
|
10
|
+
|
|
9
11
|
- Retrieve and collect variables from one or multiple files in your project.
|
|
10
12
|
- YAML file support (support for other file types coming soon.)
|
|
11
13
|
- Data can also be retrieved from CLI or environment variables .
|
|
@@ -25,9 +27,11 @@ To install the project, you can use the following steps:
|
|
|
25
27
|
1. Ensure that you have [Node.js](https://nodejs.org/) installed on your machine.
|
|
26
28
|
2. Open a terminal or command prompt.
|
|
27
29
|
3. Run the following command to install the project and its dependencies via npm:
|
|
30
|
+
|
|
28
31
|
```bash
|
|
29
32
|
$ npm install @meltstudio/config-loader
|
|
30
33
|
```
|
|
34
|
+
|
|
31
35
|
```bash
|
|
32
36
|
$ yarn add @meltstudio/config-loader
|
|
33
37
|
```
|
|
@@ -39,48 +43,45 @@ Here's an example of how to use the `@meltstudio/config-loader` package in a Typ
|
|
|
39
43
|
```typescript
|
|
40
44
|
import path from "path";
|
|
41
45
|
|
|
42
|
-
import
|
|
46
|
+
import c from "@meltstudio/config-loader";
|
|
43
47
|
|
|
44
48
|
const run = (): void => {
|
|
45
|
-
const settings =
|
|
46
|
-
{
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
defaultValue: "www.mywebsite.dev",
|
|
53
|
-
}),
|
|
54
|
-
description: option.string({ required: true }),
|
|
55
|
-
isProduction: option.bool({ required: true }),
|
|
56
|
-
},
|
|
57
|
-
database: {
|
|
58
|
-
host: option.string({ required: true }),
|
|
59
|
-
port: option.number({ required: true }),
|
|
60
|
-
credentials: {
|
|
61
|
-
username: option.string(),
|
|
62
|
-
password: option.string(),
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
socialMedia: option.array({
|
|
66
|
-
required: true,
|
|
67
|
-
item: option.string({ required: true }),
|
|
68
|
-
}),
|
|
69
|
-
features: option.array({
|
|
70
|
-
required: true,
|
|
71
|
-
item: {
|
|
72
|
-
name: option.string(),
|
|
73
|
-
enabled: option.bool(),
|
|
74
|
-
},
|
|
49
|
+
const settings = c.schema({
|
|
50
|
+
version: c.string({ required: true, cli: true }),
|
|
51
|
+
website: {
|
|
52
|
+
title: c.string({ required: true }),
|
|
53
|
+
url: c.string({
|
|
54
|
+
required: false,
|
|
55
|
+
defaultValue: "www.mywebsite.dev",
|
|
75
56
|
}),
|
|
57
|
+
description: c.string({ required: true }),
|
|
58
|
+
isProduction: c.bool({ required: true }),
|
|
76
59
|
},
|
|
77
|
-
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
60
|
+
database: {
|
|
61
|
+
host: c.string({ required: true }),
|
|
62
|
+
port: c.number({ required: true }),
|
|
63
|
+
credentials: {
|
|
64
|
+
username: c.string(),
|
|
65
|
+
password: c.string(),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
socialMedia: c.array({
|
|
69
|
+
required: true,
|
|
70
|
+
item: c.string({ required: true }),
|
|
71
|
+
}),
|
|
72
|
+
features: c.array({
|
|
73
|
+
required: true,
|
|
74
|
+
item: {
|
|
75
|
+
name: c.string(),
|
|
76
|
+
enabled: c.bool(),
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
const config = settings.load({
|
|
81
|
+
env: false,
|
|
82
|
+
args: true,
|
|
83
|
+
files: path.join(__dirname, "./config.yaml"),
|
|
84
|
+
});
|
|
84
85
|
console.log(JSON.stringify(config, null, 2));
|
|
85
86
|
};
|
|
86
87
|
|
|
@@ -88,6 +89,7 @@ run();
|
|
|
88
89
|
```
|
|
89
90
|
|
|
90
91
|
With a config.yaml file with the following contents:
|
|
92
|
+
|
|
91
93
|
```yaml
|
|
92
94
|
version: 1.0.0
|
|
93
95
|
website:
|
|
@@ -117,6 +119,7 @@ apiKeys:
|
|
|
117
119
|
```
|
|
118
120
|
|
|
119
121
|
The expected output would be:
|
|
122
|
+
|
|
120
123
|
```json
|
|
121
124
|
{
|
|
122
125
|
"version": "1.0.0",
|
|
@@ -156,84 +159,93 @@ You can try executing our example in your project by following these steps with
|
|
|
156
159
|
```bash
|
|
157
160
|
yarn example:run
|
|
158
161
|
```
|
|
162
|
+
|
|
159
163
|
### Usage with CLI
|
|
164
|
+
|
|
160
165
|
When using our package with cli, it is important to have the cli attribute set to true.
|
|
161
166
|
This will allow values to be sent when running the package from the command line.
|
|
167
|
+
|
|
162
168
|
```typescript
|
|
163
169
|
import path from "path";
|
|
164
170
|
|
|
165
|
-
import
|
|
171
|
+
import c from "@meltstudio/config-loader";
|
|
166
172
|
|
|
167
173
|
const run = (): void => {
|
|
168
|
-
const settings =
|
|
169
|
-
{
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
const config = settings.get();
|
|
174
|
+
const settings = c.schema({
|
|
175
|
+
version: c.string({
|
|
176
|
+
required: true,
|
|
177
|
+
cli: true, 👈
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
const config = settings.load({
|
|
181
|
+
env: false,
|
|
182
|
+
args: true,
|
|
183
|
+
files: path.join(__dirname, "./config.yaml"),
|
|
184
|
+
});
|
|
182
185
|
console.log(JSON.stringify(config, null, 2));
|
|
183
186
|
};
|
|
184
187
|
|
|
185
188
|
run();
|
|
186
189
|
```
|
|
187
|
-
|
|
190
|
+
|
|
191
|
+
To use it you need to send the property name on the command line with the new value
|
|
192
|
+
|
|
188
193
|
```bash
|
|
189
194
|
yarn example:run --version 2.0.0
|
|
190
195
|
```
|
|
196
|
+
|
|
191
197
|
Having the following config.yaml file:
|
|
198
|
+
|
|
192
199
|
```yaml
|
|
193
200
|
version: 1.0.0
|
|
194
201
|
```
|
|
202
|
+
|
|
195
203
|
The expected output would be:
|
|
204
|
+
|
|
196
205
|
```json
|
|
197
206
|
{
|
|
198
|
-
"version": "2.0.0"
|
|
207
|
+
"version": "2.0.0"
|
|
199
208
|
}
|
|
200
209
|
```
|
|
210
|
+
|
|
201
211
|
You can see that the CLI variable overrode the yaml file variable
|
|
212
|
+
|
|
202
213
|
### Usage with Environment Variables
|
|
214
|
+
|
|
203
215
|
The Config Loader package allows you to use environment variables in your system configuration. You can specify variable names in your configuration and get them. To use this feature you need to set **env: true**
|
|
216
|
+
|
|
204
217
|
```typescript
|
|
205
218
|
import path from "path";
|
|
206
219
|
|
|
207
|
-
import
|
|
220
|
+
import c from "@meltstudio/config-loader";
|
|
208
221
|
|
|
209
222
|
const run = (): void => {
|
|
210
|
-
const settings =
|
|
211
|
-
{
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}),
|
|
221
|
-
},
|
|
223
|
+
const settings = c.schema({
|
|
224
|
+
database: {
|
|
225
|
+
host: c.string({ required: true }),
|
|
226
|
+
port: c.number({ required: true }),
|
|
227
|
+
credentials: {
|
|
228
|
+
username: c.string(),
|
|
229
|
+
password: c.string({
|
|
230
|
+
env: "DB_PASSWORD",
|
|
231
|
+
cli: true,
|
|
232
|
+
}),
|
|
222
233
|
},
|
|
223
234
|
},
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
);
|
|
230
|
-
const config = settings.get();
|
|
235
|
+
});
|
|
236
|
+
const config = settings.load({
|
|
237
|
+
env: true, 👈
|
|
238
|
+
args: true,
|
|
239
|
+
files: path.join(__dirname, "./config.yaml"),
|
|
240
|
+
});
|
|
231
241
|
console.log(JSON.stringify(config, null, 2));
|
|
232
242
|
};
|
|
233
243
|
|
|
234
244
|
run();
|
|
235
245
|
```
|
|
246
|
+
|
|
236
247
|
With the following config.yaml file:
|
|
248
|
+
|
|
237
249
|
```yaml
|
|
238
250
|
database:
|
|
239
251
|
host: localhost
|
|
@@ -242,10 +254,13 @@ database:
|
|
|
242
254
|
username: admin
|
|
243
255
|
password: IGNORED_PASSWORD
|
|
244
256
|
```
|
|
257
|
+
|
|
245
258
|
```bash
|
|
246
259
|
yarn example:run
|
|
247
260
|
```
|
|
261
|
+
|
|
248
262
|
If you have the environment variable `DB_PASSWORD=ENV_USED_PASSWORD`, the expected output would be:
|
|
263
|
+
|
|
249
264
|
```json
|
|
250
265
|
{
|
|
251
266
|
"database": {
|
|
@@ -258,6 +273,9 @@ If you have the environment variable `DB_PASSWORD=ENV_USED_PASSWORD`, the expect
|
|
|
258
273
|
}
|
|
259
274
|
}
|
|
260
275
|
```
|
|
276
|
+
|
|
261
277
|
You can notice that the environment variable overrode the value in the config.yaml file
|
|
278
|
+
|
|
262
279
|
## License
|
|
280
|
+
|
|
263
281
|
This package is licensed under the Apache License 2.0. For more information, please see the [LICENSE](./LICENSE) file.
|
package/dist/index.d.ts
CHANGED
|
@@ -9,9 +9,6 @@ declare class ConfigNode {
|
|
|
9
9
|
constructor(value: Value | ArrayValue, path: string, source_type: SourceTypes, file: string | null, variable_name: string | null, arg_name: string | null);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
type NodeTree = {
|
|
13
|
-
[key: string]: NodeTree | ConfigNode;
|
|
14
|
-
};
|
|
15
12
|
type SettingsSources<T> = {
|
|
16
13
|
env: boolean;
|
|
17
14
|
args: boolean;
|
|
@@ -85,27 +82,10 @@ declare class PrimitiveOption extends OptionBase {
|
|
|
85
82
|
|
|
86
83
|
type OptionTypes = PrimitiveOption | ArrayOption;
|
|
87
84
|
|
|
88
|
-
declare class
|
|
85
|
+
declare class SettingsBuilder {
|
|
89
86
|
private readonly schema;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
private argsData;
|
|
93
|
-
private envData;
|
|
94
|
-
private optionsTree;
|
|
95
|
-
private defaultData;
|
|
96
|
-
private program;
|
|
97
|
-
constructor(schema: Node, sources: SettingsSources<T>);
|
|
98
|
-
private validateFiles;
|
|
99
|
-
private load;
|
|
100
|
-
private traverseOptions;
|
|
101
|
-
private buildOption;
|
|
102
|
-
private getValidatedArray;
|
|
103
|
-
private processArrayWithSchema;
|
|
104
|
-
private setOption;
|
|
105
|
-
private addArg;
|
|
106
|
-
private getValuesFromTree;
|
|
107
|
-
get(): T;
|
|
108
|
-
getExtended(): NodeTree;
|
|
87
|
+
constructor(schema: Node);
|
|
88
|
+
load<T>(sources: SettingsSources<T>): T;
|
|
109
89
|
}
|
|
110
90
|
|
|
111
91
|
interface OptionPropsArgs {
|
|
@@ -125,6 +105,7 @@ declare const option: {
|
|
|
125
105
|
number: (opts?: OptionPropsArgs) => PrimitiveOption;
|
|
126
106
|
bool: (opts?: OptionPropsArgs) => PrimitiveOption;
|
|
127
107
|
array: (opts: ArrayOptionPropsArgs) => ArrayOption;
|
|
108
|
+
schema: (theSchema: Node) => SettingsBuilder;
|
|
128
109
|
};
|
|
129
110
|
|
|
130
|
-
export {
|
|
111
|
+
export { option as default };
|
package/dist/index.js
CHANGED
|
@@ -35,29 +35,13 @@ var __publicField = (obj, key, value) => {
|
|
|
35
35
|
// src/index.ts
|
|
36
36
|
var src_exports = {};
|
|
37
37
|
__export(src_exports, {
|
|
38
|
-
default: () => src_default
|
|
39
|
-
option: () => option
|
|
38
|
+
default: () => src_default
|
|
40
39
|
});
|
|
41
40
|
module.exports = __toCommonJS(src_exports);
|
|
42
41
|
|
|
43
|
-
// src/
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// src/option/arrayOption.ts
|
|
48
|
-
var ArrayValueContainer = class {
|
|
49
|
-
val;
|
|
50
|
-
item;
|
|
51
|
-
constructor(item, val) {
|
|
52
|
-
this.val = val;
|
|
53
|
-
this.item = item;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
var arrayOption_default = ArrayValueContainer;
|
|
57
|
-
|
|
58
|
-
// src/option/base.ts
|
|
59
|
-
var fs = __toESM(require("fs"));
|
|
60
|
-
var import_js_yaml = __toESM(require("js-yaml"));
|
|
42
|
+
// src/settings.ts
|
|
43
|
+
var import_commander = require("commander");
|
|
44
|
+
var fs2 = __toESM(require("fs"));
|
|
61
45
|
|
|
62
46
|
// src/nodes/configNode.ts
|
|
63
47
|
var ConfigNode = class {
|
|
@@ -78,6 +62,34 @@ var ConfigNode = class {
|
|
|
78
62
|
};
|
|
79
63
|
var configNode_default = ConfigNode;
|
|
80
64
|
|
|
65
|
+
// src/nodes/configNodeArray.ts
|
|
66
|
+
var ConfigNodeArray = class {
|
|
67
|
+
arrayValues;
|
|
68
|
+
constructor(arrayValues) {
|
|
69
|
+
this.arrayValues = arrayValues;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var configNodeArray_default = ConfigNodeArray;
|
|
73
|
+
|
|
74
|
+
// src/types.ts
|
|
75
|
+
var InvalidValue = class {
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/option/arrayOption.ts
|
|
79
|
+
var ArrayValueContainer = class {
|
|
80
|
+
val;
|
|
81
|
+
item;
|
|
82
|
+
constructor(item, val) {
|
|
83
|
+
this.val = val;
|
|
84
|
+
this.item = item;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var arrayOption_default = ArrayValueContainer;
|
|
88
|
+
|
|
89
|
+
// src/option/base.ts
|
|
90
|
+
var fs = __toESM(require("fs"));
|
|
91
|
+
var import_js_yaml = __toESM(require("js-yaml"));
|
|
92
|
+
|
|
81
93
|
// src/utils.ts
|
|
82
94
|
function valueIsInvalid(val) {
|
|
83
95
|
return val instanceof InvalidValue || val === null || val === void 0;
|
|
@@ -410,19 +422,6 @@ var ArrayOption = class extends OptionBase {
|
|
|
410
422
|
var PrimitiveOption = class extends OptionBase {
|
|
411
423
|
};
|
|
412
424
|
|
|
413
|
-
// src/settings.ts
|
|
414
|
-
var import_commander = require("commander");
|
|
415
|
-
var fs2 = __toESM(require("fs"));
|
|
416
|
-
|
|
417
|
-
// src/nodes/configNodeArray.ts
|
|
418
|
-
var ConfigNodeArray = class {
|
|
419
|
-
arrayValues;
|
|
420
|
-
constructor(arrayValues) {
|
|
421
|
-
this.arrayValues = arrayValues;
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
var configNodeArray_default = ConfigNodeArray;
|
|
425
|
-
|
|
426
425
|
// src/settings.ts
|
|
427
426
|
var Settings = class {
|
|
428
427
|
schema;
|
|
@@ -433,8 +432,8 @@ var Settings = class {
|
|
|
433
432
|
optionsTree = {};
|
|
434
433
|
defaultData = {};
|
|
435
434
|
program;
|
|
436
|
-
constructor(
|
|
437
|
-
this.schema =
|
|
435
|
+
constructor(schema2, sources) {
|
|
436
|
+
this.schema = schema2;
|
|
438
437
|
this.sources = sources;
|
|
439
438
|
this.program = new import_commander.Command().allowUnknownOption(true).allowExcessArguments(true);
|
|
440
439
|
this.load();
|
|
@@ -645,8 +644,19 @@ var Settings = class {
|
|
|
645
644
|
};
|
|
646
645
|
var settings_default = Settings;
|
|
647
646
|
|
|
647
|
+
// src/builder/settings.ts
|
|
648
|
+
var SettingsBuilder = class {
|
|
649
|
+
schema;
|
|
650
|
+
constructor(schema2) {
|
|
651
|
+
this.schema = schema2;
|
|
652
|
+
}
|
|
653
|
+
load(sources) {
|
|
654
|
+
const settings = new settings_default(this.schema, sources);
|
|
655
|
+
return settings.get();
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
|
|
648
659
|
// src/index.ts
|
|
649
|
-
var src_default = settings_default;
|
|
650
660
|
var DEFAULTS = {
|
|
651
661
|
required: false,
|
|
652
662
|
env: null,
|
|
@@ -681,14 +691,15 @@ var array = (opts) => {
|
|
|
681
691
|
...opts
|
|
682
692
|
});
|
|
683
693
|
};
|
|
694
|
+
var schema = (theSchema) => {
|
|
695
|
+
return new SettingsBuilder(theSchema);
|
|
696
|
+
};
|
|
684
697
|
var option = {
|
|
685
698
|
string,
|
|
686
699
|
number,
|
|
687
700
|
bool,
|
|
688
701
|
// object,
|
|
689
|
-
array
|
|
702
|
+
array,
|
|
703
|
+
schema
|
|
690
704
|
};
|
|
691
|
-
|
|
692
|
-
0 && (module.exports = {
|
|
693
|
-
option
|
|
694
|
-
});
|
|
705
|
+
var src_default = option;
|
package/package.json
CHANGED