node-confmanager 1.4.7 → 1.6.1
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 +6 -89
- package/lib/index.d.ts +11 -11
- package/lib/main.js +38 -20
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -24,106 +24,23 @@ $ npm install node-confmanager
|
|
|
24
24
|
|
|
25
25
|
## Doc
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
### Inheritance
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
[check the official 'node-containerpattern' object documentation](https://github.com/Psychopoulet/node-containerpattern)
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
### Content
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
* ``` spaces: string ``` formate file
|
|
35
|
-
* ``` shortcuts: string ``` for container
|
|
36
|
-
|
|
37
|
-
-- Constructor --
|
|
38
|
-
|
|
39
|
-
* ``` constructor(confPath?: string, spaces?: boolean, recursionSeparator?: string) ```
|
|
40
|
-
|
|
41
|
-
-- Methods --
|
|
42
|
-
|
|
43
|
-
* ``` deleteFile() : return Promise instance ``` delete the conf file
|
|
44
|
-
* ``` fileExists() : return Promise instance => then((exists) => {}) ``` check if the conf file exists
|
|
45
|
-
* ``` clearShortcuts() : return this ``` forget all the shortcuts
|
|
46
|
-
* ``` clear() : return this ``` node-containerpattern.clear & clearShortcuts
|
|
47
|
-
* ``` load() : return Promise instance ``` load data from conf file then commandline (commandline takeover)
|
|
48
|
-
* ``` save() : return Promise instance ``` save data into conf file
|
|
49
|
-
* ``` shortcut(string key, string shortkey) : return this ``` bind a shortcut for commandline
|
|
33
|
+
[check the TypeScript definition file](https://github.com/Psychopoulet/node-confmanager/blob/master/lib/index.d.ts)
|
|
50
34
|
|
|
51
35
|
## Examples
|
|
52
36
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
const ConfManager = require("node-confmanager");
|
|
57
|
-
|
|
58
|
-
const conf = new ConfManager(require("path").join(__dirname, "conf.json"));
|
|
59
|
-
|
|
60
|
-
conf
|
|
61
|
-
.skeleton("debug", "boolean") // add skeleton (based on [node-containerpattern](https://www.npmjs.com/package/node-containerpattern)) to check datatype
|
|
62
|
-
.shortcut("debug", "d") // add shortcut to simply use comandline params, can add "-d true" to commandline to activate debug
|
|
63
|
-
.shortcut("usr.login", "ul")
|
|
64
|
-
.shortcut("usr.password", "up");
|
|
65
|
-
|
|
66
|
-
conf.fileExists().then((exists) => {
|
|
67
|
-
|
|
68
|
-
return exists ? Promise.resolve() : Conf.set("usr", { login : "login", pwd : "pwd" })
|
|
69
|
-
.set("debug", false)
|
|
70
|
-
.set("prod", "n") // = false
|
|
71
|
-
.save();
|
|
72
|
-
|
|
73
|
-
}).then(() => {
|
|
74
|
-
|
|
75
|
-
// can add "--usr.login login2" or "-ul login2" to commandline to force login change
|
|
76
|
-
return conf.load();
|
|
77
|
-
|
|
78
|
-
}).then(() => {
|
|
79
|
-
|
|
80
|
-
console.log(conf.get("debug"));
|
|
81
|
-
console.log(conf.get("usr.login"));
|
|
82
|
-
|
|
83
|
-
}).catch((err) => {
|
|
84
|
-
console.log(err);
|
|
85
|
-
});
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Typescript
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
import ConfManager = require("node-confmanager");
|
|
92
|
-
import { join } from "path";
|
|
93
|
-
|
|
94
|
-
const Conf = new ConfManager(join(__dirname, "conf.json"));
|
|
95
|
-
|
|
96
|
-
Conf
|
|
97
|
-
.skeleton("debug", "boolean").shortcut("debug", "d")
|
|
98
|
-
.shortcut("usr.login", "ul")
|
|
99
|
-
.shortcut("usr.password", "up");
|
|
100
|
-
|
|
101
|
-
Conf.fileExists().then((exists: boolean) => {
|
|
102
|
-
|
|
103
|
-
return exists ? Promise.resolve() : Conf.set("usr", { login : "login", pwd : "pwd" })
|
|
104
|
-
.set("debug", false)
|
|
105
|
-
.set("prod", "n") // = false
|
|
106
|
-
.save();
|
|
107
|
-
|
|
108
|
-
}).then(() => {
|
|
109
|
-
|
|
110
|
-
return Conf.load();
|
|
111
|
-
|
|
112
|
-
}).then(() => {
|
|
113
|
-
|
|
114
|
-
console.log(Conf.get("debug"));
|
|
115
|
-
console.log(Conf.get("usr.login"));
|
|
116
|
-
|
|
117
|
-
}).catch((err: Error) => {
|
|
118
|
-
console.log(err);
|
|
119
|
-
});
|
|
120
|
-
```
|
|
37
|
+
[check the TypeScript compilation tests](https://github.com/Psychopoulet/node-confmanager/blob/master/test/typescript/compilation.ts)
|
|
121
38
|
|
|
122
39
|
### Run
|
|
123
40
|
|
|
124
41
|
```bash
|
|
125
42
|
node mysoft.js -d
|
|
126
|
-
node mysoft.js --debug
|
|
43
|
+
node mysoft.js --debug # if "skeleton" is defined as a boolean
|
|
127
44
|
node mysoft.js --debug "true"
|
|
128
45
|
node mysoft.js --debug "yes"
|
|
129
46
|
node mysoft.js --debug "y"
|
package/lib/index.d.ts
CHANGED
|
@@ -6,22 +6,22 @@ declare module "node-confmanager" {
|
|
|
6
6
|
|
|
7
7
|
class ConfManager extends Container {
|
|
8
8
|
|
|
9
|
-
public filePath: string;
|
|
10
|
-
public spaces: boolean;
|
|
11
|
-
public shortcuts: Array<string>;
|
|
9
|
+
public filePath: string; // conf file
|
|
10
|
+
public spaces: boolean; // formate file
|
|
11
|
+
public shortcuts: Array<string>; // for container
|
|
12
12
|
|
|
13
13
|
constructor(filePath?: string, spaces?: boolean, recursionSeparator?: string);
|
|
14
14
|
|
|
15
15
|
protected _loadFromConsole(): Promise<void>;
|
|
16
16
|
|
|
17
|
-
public
|
|
18
|
-
public clearShortcuts(): this;
|
|
19
|
-
public
|
|
20
|
-
public
|
|
21
|
-
public get(key: string): any;
|
|
22
|
-
public
|
|
23
|
-
public
|
|
24
|
-
public
|
|
17
|
+
// public clear(): void; // Container.clear & clearShortcuts
|
|
18
|
+
public clearShortcuts(): this; // forget all the shortcuts
|
|
19
|
+
public deleteFile(): Promise<void>; // delete the conf file
|
|
20
|
+
public fileExists(): Promise<boolean>; // check if the conf file exists
|
|
21
|
+
// public get(key: string): any; // Container.get with cloned data
|
|
22
|
+
public load(): Promise<void>; // load data from conf file then commandline (commandline takeover)
|
|
23
|
+
public save(): Promise<void>; // save data into conf file
|
|
24
|
+
public shortcut(key: string, shortkey: string): this; // bind a shortcut for commandline
|
|
25
25
|
|
|
26
26
|
}
|
|
27
27
|
|
package/lib/main.js
CHANGED
|
@@ -60,19 +60,27 @@ module.exports = class ConfManager extends Container {
|
|
|
60
60
|
if (arg.startsWith("-")) {
|
|
61
61
|
|
|
62
62
|
const isShortcut = !arg.startsWith("--");
|
|
63
|
-
|
|
64
63
|
const argument = arg.slice(isShortcut ? 1 : 2, arg.length);
|
|
65
64
|
|
|
66
|
-
if (!isShortcut || this.shortcuts[argument]) {
|
|
65
|
+
if (argument && (!isShortcut || this.shortcuts[argument])) {
|
|
67
66
|
|
|
68
67
|
const key = isShortcut ? this.shortcuts[argument] : argument;
|
|
69
68
|
|
|
70
|
-
if (
|
|
71
|
-
this.set(key, args[i + 1]);
|
|
72
|
-
}
|
|
73
|
-
else if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
|
|
69
|
+
if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
|
|
74
70
|
this.set(key, true);
|
|
75
71
|
}
|
|
72
|
+
else if (i + 1 >= args.length) {
|
|
73
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (no more arguments)");
|
|
74
|
+
}
|
|
75
|
+
else if (args[i + 1].startsWith("--")) {
|
|
76
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid key)");
|
|
77
|
+
}
|
|
78
|
+
else if (args[i + 1].startsWith("-") && this.shortcuts[args[i + 1].slice(1)]) {
|
|
79
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid shortcut)");
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.set(key, args[i + 1]);
|
|
83
|
+
}
|
|
76
84
|
|
|
77
85
|
}
|
|
78
86
|
|
|
@@ -86,26 +94,21 @@ module.exports = class ConfManager extends Container {
|
|
|
86
94
|
|
|
87
95
|
}
|
|
88
96
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const { key, shortkey } = checkShortcut(_key, _shortkey);
|
|
92
|
-
|
|
93
|
-
this.shortcuts[shortkey] = key;
|
|
94
|
-
|
|
95
|
-
return this;
|
|
97
|
+
// public
|
|
96
98
|
|
|
99
|
+
// Container.clear & clearShortcuts
|
|
100
|
+
clear () {
|
|
101
|
+
super.clear();
|
|
102
|
+
this.clearShortcuts();
|
|
97
103
|
}
|
|
98
104
|
|
|
105
|
+
// forget all the shortcuts
|
|
99
106
|
clearShortcuts () {
|
|
100
107
|
this.shortcuts = [];
|
|
101
108
|
return this;
|
|
102
109
|
}
|
|
103
110
|
|
|
104
|
-
|
|
105
|
-
super.clear();
|
|
106
|
-
return this.clearShortcuts();
|
|
107
|
-
}
|
|
108
|
-
|
|
111
|
+
// delete the conf file
|
|
109
112
|
deleteFile () {
|
|
110
113
|
|
|
111
114
|
return !this.filePath ? Promise.resolve() : pathExists(this.filePath).then((exists) => {
|
|
@@ -114,10 +117,17 @@ module.exports = class ConfManager extends Container {
|
|
|
114
117
|
|
|
115
118
|
}
|
|
116
119
|
|
|
120
|
+
// check if the conf file exists
|
|
117
121
|
fileExists () {
|
|
118
122
|
return !this.filePath ? Promise.resolve(false) : pathExists(this.filePath);
|
|
119
123
|
}
|
|
120
124
|
|
|
125
|
+
// Container.get with cloned data
|
|
126
|
+
get (key) {
|
|
127
|
+
return clone(super.get(key));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// load data from conf file then commandline (commandline takeover)
|
|
121
131
|
load () {
|
|
122
132
|
|
|
123
133
|
this.clearData();
|
|
@@ -138,6 +148,7 @@ module.exports = class ConfManager extends Container {
|
|
|
138
148
|
|
|
139
149
|
}
|
|
140
150
|
|
|
151
|
+
// save data into conf file
|
|
141
152
|
save () {
|
|
142
153
|
|
|
143
154
|
return !this.filePath ? Promise.resolve() : mkdirp(dirname(this.filePath)).then(() => {
|
|
@@ -158,8 +169,15 @@ module.exports = class ConfManager extends Container {
|
|
|
158
169
|
|
|
159
170
|
}
|
|
160
171
|
|
|
161
|
-
|
|
162
|
-
|
|
172
|
+
// bind a shortcut for commandline
|
|
173
|
+
shortcut (_key, _shortkey) {
|
|
174
|
+
|
|
175
|
+
const { key, shortkey } = checkShortcut(_key, _shortkey);
|
|
176
|
+
|
|
177
|
+
this.shortcuts[shortkey] = key;
|
|
178
|
+
|
|
179
|
+
return this;
|
|
180
|
+
|
|
163
181
|
}
|
|
164
182
|
|
|
165
183
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-confmanager",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "A configuration manager",
|
|
5
5
|
"main": "lib/main.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"url": "https://github.com/Psychopoulet/node-confmanager/issues"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"node-containerpattern": "1.
|
|
48
|
+
"node-containerpattern": "1.6.0",
|
|
49
49
|
"fs-extra": "10.1.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"husky": "8.0.1",
|
|
57
57
|
"mocha": "10.0.0",
|
|
58
58
|
"nyc": "15.1.0",
|
|
59
|
-
"typescript": "4.
|
|
59
|
+
"typescript": "4.7.2"
|
|
60
60
|
},
|
|
61
61
|
"homepage": "https://github.com/Psychopoulet/node-confmanager#readme",
|
|
62
62
|
"engines": {
|