@coderich/sandman 0.0.18 → 0.1.1
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/Sandman.js +19 -6
package/package.json
CHANGED
package/src/Sandman.js
CHANGED
|
@@ -21,15 +21,21 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
21
21
|
this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
|
|
22
22
|
this.#configClient.watch(this.#configDir, event => this.emit('save', event));
|
|
23
23
|
|
|
24
|
-
this.#readline.on('line',
|
|
24
|
+
this.#readline.on('line', (line) => {
|
|
25
25
|
if (!line) return this.prompt();
|
|
26
26
|
const [cmd, key, ...args] = UtilService.parseArgs(line.trim());
|
|
27
27
|
const $cmd = Object.keys(this.#cli).includes(cmd) ? cmd : cmdNotFound;
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
return this.#cli[$cmd](key, ...args)
|
|
29
|
+
.then(value => this.emit($cmd, value))
|
|
30
|
+
.catch(error => this.emit('error', { key: $cmd, error }));
|
|
30
31
|
});
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
emit(event, ...args) {
|
|
35
|
+
if (event === 'error' && !this.listenerCount('error')) return false;
|
|
36
|
+
return super.emit(event, ...args);
|
|
37
|
+
}
|
|
38
|
+
|
|
33
39
|
cli() {
|
|
34
40
|
return this.#cli;
|
|
35
41
|
}
|
|
@@ -78,7 +84,11 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
78
84
|
$: this.#configClient.raw(key),
|
|
79
85
|
[key]: this.#configClient.get(key),
|
|
80
86
|
}),
|
|
81
|
-
curl: key =>
|
|
87
|
+
curl: (key) => {
|
|
88
|
+
const { path, params, ...raw } = this.#configClient.get(key, {}).request ?? {};
|
|
89
|
+
const { url } = FetchService.normalizeRequest({ ...raw, path, params });
|
|
90
|
+
return CURLService.toCURL({ ...raw, url });
|
|
91
|
+
},
|
|
82
92
|
quit: () => process.exit(),
|
|
83
93
|
}, {
|
|
84
94
|
get(obj, prop, receiver) {
|
|
@@ -86,10 +96,13 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
86
96
|
|
|
87
97
|
if (typeof value === 'function') {
|
|
88
98
|
return (...args) => {
|
|
99
|
+
const result = new Promise((resolve) => { resolve(value.apply(this, args)); });
|
|
100
|
+
|
|
101
|
+
if (self.#cliCounter > 0) return result;
|
|
102
|
+
|
|
89
103
|
self.#cliCounter++;
|
|
90
104
|
self.#readline.pause();
|
|
91
|
-
|
|
92
|
-
Promise.resolve(result).catch(() => null).finally(() => setImmediate(() => {
|
|
105
|
+
result.catch(() => null).finally(() => setImmediate(() => {
|
|
93
106
|
if (--self.#cliCounter === 0) self.prompt();
|
|
94
107
|
}));
|
|
95
108
|
return result;
|