@coderich/sandman 0.0.18 → 0.1.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Sandman.js +14 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coderich/sandman",
3
- "version": "0.0.18",
3
+ "version": "0.1.0",
4
4
  "main": "index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
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', async (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
- const value = await Promise.resolve(this.#cli[$cmd](key, ...args)).catch(e => e);
29
- return this.emit($cmd, value);
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
  }
@@ -86,10 +92,13 @@ module.exports = class Sandman extends EventEmitter {
86
92
 
87
93
  if (typeof value === 'function') {
88
94
  return (...args) => {
95
+ const result = new Promise((resolve) => { resolve(value.apply(this, args)); });
96
+
97
+ if (self.#cliCounter > 0) return result;
98
+
89
99
  self.#cliCounter++;
90
100
  self.#readline.pause();
91
- const result = value.apply(this, args);
92
- Promise.resolve(result).catch(() => null).finally(() => setImmediate(() => {
101
+ result.catch(() => null).finally(() => setImmediate(() => {
93
102
  if (--self.#cliCounter === 0) self.prompt();
94
103
  }));
95
104
  return result;