node-confmanager 1.7.1 → 1.8.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 CHANGED
@@ -39,13 +39,20 @@ $ npm install node-confmanager
39
39
  ### Run
40
40
 
41
41
  ```bash
42
- node mysoft.js -d
43
- node mysoft.js --debug # if "skeleton" is defined as a boolean
42
+ # if "debug" skeleton is setted & defined as a boolean
43
+ node mysoft.js -d # if "debug" shortcut is setted
44
+ node mysoft.js --debug
44
45
  node mysoft.js --debug "true"
45
46
  node mysoft.js --debug "yes"
46
47
  node mysoft.js --debug "y"
47
48
  ```
48
49
 
50
+ ```bash
51
+ # if "arr" skeleton is defined as a array
52
+ node mysoft.js --arr test1 test2
53
+ node mysoft.js --arr "[ \"test1\", \"test2\" ]"
54
+ ```
55
+
49
56
  ## Tests
50
57
 
51
58
  ```bash
@@ -1,18 +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
- }
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(loadConsole?: boolean): Promise<void>;
16
+ save(): Promise<void>;
17
+ shortcut(_key: string, _shortkey: string): this;
18
+ }
@@ -1,131 +1,164 @@
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
- ;
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 node_path_1 = require("node:path");
9
+ const promises_1 = require("node:fs/promises");
10
+ // externals
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
+ const isFile_1 = __importDefault(require("./utils/isFile"));
16
+ // module
17
+ class ConfManager extends NodeContainerPattern {
18
+ // constructor
19
+ constructor(filePath, spaces = false, recursionSeparator = ".") {
20
+ if ("undefined" !== typeof filePath && "string" !== typeof filePath) {
21
+ throw new TypeError("\"filePath\" parameter is not a string");
22
+ }
23
+ else if ("undefined" !== typeof filePath && "" === filePath.trim()) {
24
+ throw new Error("\"filePath\" parameter is empty");
25
+ }
26
+ else if ("undefined" !== typeof spaces && "boolean" !== typeof spaces) {
27
+ throw new TypeError("The \"spaces\" parameter is not a boolean");
28
+ }
29
+ else if ("undefined" !== typeof recursionSeparator && "string" !== typeof recursionSeparator) {
30
+ throw new TypeError("The \"recursionSeparator\" parameter is not a string");
31
+ }
32
+ else if ("undefined" !== typeof recursionSeparator && "" === recursionSeparator.trim()) {
33
+ throw new Error("\"recursionSeparator\" parameter is empty");
34
+ }
35
+ else {
36
+ super(recursionSeparator);
37
+ this.filePath = "undefined" !== typeof filePath ? filePath.trim() : "";
38
+ this.spaces = spaces;
39
+ this.shortcuts = {};
40
+ }
41
+ }
42
+ // private
43
+ _loadFromConsole() {
44
+ process.argv.slice(2, process.argv.length).forEach((arg, i, args) => {
45
+ if ("--" === arg) {
46
+ return;
47
+ }
48
+ if (arg.startsWith("-")) {
49
+ const isShortcut = !arg.startsWith("--");
50
+ const argument = arg.slice(isShortcut ? 1 : 2, arg.length);
51
+ if (argument && (!isShortcut || this.shortcuts[argument])) {
52
+ const key = isShortcut ? this.shortcuts[argument] : argument;
53
+ // boolean
54
+ if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
55
+ this.set(key, true);
56
+ }
57
+ // check args
58
+ else if (i + 1 >= args.length) {
59
+ throw new ReferenceError("Missing value for \"" + argument + "\" key (no more arguments)");
60
+ }
61
+ else if (args[i + 1].startsWith("--")) {
62
+ throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid key)");
63
+ }
64
+ else if (args[i + 1].startsWith("-") && this.shortcuts[args[i + 1].slice(1)]) {
65
+ throw new ReferenceError("Missing value for \"" + argument + "\" key (next argument is a valid shortcut)");
66
+ }
67
+ // array
68
+ else if (this.skeletons[key] && "array" === this.skeletons[key]) {
69
+ const nextArgs = args.slice(i + 1, args.length);
70
+ if (!nextArgs.length) {
71
+ this.set(key, []);
72
+ }
73
+ else {
74
+ const endArrayArgs = nextArgs.findIndex((a) => {
75
+ return a.startsWith("--") ||
76
+ (a.startsWith("-") && !!this.shortcuts[a.slice(1)]);
77
+ });
78
+ const values = 0 < endArrayArgs ? nextArgs.slice(0, endArrayArgs) : nextArgs;
79
+ if (1 === values.length && values[0].startsWith("[") && values[0].endsWith("]")) {
80
+ this.set(key, JSON.parse(values[0]));
81
+ }
82
+ else {
83
+ this.set(key, values);
84
+ }
85
+ }
86
+ }
87
+ else {
88
+ this.set(key, args[i + 1]);
89
+ }
90
+ }
91
+ }
92
+ });
93
+ }
94
+ // public
95
+ // Container.clear & clearShortcuts
96
+ clear() {
97
+ super.clear();
98
+ this.clearShortcuts();
99
+ }
100
+ // forget all the shortcuts
101
+ clearShortcuts() {
102
+ this.shortcuts = {};
103
+ return this;
104
+ }
105
+ // delete the conf file
106
+ deleteFile() {
107
+ return !this.filePath ? Promise.resolve() : (0, isFile_1.default)(this.filePath).then((exists) => {
108
+ return exists ? (0, promises_1.unlink)(this.filePath) : Promise.resolve();
109
+ });
110
+ }
111
+ // check if the conf file exists
112
+ fileExists() {
113
+ return !this.filePath ? Promise.resolve(false) : (0, isFile_1.default)(this.filePath);
114
+ }
115
+ // Container.get with cloned data
116
+ get(key) {
117
+ return (0, clone_1.default)(super.get(key));
118
+ }
119
+ // load data from conf file then commandline (commandline takeover)
120
+ load(loadConsole = true) {
121
+ this.clearData();
122
+ return this.fileExists().then((exists) => {
123
+ if (!exists) {
124
+ if (loadConsole) {
125
+ this._loadFromConsole();
126
+ }
127
+ }
128
+ else {
129
+ return (0, promises_1.readFile)(this.filePath, "utf8").then((content) => {
130
+ return JSON.parse(content);
131
+ }).then((data) => {
132
+ for (const key in data) {
133
+ this.set(key, data[key]);
134
+ }
135
+ if (loadConsole) {
136
+ this._loadFromConsole();
137
+ }
138
+ });
139
+ }
140
+ });
141
+ }
142
+ // save data into conf file
143
+ save() {
144
+ return !this.filePath ? Promise.resolve() : (0, promises_1.mkdir)((0, node_path_1.dirname)(this.filePath), {
145
+ "recursive": true
146
+ }).then(() => {
147
+ const objects = {};
148
+ this.forEach((value, key) => {
149
+ objects[key] = value;
150
+ });
151
+ return this.spaces ?
152
+ (0, promises_1.writeFile)(this.filePath, JSON.stringify(objects, undefined, 2), "utf8") :
153
+ (0, promises_1.writeFile)(this.filePath, JSON.stringify(objects), "utf8");
154
+ });
155
+ }
156
+ // bind a shortcut for commandline
157
+ shortcut(_key, _shortkey) {
158
+ const { key, shortkey } = (0, checkShortcut_1.default)(_key, _shortkey);
159
+ this.shortcuts[shortkey] = key;
160
+ return this;
161
+ }
162
+ }
163
+ exports.default = ConfManager;
164
+ ;
package/lib/cjs/main.cjs CHANGED
@@ -1,8 +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;
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;
@@ -1,2 +1,2 @@
1
- import NodeConfManager from "./NodeConfManager";
2
- export = NodeConfManager;
1
+ import NodeConfManager from "./NodeConfManager";
2
+ export = NodeConfManager;
@@ -1,4 +1,4 @@
1
- export default function checkShortcut(key: string, shortkey: string): {
2
- "key": string;
3
- "shortkey": string;
4
- };
1
+ export default function checkShortcut(key: string, shortkey: string): {
2
+ "key": string;
3
+ "shortkey": string;
4
+ };
@@ -1,31 +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
- ;
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
+ ;
@@ -1 +1 @@
1
- export default function clone(from: any): any;
1
+ export default function clone(from: any): any;
@@ -1,21 +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
- ;
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
+ ;
@@ -0,0 +1 @@
1
+ export default function isFile(file: string): Promise<boolean>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // deps
4
+ // natives
5
+ const node_fs_1 = require("node:fs");
6
+ // module
7
+ function isFile(file) {
8
+ return new Promise((resolve, reject) => {
9
+ if ("undefined" === typeof file) {
10
+ reject(new ReferenceError("missing \"file\" argument"));
11
+ }
12
+ else if ("string" !== typeof file) {
13
+ reject(new TypeError("\"file\" argument is not a string"));
14
+ }
15
+ else if ("" === file.trim()) {
16
+ reject(new Error("\"file\" argument is empty"));
17
+ }
18
+ else {
19
+ (0, node_fs_1.lstat)(file, (err, stats) => {
20
+ return resolve(Boolean(!err && stats.isFile()));
21
+ });
22
+ }
23
+ });
24
+ }
25
+ exports.default = isFile;
26
+ ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-confmanager",
3
- "version": "1.7.1",
3
+ "version": "1.8.1",
4
4
  "description": "A configuration manager",
5
5
 
6
6
  "type": "commonjs",
@@ -18,12 +18,14 @@
18
18
 
19
19
  "scripts": {
20
20
 
21
- "check-updates": "npx check-version-modules",
21
+ "build": "npx tsc --project \"./tsconfig.json\"",
22
22
 
23
- "compile": "npx tsc --project \"./tsconfig.json\"",
23
+ "lint": "npx eslint ./test/**/*.js",
24
+ "check-requires": "npx used-deps-analyzer \"./package.json\" \"./src\" --no-dev --overkill \"fs-extra\" \"node-promfs\"",
25
+ "check-updates": "npx check-version-modules",
26
+ "unit-tests": "npm run build && npx nyc --reporter=html --reporter=text mocha",
24
27
 
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",
28
+ "tests": "npm run-script lint && npm run check-requires && npm run-script check-updates && npm run-script unit-tests",
27
29
  "ci": "npm run-script tests && npx nyc report --reporter=text-lcov | coveralls"
28
30
 
29
31
  },
@@ -62,21 +64,21 @@
62
64
  "url": "https://github.com/Psychopoulet/node-confmanager/issues"
63
65
  },
64
66
  "dependencies": {
65
- "node-containerpattern": "1.7.3",
66
- "fs-extra": "11.1.0"
67
+ "node-containerpattern": "1.7.5"
67
68
  },
68
69
  "devDependencies": {
69
- "@types/fs-extra": "11.0.0",
70
- "@types/node": "18.11.18",
70
+ "@types/node": "20.5.0",
71
71
  "check-version-modules": "1.3.5",
72
72
  "coveralls": "3.1.1",
73
+ "eslint": "8.47.0",
73
74
  "husky": "8.0.3",
74
75
  "mocha": "10.2.0",
75
76
  "nyc": "15.1.0",
76
- "typescript": "4.9.4"
77
+ "typescript": "5.1.6",
78
+ "used-deps-analyzer": "0.1.2"
77
79
  },
78
80
  "homepage": "https://github.com/Psychopoulet/node-confmanager#readme",
79
81
  "engines": {
80
- "node": ">=12.0.0"
82
+ "node": ">=16.0.0"
81
83
  }
82
84
  }