node-confmanager 1.5.0 → 1.7.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/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/cjs/NodeConfManager.d.ts +18 -0
- package/lib/cjs/NodeConfManager.js +131 -0
- package/lib/cjs/main.cjs +8 -0
- package/lib/cjs/main.d.cts +2 -0
- package/lib/cjs/utils/checkShortcut.d.ts +4 -0
- package/lib/cjs/utils/checkShortcut.js +31 -0
- package/lib/cjs/utils/clone.d.ts +1 -0
- package/lib/cjs/utils/clone.js +21 -0
- package/package.json +29 -12
- package/lib/checkShortcut.js +0 -36
- package/lib/clone.js +0 -24
- package/lib/index.d.ts +0 -30
- package/lib/main.js +0 -175
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import NodeContainerPattern = require("node-containerpattern");
|
|
2
|
+
export default class ConfManager extends NodeContainerPattern {
|
|
3
|
+
filePath: string;
|
|
4
|
+
spaces: boolean;
|
|
5
|
+
shortcuts: {
|
|
6
|
+
[key: string]: string;
|
|
7
|
+
};
|
|
8
|
+
constructor(filePath: string, spaces?: boolean, recursionSeparator?: string);
|
|
9
|
+
private _loadFromConsole;
|
|
10
|
+
clear(): void;
|
|
11
|
+
clearShortcuts(): this;
|
|
12
|
+
deleteFile(): Promise<void>;
|
|
13
|
+
fileExists(): Promise<boolean>;
|
|
14
|
+
get(key: string): any;
|
|
15
|
+
load(): Promise<void>;
|
|
16
|
+
save(): Promise<void>;
|
|
17
|
+
shortcut(_key: string, _shortkey: string): this;
|
|
18
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
// deps
|
|
7
|
+
// natives
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
// externals
|
|
10
|
+
const fs_extra_1 = require("fs-extra");
|
|
11
|
+
const NodeContainerPattern = require("node-containerpattern");
|
|
12
|
+
// locals
|
|
13
|
+
const checkShortcut_1 = __importDefault(require("./utils/checkShortcut"));
|
|
14
|
+
const clone_1 = __importDefault(require("./utils/clone"));
|
|
15
|
+
// module
|
|
16
|
+
class ConfManager extends NodeContainerPattern {
|
|
17
|
+
// constructor
|
|
18
|
+
constructor(filePath, spaces = false, recursionSeparator = ".") {
|
|
19
|
+
if ("undefined" !== typeof filePath && "string" !== typeof filePath) {
|
|
20
|
+
throw new TypeError("\"filePath\" parameter is not a string");
|
|
21
|
+
}
|
|
22
|
+
else if ("undefined" !== typeof filePath && "" === filePath.trim()) {
|
|
23
|
+
throw new Error("\"filePath\" parameter is empty");
|
|
24
|
+
}
|
|
25
|
+
else if ("undefined" !== typeof spaces && "boolean" !== typeof spaces) {
|
|
26
|
+
throw new TypeError("The \"spaces\" parameter is not a boolean");
|
|
27
|
+
}
|
|
28
|
+
else if ("undefined" !== typeof recursionSeparator && "string" !== typeof recursionSeparator) {
|
|
29
|
+
throw new TypeError("The \"recursionSeparator\" parameter is not a string");
|
|
30
|
+
}
|
|
31
|
+
else if ("undefined" !== typeof recursionSeparator && "" === recursionSeparator.trim()) {
|
|
32
|
+
throw new Error("\"recursionSeparator\" parameter is empty");
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
super(recursionSeparator);
|
|
36
|
+
this.filePath = "undefined" !== typeof filePath ? filePath.trim() : "";
|
|
37
|
+
this.spaces = spaces;
|
|
38
|
+
this.shortcuts = {};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// private
|
|
42
|
+
_loadFromConsole() {
|
|
43
|
+
return Promise.resolve().then(() => {
|
|
44
|
+
process.argv.slice(2, process.argv.length).forEach((arg, i, args) => {
|
|
45
|
+
if (arg.startsWith("-")) {
|
|
46
|
+
const isShortcut = !arg.startsWith("--");
|
|
47
|
+
const argument = arg.slice(isShortcut ? 1 : 2, arg.length);
|
|
48
|
+
if (argument && (!isShortcut || this.shortcuts[argument])) {
|
|
49
|
+
const key = isShortcut ? this.shortcuts[argument] : argument;
|
|
50
|
+
if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
|
|
51
|
+
this.set(key, true);
|
|
52
|
+
}
|
|
53
|
+
else if (i + 1 >= args.length) {
|
|
54
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (no more arguments)");
|
|
55
|
+
}
|
|
56
|
+
else if (args[i + 1].startsWith("--")) {
|
|
57
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid key)");
|
|
58
|
+
}
|
|
59
|
+
else if (args[i + 1].startsWith("-") && this.shortcuts[args[i + 1].slice(1)]) {
|
|
60
|
+
throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid shortcut)");
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.set(key, args[i + 1]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// public
|
|
72
|
+
// Container.clear & clearShortcuts
|
|
73
|
+
clear() {
|
|
74
|
+
super.clear();
|
|
75
|
+
this.clearShortcuts();
|
|
76
|
+
}
|
|
77
|
+
// forget all the shortcuts
|
|
78
|
+
clearShortcuts() {
|
|
79
|
+
this.shortcuts = {};
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
// delete the conf file
|
|
83
|
+
deleteFile() {
|
|
84
|
+
return !this.filePath ? Promise.resolve() : (0, fs_extra_1.pathExists)(this.filePath).then((exists) => {
|
|
85
|
+
return exists ? (0, fs_extra_1.unlink)(this.filePath) : Promise.resolve();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// check if the conf file exists
|
|
89
|
+
fileExists() {
|
|
90
|
+
return !this.filePath ? Promise.resolve(false) : (0, fs_extra_1.pathExists)(this.filePath);
|
|
91
|
+
}
|
|
92
|
+
// Container.get with cloned data
|
|
93
|
+
get(key) {
|
|
94
|
+
return (0, clone_1.default)(super.get(key));
|
|
95
|
+
}
|
|
96
|
+
// load data from conf file then commandline (commandline takeover)
|
|
97
|
+
load() {
|
|
98
|
+
this.clearData();
|
|
99
|
+
return this.fileExists().then((exists) => {
|
|
100
|
+
return !exists ? this._loadFromConsole() : (0, fs_extra_1.readJson)(this.filePath, "utf8").then((data) => {
|
|
101
|
+
for (const key in data) {
|
|
102
|
+
this.set(key, data[key]);
|
|
103
|
+
}
|
|
104
|
+
return this._loadFromConsole();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
// save data into conf file
|
|
109
|
+
save() {
|
|
110
|
+
return !this.filePath ? Promise.resolve() : (0, fs_extra_1.mkdirp)((0, path_1.dirname)(this.filePath)).then(() => {
|
|
111
|
+
const objects = {};
|
|
112
|
+
this.forEach((value, key) => {
|
|
113
|
+
objects[key] = value;
|
|
114
|
+
});
|
|
115
|
+
return this.spaces ? (0, fs_extra_1.writeJson)(this.filePath, objects, {
|
|
116
|
+
"encoding": "utf8",
|
|
117
|
+
"spaces": 2
|
|
118
|
+
}) : (0, fs_extra_1.writeJson)(this.filePath, objects, {
|
|
119
|
+
"encoding": "utf8"
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
// bind a shortcut for commandline
|
|
124
|
+
shortcut(_key, _shortkey) {
|
|
125
|
+
const { key, shortkey } = (0, checkShortcut_1.default)(_key, _shortkey);
|
|
126
|
+
this.shortcuts[shortkey] = key;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.default = ConfManager;
|
|
131
|
+
;
|
package/lib/cjs/main.cjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
// deps
|
|
6
|
+
// locals
|
|
7
|
+
const NodeConfManager_1 = __importDefault(require("./NodeConfManager"));
|
|
8
|
+
module.exports = NodeConfManager_1.default;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// module
|
|
4
|
+
function checkShortcut(key, shortkey) {
|
|
5
|
+
if ("undefined" === typeof key) {
|
|
6
|
+
throw new ReferenceError("Missing \"key\" parameter");
|
|
7
|
+
}
|
|
8
|
+
else if ("string" !== typeof key) {
|
|
9
|
+
throw new TypeError("\"key\" parameter is not a string");
|
|
10
|
+
}
|
|
11
|
+
else if ("" === key.trim()) {
|
|
12
|
+
throw new RangeError("\"key\" parameter is empty");
|
|
13
|
+
}
|
|
14
|
+
else if ("undefined" === typeof shortkey) {
|
|
15
|
+
throw new ReferenceError("Missing \"shortkey\" parameter");
|
|
16
|
+
}
|
|
17
|
+
else if ("string" !== typeof shortkey) {
|
|
18
|
+
throw new TypeError("\"shortkey\" parameter is not a string");
|
|
19
|
+
}
|
|
20
|
+
else if ("" === shortkey.trim()) {
|
|
21
|
+
throw new RangeError("\"shortkey\" parameter is empty");
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return {
|
|
25
|
+
"key": key.trim().toLowerCase(),
|
|
26
|
+
"shortkey": shortkey.trim().toLowerCase()
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.default = checkShortcut;
|
|
31
|
+
;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function clone(from: any): any;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// module
|
|
4
|
+
function clone(from) {
|
|
5
|
+
if (from && "object" === typeof from) {
|
|
6
|
+
if (Object === from.constructor) {
|
|
7
|
+
return Object.assign({}, from);
|
|
8
|
+
}
|
|
9
|
+
else if (Array === from.constructor) {
|
|
10
|
+
return [...from];
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return new from.constructor(from);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return from;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.default = clone;
|
|
21
|
+
;
|
package/package.json
CHANGED
|
@@ -1,22 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-confmanager",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "A configuration manager",
|
|
5
|
-
|
|
6
|
-
"
|
|
5
|
+
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"typings": "./lib/cjs/main.d.cts",
|
|
8
|
+
"main": "./lib/cjs/main.cjs",
|
|
9
|
+
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./lib/cjs/main.d.cts",
|
|
14
|
+
"default": "./lib/cjs/main.cjs"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
|
|
7
19
|
"scripts": {
|
|
8
|
-
|
|
20
|
+
|
|
9
21
|
"check-updates": "npx check-version-modules",
|
|
10
|
-
|
|
11
|
-
"
|
|
22
|
+
|
|
23
|
+
"compile": "npx tsc --project \"./tsconfig.json\"",
|
|
24
|
+
|
|
25
|
+
"unit-tests": "npm run compile && npx nyc --reporter=html --reporter=text mocha",
|
|
26
|
+
"tests": "npm run-script check-updates && npm run-script unit-tests",
|
|
12
27
|
"ci": "npm run-script tests && npx nyc report --reporter=text-lcov | coveralls"
|
|
28
|
+
|
|
13
29
|
},
|
|
30
|
+
|
|
14
31
|
"files": [
|
|
15
32
|
"/lib"
|
|
16
33
|
],
|
|
34
|
+
|
|
17
35
|
"husky": {
|
|
18
36
|
"hooks": {
|
|
19
|
-
"pre-commit": "npm run-script lint",
|
|
20
37
|
"pre-push": "npm run-script tests"
|
|
21
38
|
}
|
|
22
39
|
},
|
|
@@ -45,21 +62,21 @@
|
|
|
45
62
|
"url": "https://github.com/Psychopoulet/node-confmanager/issues"
|
|
46
63
|
},
|
|
47
64
|
"dependencies": {
|
|
48
|
-
"node-containerpattern": "1.
|
|
65
|
+
"node-containerpattern": "1.7.2",
|
|
49
66
|
"fs-extra": "10.1.0"
|
|
50
67
|
},
|
|
51
68
|
"devDependencies": {
|
|
52
|
-
"@types/node": "
|
|
69
|
+
"@types/node": "18.0.0",
|
|
70
|
+
"@types/fs-extra": "9.0.13",
|
|
53
71
|
"check-version-modules": "1.3.5",
|
|
54
72
|
"coveralls": "3.1.1",
|
|
55
|
-
"eslint": "8.16.0",
|
|
56
73
|
"husky": "8.0.1",
|
|
57
74
|
"mocha": "10.0.0",
|
|
58
75
|
"nyc": "15.1.0",
|
|
59
|
-
"typescript": "4.
|
|
76
|
+
"typescript": "4.7.4"
|
|
60
77
|
},
|
|
61
78
|
"homepage": "https://github.com/Psychopoulet/node-confmanager#readme",
|
|
62
79
|
"engines": {
|
|
63
|
-
"node": ">=
|
|
80
|
+
"node": ">=12.0.0"
|
|
64
81
|
}
|
|
65
82
|
}
|
package/lib/checkShortcut.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// module
|
|
4
|
-
|
|
5
|
-
module.exports = function checkShortcut (key, shortkey) {
|
|
6
|
-
|
|
7
|
-
if ("undefined" === typeof key) {
|
|
8
|
-
throw new ReferenceError("Missing \"key\" parameter");
|
|
9
|
-
}
|
|
10
|
-
else if ("string" !== typeof key) {
|
|
11
|
-
throw new TypeError("\"key\" parameter is not a string");
|
|
12
|
-
}
|
|
13
|
-
else if ("" === key.trim()) {
|
|
14
|
-
throw new RangeError("\"key\" parameter is empty");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
else if ("undefined" === typeof shortkey) {
|
|
18
|
-
throw new ReferenceError("Missing \"shortkey\" parameter");
|
|
19
|
-
}
|
|
20
|
-
else if ("string" !== typeof shortkey) {
|
|
21
|
-
throw new TypeError("\"shortkey\" parameter is not a string");
|
|
22
|
-
}
|
|
23
|
-
else if ("" === shortkey.trim()) {
|
|
24
|
-
throw new RangeError("\"shortkey\" parameter is empty");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
else {
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
"key": key.trim().toLowerCase(),
|
|
31
|
-
"shortkey": shortkey.trim().toLowerCase()
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
};
|
package/lib/clone.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// module
|
|
4
|
-
|
|
5
|
-
module.exports = function clone (from) {
|
|
6
|
-
|
|
7
|
-
if (from && "object" === typeof from) {
|
|
8
|
-
|
|
9
|
-
if (Object === from.constructor) {
|
|
10
|
-
return { ...from };
|
|
11
|
-
}
|
|
12
|
-
else if (Array === from.constructor) {
|
|
13
|
-
return [ ...from ];
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return new from.constructor(from);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
return from;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
};
|
package/lib/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
declare module "node-confmanager" {
|
|
4
|
-
|
|
5
|
-
import Container = require("node-containerpattern");
|
|
6
|
-
|
|
7
|
-
class ConfManager extends Container {
|
|
8
|
-
|
|
9
|
-
public filePath: string; // conf file
|
|
10
|
-
public spaces: boolean; // formate file
|
|
11
|
-
public shortcuts: Array<string>; // for container
|
|
12
|
-
|
|
13
|
-
constructor(filePath?: string, spaces?: boolean, recursionSeparator?: string);
|
|
14
|
-
|
|
15
|
-
protected _loadFromConsole(): Promise<void>;
|
|
16
|
-
|
|
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
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export = ConfManager;
|
|
29
|
-
|
|
30
|
-
}
|
package/lib/main.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// deps
|
|
4
|
-
|
|
5
|
-
// natives
|
|
6
|
-
const { dirname, join } = require("path");
|
|
7
|
-
|
|
8
|
-
// externals
|
|
9
|
-
const { pathExists, mkdirp, readJson, unlink, writeJson } = require("fs-extra");
|
|
10
|
-
const Container = require("node-containerpattern");
|
|
11
|
-
|
|
12
|
-
// locals
|
|
13
|
-
const checkShortcut = require(join(__dirname, "checkShortcut.js"));
|
|
14
|
-
const clone = require(join(__dirname, "clone.js"));
|
|
15
|
-
|
|
16
|
-
// module
|
|
17
|
-
|
|
18
|
-
module.exports = class ConfManager extends Container {
|
|
19
|
-
|
|
20
|
-
constructor (filePath, spaces = false, recursionSeparator = ".") {
|
|
21
|
-
|
|
22
|
-
if ("undefined" !== typeof filePath && "string" !== typeof filePath) {
|
|
23
|
-
throw new TypeError("\"filePath\" parameter is not a string");
|
|
24
|
-
}
|
|
25
|
-
else if ("undefined" !== typeof filePath && "" === filePath.trim()) {
|
|
26
|
-
throw new Error("\"filePath\" parameter is empty");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
else if ("undefined" !== typeof spaces && "boolean" !== typeof spaces) {
|
|
30
|
-
throw new TypeError("The \"spaces\" parameter is not a boolean");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
else if ("undefined" !== typeof recursionSeparator && "string" !== typeof recursionSeparator) {
|
|
34
|
-
throw new TypeError("The \"recursionSeparator\" parameter is not a string");
|
|
35
|
-
}
|
|
36
|
-
else if ("undefined" !== typeof recursionSeparator && "" === recursionSeparator.trim()) {
|
|
37
|
-
throw new Error("\"recursionSeparator\" parameter is empty");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
else {
|
|
41
|
-
|
|
42
|
-
super(recursionSeparator);
|
|
43
|
-
|
|
44
|
-
this.filePath = "undefined" !== typeof filePath ? filePath.trim() : "";
|
|
45
|
-
this.spaces = spaces;
|
|
46
|
-
this.shortcuts = [];
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// private
|
|
53
|
-
|
|
54
|
-
_loadFromConsole () {
|
|
55
|
-
|
|
56
|
-
return Promise.resolve().then(() => {
|
|
57
|
-
|
|
58
|
-
(0, process).argv.slice(2, (0, process).argv.length).forEach((arg, i, args) => {
|
|
59
|
-
|
|
60
|
-
if (arg.startsWith("-")) {
|
|
61
|
-
|
|
62
|
-
const isShortcut = !arg.startsWith("--");
|
|
63
|
-
|
|
64
|
-
const argument = arg.slice(isShortcut ? 1 : 2, arg.length);
|
|
65
|
-
|
|
66
|
-
if (!isShortcut || this.shortcuts[argument]) {
|
|
67
|
-
|
|
68
|
-
const key = isShortcut ? this.shortcuts[argument] : argument;
|
|
69
|
-
|
|
70
|
-
if (i + 1 < args.length && -1 >= args[i + 1].indexOf("--")) {
|
|
71
|
-
this.set(key, args[i + 1]);
|
|
72
|
-
}
|
|
73
|
-
else if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
|
|
74
|
-
this.set(key, true);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
return Promise.resolve();
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// public
|
|
90
|
-
|
|
91
|
-
// Container.clear & clearShortcuts
|
|
92
|
-
clear () {
|
|
93
|
-
super.clear();
|
|
94
|
-
this.clearShortcuts();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// forget all the shortcuts
|
|
98
|
-
clearShortcuts () {
|
|
99
|
-
this.shortcuts = [];
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// delete the conf file
|
|
104
|
-
deleteFile () {
|
|
105
|
-
|
|
106
|
-
return !this.filePath ? Promise.resolve() : pathExists(this.filePath).then((exists) => {
|
|
107
|
-
return exists ? unlink(this.filePath) : Promise.resolve();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// check if the conf file exists
|
|
113
|
-
fileExists () {
|
|
114
|
-
return !this.filePath ? Promise.resolve(false) : pathExists(this.filePath);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Container.get with cloned data
|
|
118
|
-
get (key) {
|
|
119
|
-
return clone(super.get(key));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// load data from conf file then commandline (commandline takeover)
|
|
123
|
-
load () {
|
|
124
|
-
|
|
125
|
-
this.clearData();
|
|
126
|
-
|
|
127
|
-
return this.fileExists().then((exists) => {
|
|
128
|
-
|
|
129
|
-
return !exists ? this._loadFromConsole() : readJson(this.filePath, "utf8").then((data) => {
|
|
130
|
-
|
|
131
|
-
for (const key in data) {
|
|
132
|
-
this.set(key, data[key]);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return this._loadFromConsole();
|
|
136
|
-
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// save data into conf file
|
|
144
|
-
save () {
|
|
145
|
-
|
|
146
|
-
return !this.filePath ? Promise.resolve() : mkdirp(dirname(this.filePath)).then(() => {
|
|
147
|
-
|
|
148
|
-
const objects = {};
|
|
149
|
-
this.forEach((value, key) => {
|
|
150
|
-
objects[key] = value;
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return this.spaces ? writeJson(this.filePath, objects, {
|
|
154
|
-
"encoding": "utf8",
|
|
155
|
-
"space": 2
|
|
156
|
-
}) : writeJson(this.filePath, objects, {
|
|
157
|
-
"encoding": "utf8"
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// bind a shortcut for commandline
|
|
165
|
-
shortcut (_key, _shortkey) {
|
|
166
|
-
|
|
167
|
-
const { key, shortkey } = checkShortcut(_key, _shortkey);
|
|
168
|
-
|
|
169
|
-
this.shortcuts[shortkey] = key;
|
|
170
|
-
|
|
171
|
-
return this;
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
};
|