@coderich/sandman 0.0.6 → 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 +39 -36
- package/src/FetchService.js +1 -1
- package/src/ReadlineService.js +8 -5
- package/src/Sandman.js +42 -15
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
|
@@ -9,27 +9,36 @@ 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) {
|
|
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
|
+
}
|
|
25
|
+
|
|
18
26
|
const data = super.get(key, ...args);
|
|
19
|
-
const
|
|
20
|
-
|
|
27
|
+
const mergedData = this.#mergeMergeData(key, data);
|
|
28
|
+
this.set(dataSymbol, mergedData);
|
|
29
|
+
const resolvedData = super.get(dataSymbol);
|
|
30
|
+
this.del(dataSymbol);
|
|
31
|
+
return resolvedData;
|
|
32
|
+
}
|
|
21
33
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
27
|
-
});
|
|
34
|
+
raw(key) {
|
|
35
|
+
const data = Util.get(this.toObject().config, key);
|
|
36
|
+
return this.#mergeMergeData(key, data);
|
|
37
|
+
}
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
super.set(
|
|
32
|
-
return super.get(dataSymbol);
|
|
39
|
+
set(key = '', value) {
|
|
40
|
+
if (key.startsWith?.('.')) return this.resolve({ '.': { [key.substring(1)]: value } });
|
|
41
|
+
return super.set(key, value);
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
mergeDir(dir = this.#configDir) {
|
|
@@ -55,6 +64,23 @@ module.exports = class ConfigClient extends Config {
|
|
|
55
64
|
this.del(key);
|
|
56
65
|
}
|
|
57
66
|
});
|
|
67
|
+
|
|
68
|
+
return watcher;
|
|
69
|
+
}
|
|
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);
|
|
58
84
|
}
|
|
59
85
|
|
|
60
86
|
#ignore({ name, filepath, paths }) {
|
|
@@ -79,29 +105,6 @@ module.exports = class ConfigClient extends Config {
|
|
|
79
105
|
return { ...parsed, filepath, paths, key };
|
|
80
106
|
};
|
|
81
107
|
|
|
82
|
-
// exports.decorateRequest = (mergeData, key, request) => {
|
|
83
|
-
// const toMerge = key.split('.').reduce((prev, k, i, arr) => {
|
|
84
|
-
// const $key = arr.slice(0, i).join('.');
|
|
85
|
-
// return Merge({}, prev, mergeData[$key]?.request);
|
|
86
|
-
// }, Merge({}, mergeData.request));
|
|
87
|
-
|
|
88
|
-
// return Merge({}, toMerge, request);
|
|
89
|
-
// };
|
|
90
|
-
|
|
91
|
-
// #get(key, ...rest) {
|
|
92
|
-
// const { config } = this.#configClient.toObject();
|
|
93
|
-
// const value = get(config, key);
|
|
94
|
-
|
|
95
|
-
// if (value?.request) {
|
|
96
|
-
// const $request = FetchService.decorateRequest(this.#mergeData, key, value.request);
|
|
97
|
-
// const request = this.#configClient.resolve({ vars: value.request.vars }).set(resolveSymbol, $request).get(resolveSymbol);
|
|
98
|
-
// this.#configClient.del(resolveSymbol);
|
|
99
|
-
// return Merge({}, value, { request });
|
|
100
|
-
// }
|
|
101
|
-
|
|
102
|
-
// return this.#configClient.get(key, ...rest);
|
|
103
|
-
// }
|
|
104
|
-
|
|
105
108
|
// mergeConfigDir(dir) {
|
|
106
109
|
// const ignored = (parsed) => {
|
|
107
110
|
// if (parsed.name.startsWith('.')) return true;
|
package/src/FetchService.js
CHANGED
|
@@ -65,7 +65,7 @@ exports.toCURL = (request, { pretty = true, redactAuth = false } = {}) => {
|
|
|
65
65
|
if (redactAuth && /^authorization$/i.test(k)) {
|
|
66
66
|
hdrs[k] = hdrs[k].replace(/(?<=^.{6}).+/, '***REDACTED***');
|
|
67
67
|
}
|
|
68
|
-
parts.push('-H', shq(`${k}: ${hdrs[k]}`));
|
|
68
|
+
parts.push('-H', exports.shq(`${k}: ${hdrs[k]}`));
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
// body (skip for GET)
|
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
|
@@ -14,26 +14,19 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
14
14
|
this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
|
|
15
15
|
|
|
16
16
|
this.#readline.on('line', async (line) => {
|
|
17
|
-
if (!line) return this.#
|
|
18
|
-
const [cmd, ...args] = line.trim()
|
|
17
|
+
if (!line) return this.#prompt();
|
|
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);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
|
+
this.#configClient.watch();
|
|
24
25
|
this.#prompt();
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
cli() {
|
|
28
|
-
return
|
|
29
|
-
resolve: {
|
|
30
|
-
value: (...args) => {
|
|
31
|
-
this.#configClient.resolve(...args);
|
|
32
|
-
return this.#cli;
|
|
33
|
-
},
|
|
34
|
-
configurable: true,
|
|
35
|
-
},
|
|
36
|
-
});
|
|
29
|
+
return this.#cli;
|
|
37
30
|
}
|
|
38
31
|
|
|
39
32
|
#run(key) {
|
|
@@ -60,14 +53,18 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
60
53
|
return this;
|
|
61
54
|
}
|
|
62
55
|
|
|
56
|
+
set(key, value) {
|
|
57
|
+
if (key.startsWith?.('.')) this.resolve({ '.': { [key.substring(1)]: value } });
|
|
58
|
+
return super.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
|
|
63
61
|
#createCLI() {
|
|
64
62
|
const self = this;
|
|
65
63
|
|
|
66
|
-
this.#cli = new Proxy({
|
|
67
|
-
|
|
64
|
+
this.#cli = Object.defineProperties(new Proxy({
|
|
65
|
+
raw: key => this.#configClient.raw(key),
|
|
68
66
|
get: (...args) => this.#configClient.get(...args),
|
|
69
|
-
set: (
|
|
70
|
-
del: (key = '') => this.#configClient.del(key),
|
|
67
|
+
set: (...args) => this.#configClient.set(...args),
|
|
71
68
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
72
69
|
quit: () => process.exit(),
|
|
73
70
|
}, {
|
|
@@ -88,6 +85,36 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
88
85
|
|
|
89
86
|
return value;
|
|
90
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
|
+
},
|
|
91
100
|
});
|
|
92
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
|
+
}
|
|
93
120
|
};
|