@coderich/sandman 0.0.1 → 0.0.2
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/FetchService.js +5 -3
- package/src/Sandman.js +44 -11
- package/src/app.js +2 -2
package/package.json
CHANGED
package/src/FetchService.js
CHANGED
|
@@ -13,7 +13,9 @@ exports.fetch = (req) => {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
exports.normalizeRequest = (req) => {
|
|
16
|
+
req.path ??= '';
|
|
16
17
|
req.method ??= 'get'; req.headers ??= {}; req.params ??= {};
|
|
18
|
+
req.url += req.path;
|
|
17
19
|
req.url = Object.entries(req.params).reduce((url, [key, value]) => { url.searchParams.append(key, value); return url; }, new URL(req.url)).toString();
|
|
18
20
|
req.headers = Object.entries(req.headers).reduce((prev, [key, value]) => Object.assign(prev, { [key.toLowerCase()]: value }), {});
|
|
19
21
|
const [contentType] = req.headers['content-type']?.split(';') || [];
|
|
@@ -50,8 +52,8 @@ exports.normalizeRequest = (req) => {
|
|
|
50
52
|
exports.decorateRequest = (mergeData, key, request) => {
|
|
51
53
|
const toMerge = key.split('.').reduce((prev, k, i, arr) => {
|
|
52
54
|
const $key = arr.slice(0, i).join('.');
|
|
53
|
-
return Merge(prev, mergeData[$key]?.request);
|
|
54
|
-
}, {
|
|
55
|
+
return Merge({}, prev, mergeData[$key]?.request);
|
|
56
|
+
}, Merge({}, mergeData.request));
|
|
55
57
|
|
|
56
|
-
return Merge(toMerge, request);
|
|
58
|
+
return Merge({}, toMerge, request);
|
|
57
59
|
};
|
package/src/Sandman.js
CHANGED
|
@@ -6,8 +6,11 @@ const { get, flatten } = require('@coderich/util');
|
|
|
6
6
|
const FetchService = require('./FetchService');
|
|
7
7
|
const ConfigClient = require('./ConfigClient');
|
|
8
8
|
|
|
9
|
+
const resolveSymbol = Symbol('resolve');
|
|
10
|
+
|
|
9
11
|
module.exports = class Sandman extends EventEmitter {
|
|
10
12
|
#configClient; #configDir; #options; #watcher; #readline; #mergeData = {}; #cli;
|
|
13
|
+
#captureCandidates = false; #candidates = []; #tabCounter = 0; #candidateIndex = 0; #line; #lastToken;
|
|
11
14
|
|
|
12
15
|
constructor(configDir, options) {
|
|
13
16
|
super();
|
|
@@ -29,7 +32,16 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
cli() {
|
|
32
|
-
return this.#cli
|
|
35
|
+
return Object.defineProperties(this.#cli, {
|
|
36
|
+
resolve: {
|
|
37
|
+
value: (...args) => {
|
|
38
|
+
this.#configClient.resolve(...args);
|
|
39
|
+
this.#prompt();
|
|
40
|
+
return this.#cli;
|
|
41
|
+
},
|
|
42
|
+
configurable: true,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
#run(key) {
|
|
@@ -60,7 +72,9 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
60
72
|
|
|
61
73
|
if (value?.request) {
|
|
62
74
|
const request = FetchService.decorateRequest(this.#mergeData, key, value.request);
|
|
63
|
-
this.#configClient.
|
|
75
|
+
const $request = this.#configClient.set(resolveSymbol, request).get(resolveSymbol);
|
|
76
|
+
this.#configClient.del(resolveSymbol);
|
|
77
|
+
return $request;
|
|
64
78
|
}
|
|
65
79
|
|
|
66
80
|
return this.#configClient.get(key, ...rest);
|
|
@@ -106,7 +120,7 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
106
120
|
const paths = lastToken.split('.');
|
|
107
121
|
const path = paths.at(-1);
|
|
108
122
|
|
|
109
|
-
// Specific request
|
|
123
|
+
// Specific request.data selector
|
|
110
124
|
if (lastToken.startsWith('.')) {
|
|
111
125
|
const api = this.#get(tokens.at(-2));
|
|
112
126
|
if (!api?.request) return [[], path];
|
|
@@ -120,34 +134,51 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
120
134
|
|
|
121
135
|
// These keys follow the typing of the user
|
|
122
136
|
const startsWithCandidates = Array.from(new Set(flatKeys.map((flatKey) => {
|
|
123
|
-
return flatKey.split('.').slice(0, paths.length).join('.');
|
|
137
|
+
return flatKey.split('.').slice(0, paths.length).join('.');
|
|
124
138
|
}))).filter((c) => {
|
|
125
139
|
return c.toLowerCase().startsWith(lastToken.toLowerCase());
|
|
126
|
-
}).map(p => p.split('.').at(-1)); // Here!
|
|
140
|
+
}); // .map(p => p.split('.').at(-1)); // Here!
|
|
127
141
|
|
|
128
142
|
// These are shortcut keys to requests
|
|
129
143
|
const requestKeyCandidates = Array.from(new Set(flatKeys.map((flatKey) => {
|
|
130
144
|
const keys = flatKey.split('.');
|
|
131
145
|
const index = keys.indexOf('request');
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
146
|
+
return index && flatKey.split('.').slice(0, index).join('.');
|
|
147
|
+
// const typedPath = keys.slice(0, paths.length - 1).join('.');
|
|
148
|
+
// const autocompletePath = keys.slice(paths.length - 1, index).join('.');
|
|
149
|
+
// return index > 0 && lastToken.toLowerCase().startsWith(typedPath.toLowerCase()) && autocompletePath;
|
|
135
150
|
}).filter(Boolean))).filter((c) => {
|
|
136
|
-
return c.toLowerCase().includes(
|
|
151
|
+
return c.toLowerCase().includes(lastToken.toLowerCase());
|
|
152
|
+
// return c.toLowerCase().includes(path.toLowerCase());
|
|
137
153
|
});
|
|
138
154
|
|
|
139
155
|
const candidates = Array.from(new Set(startsWithCandidates.concat(requestKeyCandidates)));
|
|
140
|
-
|
|
141
|
-
return [candidates,
|
|
156
|
+
if (this.#captureCandidates) { this.#candidates = candidates; this.#line = line; this.#lastToken = lastToken; }
|
|
157
|
+
return [candidates, lastToken];
|
|
142
158
|
},
|
|
143
159
|
});
|
|
144
160
|
|
|
145
161
|
process.stdin.on('keypress', (ch, key) => {
|
|
162
|
+
if (key && key.name === 'tab') this.#tabCounter++; else this.#tabCounter = 0;
|
|
163
|
+
this.#captureCandidates = this.#tabCounter === 2;
|
|
164
|
+
|
|
146
165
|
if (key && key.name === 'escape') {
|
|
147
166
|
this.#readline.line = '';
|
|
148
167
|
Readline.cursorTo(process.stdout, 0);
|
|
149
168
|
Readline.clearLine(process.stdout, 0);
|
|
150
169
|
this.#readline.prompt();
|
|
170
|
+
} else if (this.#tabCounter > 2 && this.#candidates.length) {
|
|
171
|
+
let value = this.#candidates.at(this.#candidateIndex++);
|
|
172
|
+
|
|
173
|
+
if (!value) {
|
|
174
|
+
this.#candidateIndex = 0;
|
|
175
|
+
value = this.#candidates.at(this.#candidateIndex++);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
value = this.#line.replace(this.#lastToken, value);
|
|
179
|
+
this.#readline.line = value;
|
|
180
|
+
this.#readline.cursor = value.length;
|
|
181
|
+
this.#readline.prompt(true);
|
|
151
182
|
}
|
|
152
183
|
});
|
|
153
184
|
}
|
|
@@ -167,8 +198,10 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
167
198
|
if (key) this.#configClient.set(key, api);
|
|
168
199
|
else this.#configClient.merge(api);
|
|
169
200
|
if (api.request) this.emit('save', { key, api });
|
|
201
|
+
this.#prompt();
|
|
170
202
|
} else if (['unlink', 'unlinkDir'].includes(event)) {
|
|
171
203
|
this.#configClient.del(key);
|
|
204
|
+
this.#prompt();
|
|
172
205
|
}
|
|
173
206
|
});
|
|
174
207
|
}
|
package/src/app.js
CHANGED