@coderich/sandman 0.0.7 → 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 +63 -38
- package/src/ReadlineService.js +8 -5
- package/src/Sandman.js +45 -14
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');
|
|
@@ -9,31 +10,53 @@ module.exports = class ConfigClient extends Config {
|
|
|
9
10
|
#configDir; #mergeData = {};
|
|
10
11
|
|
|
11
12
|
constructor(configDir) {
|
|
12
|
-
super(
|
|
13
|
+
super({}, {
|
|
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
|
+
},
|
|
25
|
+
});
|
|
13
26
|
this.#configDir = configDir;
|
|
14
27
|
this.mergeDir();
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
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
|
-
});
|
|
31
|
+
if (key?.startsWith?.('.')) {
|
|
32
|
+
const { dictionary } = this.toObject();
|
|
33
|
+
const k = key.substring(1);
|
|
34
|
+
return k.length ? Util.get(dictionary['.'], k) : dictionary['.'];
|
|
35
|
+
}
|
|
28
36
|
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
37
|
+
const data = super.get(key, ...args);
|
|
38
|
+
const mergedData = this.#mergeMergeData(key, data);
|
|
39
|
+
this.set(dataSymbol, mergedData);
|
|
32
40
|
const resolvedData = super.get(dataSymbol);
|
|
33
|
-
|
|
41
|
+
this.del(dataSymbol);
|
|
34
42
|
return resolvedData;
|
|
35
43
|
}
|
|
36
44
|
|
|
45
|
+
raw(key) {
|
|
46
|
+
const data = Util.get(this.toObject().config, key);
|
|
47
|
+
return this.#mergeMergeData(key, data);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
set(key = '', value) {
|
|
51
|
+
if (key.startsWith?.('.')) return this.resolve({ '.': { [key.substring(1)]: value } });
|
|
52
|
+
return super.set(key, value);
|
|
53
|
+
}
|
|
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
|
+
|
|
37
60
|
mergeDir(dir = this.#configDir) {
|
|
38
61
|
return this.merge(Config.parseDir(dir, (...args) => this.#ignore(...args)));
|
|
39
62
|
}
|
|
@@ -61,6 +84,31 @@ module.exports = class ConfigClient extends Config {
|
|
|
61
84
|
return watcher;
|
|
62
85
|
}
|
|
63
86
|
|
|
87
|
+
#mergeMergeData(key, data) {
|
|
88
|
+
const flatData = key === undefined ? Util.flatten(data) : Util.flatten({ [key]: data });
|
|
89
|
+
const apiKeys = Array.from(new Set(Object.keys(flatData).map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean)));
|
|
90
|
+
|
|
91
|
+
const mergeKeys = Object.keys(this.#mergeData).reverse();
|
|
92
|
+
|
|
93
|
+
apiKeys.forEach((apiKey) => {
|
|
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
|
+
}
|
|
100
|
+
});
|
|
101
|
+
if (this.#mergeData[dataSymbol]) {
|
|
102
|
+
Object.entries(this.#mergeData[dataSymbol]).forEach(([k, v]) => {
|
|
103
|
+
flatData[`${apiKey}.${k}`] ??= v;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const unflatData = Util.unflatten(flatData);
|
|
109
|
+
return key === undefined ? unflatData : Util.get(unflatData, key);
|
|
110
|
+
}
|
|
111
|
+
|
|
64
112
|
#ignore({ name, filepath, paths }) {
|
|
65
113
|
if (name.startsWith('.')) return true;
|
|
66
114
|
|
|
@@ -83,29 +131,6 @@ module.exports = class ConfigClient extends Config {
|
|
|
83
131
|
return { ...parsed, filepath, paths, key };
|
|
84
132
|
};
|
|
85
133
|
|
|
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
134
|
// mergeConfigDir(dir) {
|
|
110
135
|
// const ignored = (parsed) => {
|
|
111
136
|
// 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,19 @@ 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: (
|
|
67
|
+
set: (...args) => this.#configClient.set(...args),
|
|
68
|
+
del: (...args) => this.#configClient.del(...args),
|
|
72
69
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
73
70
|
quit: () => process.exit(),
|
|
74
71
|
}, {
|
|
@@ -89,6 +86,40 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
89
86
|
|
|
90
87
|
return value;
|
|
91
88
|
},
|
|
89
|
+
}), {
|
|
90
|
+
'/': {
|
|
91
|
+
configurable: true,
|
|
92
|
+
value: (...args) => this.#run(...args),
|
|
93
|
+
},
|
|
94
|
+
run: {
|
|
95
|
+
configurable: true,
|
|
96
|
+
value: (...args) => this.#run(...args),
|
|
97
|
+
},
|
|
98
|
+
resolve: {
|
|
99
|
+
configurable: true,
|
|
100
|
+
value: (...args) => {
|
|
101
|
+
this.#configClient.resolve(...args);
|
|
102
|
+
return this.#cli;
|
|
103
|
+
},
|
|
104
|
+
},
|
|
92
105
|
});
|
|
93
106
|
}
|
|
107
|
+
|
|
108
|
+
static parseArgs(line) {
|
|
109
|
+
const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
110
|
+
const args = [];
|
|
111
|
+
let match;
|
|
112
|
+
|
|
113
|
+
while ((match = regex.exec(line)) !== null) {
|
|
114
|
+
if (match[1] !== undefined) {
|
|
115
|
+
args.push(match[1]); // double-quoted
|
|
116
|
+
} else if (match[2] !== undefined) {
|
|
117
|
+
args.push(match[2]); // single-quoted
|
|
118
|
+
} else if (match[3] !== undefined) {
|
|
119
|
+
args.push(match[3]); // bare word
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return args;
|
|
124
|
+
}
|
|
94
125
|
};
|