@coderich/sandman 0.0.7 → 0.0.8
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 +37 -38
- package/src/ReadlineService.js +8 -5
- package/src/Sandman.js +40 -14
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
|
@@ -9,31 +9,38 @@ module.exports = class ConfigClient extends Config {
|
|
|
9
9
|
#configDir; #mergeData = {};
|
|
10
10
|
|
|
11
11
|
constructor(configDir) {
|
|
12
|
-
super(
|
|
12
|
+
super({}, {
|
|
13
|
+
file: name => `file-${name}`,
|
|
14
|
+
});
|
|
13
15
|
this.#configDir = configDir;
|
|
14
16
|
this.mergeDir();
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
get(key, ...args) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
Reflect.ownKeys(this.#mergeData).forEach((mergeKey) => {
|
|
24
|
-
if (mergeKey === dataSymbol) flatData[apiKey] = { ...this.#mergeData[dataSymbol], ...flatData[apiKey] };
|
|
25
|
-
else if (apiKey.startsWith(mergeKey)) flatData[apiKey] = { ...this.#mergeData[mergeKey][dataSymbol], ...flatData[apiKey] };
|
|
26
|
-
});
|
|
27
|
-
});
|
|
20
|
+
if (key?.startsWith?.('.')) {
|
|
21
|
+
const { dictionary } = this.toObject();
|
|
22
|
+
const k = key.substring(1);
|
|
23
|
+
return k.length ? Util.get(dictionary['.'], k) : dictionary['.'];
|
|
24
|
+
}
|
|
28
25
|
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
26
|
+
const data = super.get(key, ...args);
|
|
27
|
+
const mergedData = this.#mergeMergeData(key, data);
|
|
28
|
+
this.set(dataSymbol, mergedData);
|
|
32
29
|
const resolvedData = super.get(dataSymbol);
|
|
33
|
-
|
|
30
|
+
this.del(dataSymbol);
|
|
34
31
|
return resolvedData;
|
|
35
32
|
}
|
|
36
33
|
|
|
34
|
+
raw(key) {
|
|
35
|
+
const data = Util.get(this.toObject().config, key);
|
|
36
|
+
return this.#mergeMergeData(key, data);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
set(key = '', value) {
|
|
40
|
+
if (key.startsWith?.('.')) return this.resolve({ '.': { [key.substring(1)]: value } });
|
|
41
|
+
return super.set(key, value);
|
|
42
|
+
}
|
|
43
|
+
|
|
37
44
|
mergeDir(dir = this.#configDir) {
|
|
38
45
|
return this.merge(Config.parseDir(dir, (...args) => this.#ignore(...args)));
|
|
39
46
|
}
|
|
@@ -61,6 +68,21 @@ module.exports = class ConfigClient extends Config {
|
|
|
61
68
|
return watcher;
|
|
62
69
|
}
|
|
63
70
|
|
|
71
|
+
#mergeMergeData(key, data) {
|
|
72
|
+
const flatData = key === undefined ? Util.flatten(data) : Util.flatten({ [key]: data });
|
|
73
|
+
const apiKeys = Array.from(new Set(Object.keys(flatData).map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean)));
|
|
74
|
+
|
|
75
|
+
apiKeys.forEach((apiKey) => {
|
|
76
|
+
Reflect.ownKeys(this.#mergeData).forEach((mergeKey) => {
|
|
77
|
+
if (mergeKey === dataSymbol) flatData[apiKey] = { ...this.#mergeData[dataSymbol], ...flatData[apiKey] };
|
|
78
|
+
else if (apiKey.startsWith(mergeKey)) flatData[apiKey] = { ...this.#mergeData[mergeKey][dataSymbol], ...flatData[apiKey] };
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const unflatData = Util.unflatten(flatData);
|
|
83
|
+
return key === undefined ? unflatData : Util.get(unflatData, key);
|
|
84
|
+
}
|
|
85
|
+
|
|
64
86
|
#ignore({ name, filepath, paths }) {
|
|
65
87
|
if (name.startsWith('.')) return true;
|
|
66
88
|
|
|
@@ -83,29 +105,6 @@ module.exports = class ConfigClient extends Config {
|
|
|
83
105
|
return { ...parsed, filepath, paths, key };
|
|
84
106
|
};
|
|
85
107
|
|
|
86
|
-
// exports.decorateRequest = (mergeData, key, request) => {
|
|
87
|
-
// const toMerge = key.split('.').reduce((prev, k, i, arr) => {
|
|
88
|
-
// const $key = arr.slice(0, i).join('.');
|
|
89
|
-
// return Merge({}, prev, mergeData[$key]?.request);
|
|
90
|
-
// }, Merge({}, mergeData.request));
|
|
91
|
-
|
|
92
|
-
// return Merge({}, toMerge, request);
|
|
93
|
-
// };
|
|
94
|
-
|
|
95
|
-
// #get(key, ...rest) {
|
|
96
|
-
// const { config } = this.#configClient.toObject();
|
|
97
|
-
// const value = get(config, key);
|
|
98
|
-
|
|
99
|
-
// if (value?.request) {
|
|
100
|
-
// const $request = FetchService.decorateRequest(this.#mergeData, key, value.request);
|
|
101
|
-
// const request = this.#configClient.resolve({ vars: value.request.vars }).set(resolveSymbol, $request).get(resolveSymbol);
|
|
102
|
-
// this.#configClient.del(resolveSymbol);
|
|
103
|
-
// return Merge({}, value, { request });
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
|
-
// return this.#configClient.get(key, ...rest);
|
|
107
|
-
// }
|
|
108
|
-
|
|
109
108
|
// mergeConfigDir(dir) {
|
|
110
109
|
// const ignored = (parsed) => {
|
|
111
110
|
// if (parsed.name.startsWith('.')) return true;
|
package/src/ReadlineService.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const Readline = require('readline');
|
|
2
|
-
const
|
|
2
|
+
const Util = require('@coderich/util');
|
|
3
3
|
|
|
4
4
|
exports.createInterface = (cli, configClient) => {
|
|
5
5
|
const captureInfo = {
|
|
@@ -28,13 +28,16 @@ exports.createInterface = (cli, configClient) => {
|
|
|
28
28
|
if (lastToken.startsWith('.')) {
|
|
29
29
|
const api = configClient.get(tokens.at(-2));
|
|
30
30
|
if (!api?.request) return [[], path];
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
return [Object.keys(
|
|
31
|
+
const { config } = configClient.toObject();
|
|
32
|
+
const conf = Util.get(config, tokens.at(-2), {});
|
|
33
|
+
return [Object.keys(conf), path];
|
|
34
|
+
// const dataPath = ['request'].concat(paths.slice(1, -1)).join('.');
|
|
35
|
+
// const data = get(api, dataPath, {});
|
|
36
|
+
// return [Object.keys(data).filter(k => k.toLowerCase().startsWith(path.toLowerCase())), path];
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
//
|
|
37
|
-
const flatKeys = Object.keys(flatten(configClient.get()));
|
|
40
|
+
const flatKeys = Object.keys(Util.flatten(configClient.get()));
|
|
38
41
|
|
|
39
42
|
// These keys follow the typing of the user
|
|
40
43
|
const startsWithCandidates = Array.from(new Set(flatKeys.map((flatKey) => {
|
package/src/Sandman.js
CHANGED
|
@@ -15,7 +15,7 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
15
15
|
|
|
16
16
|
this.#readline.on('line', async (line) => {
|
|
17
17
|
if (!line) return this.#prompt();
|
|
18
|
-
const [cmd, ...args] = line.trim()
|
|
18
|
+
const [cmd, ...args] = Sandman.parseArgs(line.trim());
|
|
19
19
|
const info = this.#cli[cmd] ? { cmd, args } : { cmd: 'run', args: [cmd, ...args] };
|
|
20
20
|
const value = await Promise.resolve(this.#cli[info.cmd](...info.args)).catch(e => e);
|
|
21
21
|
return this.emit(cmd, value);
|
|
@@ -26,15 +26,7 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
cli() {
|
|
29
|
-
return
|
|
30
|
-
resolve: {
|
|
31
|
-
value: (...args) => {
|
|
32
|
-
this.#configClient.resolve(...args);
|
|
33
|
-
return this.#cli;
|
|
34
|
-
},
|
|
35
|
-
configurable: true,
|
|
36
|
-
},
|
|
37
|
-
});
|
|
29
|
+
return this.#cli;
|
|
38
30
|
}
|
|
39
31
|
|
|
40
32
|
#run(key) {
|
|
@@ -61,14 +53,18 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
61
53
|
return this;
|
|
62
54
|
}
|
|
63
55
|
|
|
56
|
+
set(key, value) {
|
|
57
|
+
if (key.startsWith?.('.')) this.resolve({ '.': { [key.substring(1)]: value } });
|
|
58
|
+
return super.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
|
|
64
61
|
#createCLI() {
|
|
65
62
|
const self = this;
|
|
66
63
|
|
|
67
|
-
this.#cli = new Proxy({
|
|
68
|
-
|
|
64
|
+
this.#cli = Object.defineProperties(new Proxy({
|
|
65
|
+
raw: key => this.#configClient.raw(key),
|
|
69
66
|
get: (...args) => this.#configClient.get(...args),
|
|
70
|
-
set: (
|
|
71
|
-
del: (key = '') => this.#configClient.del(key),
|
|
67
|
+
set: (...args) => this.#configClient.set(...args),
|
|
72
68
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
73
69
|
quit: () => process.exit(),
|
|
74
70
|
}, {
|
|
@@ -89,6 +85,36 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
89
85
|
|
|
90
86
|
return value;
|
|
91
87
|
},
|
|
88
|
+
}), {
|
|
89
|
+
run: {
|
|
90
|
+
configurable: true,
|
|
91
|
+
value: (...args) => this.#run(...args),
|
|
92
|
+
},
|
|
93
|
+
resolve: {
|
|
94
|
+
configurable: true,
|
|
95
|
+
value: (...args) => {
|
|
96
|
+
this.#configClient.resolve(...args);
|
|
97
|
+
return this.#cli;
|
|
98
|
+
},
|
|
99
|
+
},
|
|
92
100
|
});
|
|
93
101
|
}
|
|
102
|
+
|
|
103
|
+
static parseArgs(line) {
|
|
104
|
+
const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
105
|
+
const args = [];
|
|
106
|
+
let match;
|
|
107
|
+
|
|
108
|
+
while ((match = regex.exec(line)) !== null) {
|
|
109
|
+
if (match[1] !== undefined) {
|
|
110
|
+
args.push(match[1]); // double-quoted
|
|
111
|
+
} else if (match[2] !== undefined) {
|
|
112
|
+
args.push(match[2]); // single-quoted
|
|
113
|
+
} else if (match[3] !== undefined) {
|
|
114
|
+
args.push(match[3]); // bare word
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return args;
|
|
119
|
+
}
|
|
94
120
|
};
|