@coderich/sandman 0.0.8 → 0.0.9
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/package.json +1 -1
- package/src/ConfigClient.js +30 -4
- package/src/Sandman.js +5 -0
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const FS = require('fs');
|
|
1
2
|
const Path = require('path');
|
|
2
3
|
const Chokidar = require('chokidar');
|
|
3
4
|
const Config = require('@coderich/config');
|
|
@@ -10,7 +11,17 @@ module.exports = class ConfigClient extends Config {
|
|
|
10
11
|
|
|
11
12
|
constructor(configDir) {
|
|
12
13
|
super({}, {
|
|
13
|
-
file: name =>
|
|
14
|
+
file: (name) => {
|
|
15
|
+
if (name == null || `${name}` === 'undefined') return name;
|
|
16
|
+
const path = Path.resolve(process.cwd(), name);
|
|
17
|
+
try {
|
|
18
|
+
const buffer = FS.readFileSync(path);
|
|
19
|
+
return new File([buffer], name);
|
|
20
|
+
} catch {
|
|
21
|
+
process.stdout.write(`Unable to load file: "${name}"\n`);
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
},
|
|
14
25
|
});
|
|
15
26
|
this.#configDir = configDir;
|
|
16
27
|
this.mergeDir();
|
|
@@ -41,6 +52,11 @@ module.exports = class ConfigClient extends Config {
|
|
|
41
52
|
return super.set(key, value);
|
|
42
53
|
}
|
|
43
54
|
|
|
55
|
+
del(key = '') {
|
|
56
|
+
if (key.startsWith?.('.')) return Util.set(this.toObject().dictionary['.'], [key.substring(1)], undefined);
|
|
57
|
+
return super.del(key);
|
|
58
|
+
}
|
|
59
|
+
|
|
44
60
|
mergeDir(dir = this.#configDir) {
|
|
45
61
|
return this.merge(Config.parseDir(dir, (...args) => this.#ignore(...args)));
|
|
46
62
|
}
|
|
@@ -72,11 +88,21 @@ module.exports = class ConfigClient extends Config {
|
|
|
72
88
|
const flatData = key === undefined ? Util.flatten(data) : Util.flatten({ [key]: data });
|
|
73
89
|
const apiKeys = Array.from(new Set(Object.keys(flatData).map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean)));
|
|
74
90
|
|
|
91
|
+
const mergeKeys = Object.keys(this.#mergeData).reverse();
|
|
92
|
+
|
|
75
93
|
apiKeys.forEach((apiKey) => {
|
|
76
|
-
|
|
77
|
-
if (mergeKey
|
|
78
|
-
|
|
94
|
+
mergeKeys.forEach((mergeKey) => {
|
|
95
|
+
if (apiKey.startsWith(mergeKey)) {
|
|
96
|
+
Object.entries(this.#mergeData[mergeKey][dataSymbol]).forEach(([k, v]) => {
|
|
97
|
+
flatData[`${apiKey}.${k}`] ??= v;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
79
100
|
});
|
|
101
|
+
if (this.#mergeData[dataSymbol]) {
|
|
102
|
+
Object.entries(this.#mergeData[dataSymbol]).forEach(([k, v]) => {
|
|
103
|
+
flatData[`${apiKey}.${k}`] ??= v;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
80
106
|
});
|
|
81
107
|
|
|
82
108
|
const unflatData = Util.unflatten(flatData);
|
package/src/Sandman.js
CHANGED
|
@@ -65,6 +65,7 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
65
65
|
raw: key => this.#configClient.raw(key),
|
|
66
66
|
get: (...args) => this.#configClient.get(...args),
|
|
67
67
|
set: (...args) => this.#configClient.set(...args),
|
|
68
|
+
del: (...args) => this.#configClient.del(...args),
|
|
68
69
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
69
70
|
quit: () => process.exit(),
|
|
70
71
|
}, {
|
|
@@ -86,6 +87,10 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
86
87
|
return value;
|
|
87
88
|
},
|
|
88
89
|
}), {
|
|
90
|
+
'/': {
|
|
91
|
+
configurable: true,
|
|
92
|
+
value: (...args) => this.#run(...args),
|
|
93
|
+
},
|
|
89
94
|
run: {
|
|
90
95
|
configurable: true,
|
|
91
96
|
value: (...args) => this.#run(...args),
|