Comparing version 1.0.0 to 1.0.1
@@ -0,6 +1,8 @@ | ||
/// <reference types="node" /> | ||
export default class Config { | ||
private config; | ||
constructor(config?: NodeJS.Dict<any>); | ||
private path; | ||
private immutable; | ||
private readonly path?; | ||
private constructor(); | ||
private parseObject; | ||
private toObject; | ||
@@ -10,9 +12,12 @@ get<T = any>(key: string, defaultValue?: T): T; | ||
setImmutable(): this; | ||
getImmutable(): Boolean; | ||
getImmutable(): boolean; | ||
writeFile(path?: string): Promise<void>; | ||
writeFileSync(path?: string): void; | ||
private stringify; | ||
static parseFromFile(path: string, immutable?: Boolean): Promise<Config>; | ||
static parseFromFileSync(path: string, immutable?: Boolean): Config; | ||
parseFromFile(path?: string): Promise<void>; | ||
parseFromFileSync(path?: string): Promise<void>; | ||
private parse; | ||
static parseFromFile(path: string, immutable?: boolean): Promise<Config>; | ||
static parseFromFileSync(path: string, immutable?: boolean): Config; | ||
private static parse; | ||
} |
@@ -5,13 +5,16 @@ import { readFileSync, promises, writeFileSync } from 'fs'; | ||
export default class Config { | ||
constructor(config = {}, immutable = false, path) { | ||
constructor(config = {}) { | ||
this.config = config; | ||
this.immutable = immutable; | ||
this.path = path; | ||
this.immutable = false; | ||
for (const key in config) { | ||
const value = config[key]; | ||
if (typeof value == "object" && !Array.isArray(value) && !(value instanceof Config)) { | ||
config[key] = new Config(value, immutable); | ||
} | ||
config[key] = this.parseObject(config[key]); | ||
} | ||
} | ||
parseObject(value) { | ||
if (typeof value == "object" && !Array.isArray(value) && !(value instanceof Config)) { | ||
const cfg = new Config(value); | ||
value = cfg; | ||
} | ||
return value; | ||
} | ||
toObject() { | ||
@@ -44,2 +47,6 @@ const config = this.config; | ||
throw new ReferenceError("Config immutable"); | ||
if (typeof value == "object" && !Array.isArray(value) && !(value instanceof Config)) { | ||
const cfg = new Config(value); | ||
value = cfg; | ||
} | ||
if (this.config.hasOwnProperty(key)) | ||
@@ -61,2 +68,7 @@ this.config[key] = value; | ||
this.immutable = true; | ||
for (const key in this.config) { | ||
const value = this.config[key]; | ||
if (value instanceof Config) | ||
value.setImmutable(); | ||
} | ||
return this; | ||
@@ -83,2 +95,20 @@ } | ||
} | ||
async parseFromFile(path) { | ||
if (!path) | ||
path = this.path; | ||
const raw = await readFile(path); | ||
this.parse(raw, path); | ||
} | ||
async parseFromFileSync(path) { | ||
if (!path) | ||
path = this.path; | ||
const raw = readFileSync(path); | ||
this.parse(raw, path); | ||
} | ||
parse(raw, path) { | ||
if (this.immutable) | ||
throw new ReferenceError("Config immutable"); | ||
const strategy = (strategy => new strategy())(Strategy.getStrategy(path)); | ||
this.config = this.parseObject(strategy.parse(raw)); | ||
} | ||
static async parseFromFile(path, immutable = false) { | ||
@@ -94,4 +124,7 @@ const raw = await readFile(path); | ||
const strategy = (strategy => new strategy())(Strategy.getStrategy(path)); | ||
return new Config(strategy.parse(raw), immutable, path); | ||
const cfg = new Config(strategy.parse(raw)); | ||
cfg.immutable = immutable; | ||
cfg.path = path; | ||
return cfg; | ||
} | ||
} |
{ | ||
"name": "simcfg", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A simple module for managing configuration files", | ||
"main": "dist/index.js", | ||
"repository": "https://github.com/80LK/simcfg", | ||
"type": "module", | ||
@@ -7,0 +8,0 @@ "scripts": { |
@@ -1,3 +0,92 @@ | ||
# simple-config | ||
# Simple Config(simcfg) | ||
A simple module for managing configuration files | ||
A simple module for managing configuration files | ||
[**en**|[ru](README.RU.md)] | ||
## Install | ||
``` | ||
npm i --save simcfg | ||
``` | ||
You can install the [yaml](https://www.npmjs.com/package/yaml) module, if you want to use [YAML](https://en.wikipedia.org/wiki/YAML) format files. | ||
``` | ||
npm i --save yaml | ||
``` | ||
## Usage | ||
### New config | ||
```ts | ||
const config = new Config();//Empty config | ||
const config = new Config({ | ||
a:1 | ||
});// Config with a = 1 | ||
``` | ||
### Reading the file. | ||
```ts | ||
const config:Config = await Config.parseFromFile("test.json"); // Promise | ||
const config:Config = Config.parseFromFileSync("test.json"); | ||
config.parseFromFile("test.json"); // Promise | ||
config.parseFromFileSync("test.json"); | ||
``` | ||
**!!!WARNING!!!** | ||
The next entry in the config will throw an error when reading the file, due to an attempt to overwrite the value of *a*. | ||
```json | ||
{ | ||
"a":1, | ||
"a.b":2 | ||
} | ||
``` | ||
```yml | ||
a: 1 | ||
a.b: 2 | ||
``` | ||
### Getting values | ||
If no value is found and no standard value is specified. the method will throw an error. | ||
```ts | ||
/* | ||
@config | ||
{ | ||
"a":1, | ||
"c":{ | ||
"e":false | ||
} | ||
} | ||
*/ | ||
config.get<number>("a"); // return 1 | ||
config.get<number>("b", 10); // return 10 | ||
config.get<string>("c.d", "defaultValue"); //return "defaultValue" | ||
config.get<boolean>("c.e", false);//return false | ||
config.get<number>("f");// throw Error | ||
``` | ||
### Changing values | ||
If the config is immutable, the method will throw an error. | ||
```ts | ||
const config = Config.parseFromFileSync("..."); | ||
config.set("a", 10); //Success | ||
const config = Config.parseFromFileSync("...", true); | ||
config.set("a", 10); //throw Error | ||
``` | ||
### Writing to a file | ||
If the file is not specified, the config will be written to the file from which it was read. | ||
```ts | ||
const config = Config.parseFromFileSync("test.json"); | ||
config.set("a", 1); | ||
config.writeFile("new-test.json");// The config is written to a new file "new-test.json" | ||
config.WriteFile();// Config is written to the old file "test.json" | ||
``` | ||
The file can also be written in another format | ||
```js | ||
const config = Config.parseFromFileSync("test.json"); | ||
config.writeFile("new-test.yaml");// The config is written in YAML format | ||
`` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
16212
15
270
93