node-confmanager 1.6.1 → 1.7.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2020, Sébastien Vidal
1
+ Copyright (c) 2022, Sébastien Vidal
2
2
 
3
3
  Permission to use, copy, modify, and/or distribute this software for any
4
4
  purpose with or without fee is hereby granted, provided that the above
@@ -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
+ ;
@@ -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,2 @@
1
+ import NodeConfManager from "./NodeConfManager";
2
+ export = NodeConfManager;
@@ -0,0 +1,4 @@
1
+ export default function checkShortcut(key: string, shortkey: string): {
2
+ "key": string;
3
+ "shortkey": string;
4
+ };
@@ -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.6.1",
3
+ "version": "1.7.1",
4
4
  "description": "A configuration manager",
5
- "main": "lib/main.js",
6
- "typings": "lib/index.d.ts",
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
- "lint": "npx eslint ./lib/**/*.js ./test/**/*.js",
20
+
9
21
  "check-updates": "npx check-version-modules",
10
- "unit-tests": "npx nyc --reporter=html --reporter=text mocha",
11
- "tests": "npm run-script lint && npm run-script check-updates && npm run-script unit-tests",
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.6.0",
49
- "fs-extra": "10.1.0"
65
+ "node-containerpattern": "1.7.3",
66
+ "fs-extra": "11.1.0"
50
67
  },
51
68
  "devDependencies": {
52
- "@types/node": "17.0.35",
69
+ "@types/fs-extra": "11.0.0",
70
+ "@types/node": "18.11.18",
53
71
  "check-version-modules": "1.3.5",
54
72
  "coveralls": "3.1.1",
55
- "eslint": "8.16.0",
56
- "husky": "8.0.1",
57
- "mocha": "10.0.0",
73
+ "husky": "8.0.3",
74
+ "mocha": "10.2.0",
58
75
  "nyc": "15.1.0",
59
- "typescript": "4.7.2"
76
+ "typescript": "4.9.4"
60
77
  },
61
78
  "homepage": "https://github.com/Psychopoulet/node-confmanager#readme",
62
79
  "engines": {
63
- "node": ">=10.0.0"
80
+ "node": ">=12.0.0"
64
81
  }
65
82
  }
@@ -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,183 +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
- const argument = arg.slice(isShortcut ? 1 : 2, arg.length);
64
-
65
- if (argument && (!isShortcut || this.shortcuts[argument])) {
66
-
67
- const key = isShortcut ? this.shortcuts[argument] : argument;
68
-
69
- if (this.skeletons[key] && "boolean" === this.skeletons[key]) {
70
- this.set(key, true);
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
- }
84
-
85
- }
86
-
87
- }
88
-
89
- });
90
-
91
- return Promise.resolve();
92
-
93
- });
94
-
95
- }
96
-
97
- // public
98
-
99
- // Container.clear & clearShortcuts
100
- clear () {
101
- super.clear();
102
- this.clearShortcuts();
103
- }
104
-
105
- // forget all the shortcuts
106
- clearShortcuts () {
107
- this.shortcuts = [];
108
- return this;
109
- }
110
-
111
- // delete the conf file
112
- deleteFile () {
113
-
114
- return !this.filePath ? Promise.resolve() : pathExists(this.filePath).then((exists) => {
115
- return exists ? unlink(this.filePath) : Promise.resolve();
116
- });
117
-
118
- }
119
-
120
- // check if the conf file exists
121
- fileExists () {
122
- return !this.filePath ? Promise.resolve(false) : pathExists(this.filePath);
123
- }
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)
131
- load () {
132
-
133
- this.clearData();
134
-
135
- return this.fileExists().then((exists) => {
136
-
137
- return !exists ? this._loadFromConsole() : readJson(this.filePath, "utf8").then((data) => {
138
-
139
- for (const key in data) {
140
- this.set(key, data[key]);
141
- }
142
-
143
- return this._loadFromConsole();
144
-
145
- });
146
-
147
- });
148
-
149
- }
150
-
151
- // save data into conf file
152
- save () {
153
-
154
- return !this.filePath ? Promise.resolve() : mkdirp(dirname(this.filePath)).then(() => {
155
-
156
- const objects = {};
157
- this.forEach((value, key) => {
158
- objects[key] = value;
159
- });
160
-
161
- return this.spaces ? writeJson(this.filePath, objects, {
162
- "encoding": "utf8",
163
- "space": 2
164
- }) : writeJson(this.filePath, objects, {
165
- "encoding": "utf8"
166
- });
167
-
168
- });
169
-
170
- }
171
-
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
-
181
- }
182
-
183
- };