@coderich/sandman 0.1.1 → 0.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Sandman.js +17 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coderich/sandman",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/Sandman.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const Path = require('node:path');
2
2
  const EventEmitter = require('node:events');
3
3
  const { spawn } = require('node:child_process');
4
+ const Util = require('@coderich/util');
4
5
  const ReadlineService = require('./ReadlineService');
5
6
  const UtilService = require('./UtilService');
6
7
  const CURLService = require('./CURLService');
@@ -23,9 +24,10 @@ module.exports = class Sandman extends EventEmitter {
23
24
 
24
25
  this.#readline.on('line', (line) => {
25
26
  if (!line) return this.prompt();
26
- const [cmd, key, ...args] = UtilService.parseArgs(line.trim());
27
+ const [cmd, key = this.#configClient.get('$key'), ...args] = UtilService.parseArgs(line.trim());
27
28
  const $cmd = Object.keys(this.#cli).includes(cmd) ? cmd : cmdNotFound;
28
- return this.#cli[$cmd](key, ...args)
29
+ if ($cmd === '/') this.#configClient.set('$key', key);
30
+ return Promise.resolve(this.#cli[$cmd](key, ...args))
29
31
  .then(value => this.emit($cmd, value))
30
32
  .catch(error => this.emit('error', { key: $cmd, error }));
31
33
  });
@@ -41,12 +43,12 @@ module.exports = class Sandman extends EventEmitter {
41
43
  }
42
44
 
43
45
  prompt() {
44
- this.#readline.setPrompt(this.#configClient.get('prompt'));
46
+ this.#readline.setPrompt(this.#configClient.get('$prompt'));
45
47
  this.#readline.prompt(true);
46
48
  return this;
47
49
  }
48
50
 
49
- #run(key, opts = { emit: true }) {
51
+ async #run(key, opts = { emit: true }) {
50
52
  const api = this.#configClient.get(key, {});
51
53
 
52
54
  if (!api?.request) {
@@ -60,6 +62,12 @@ module.exports = class Sandman extends EventEmitter {
60
62
 
61
63
  return FetchService.fetch(request).then(({ response, data }) => {
62
64
  if (opts.emit) this.emit('response', { response, api, key, data });
65
+
66
+ // Assign feature
67
+ Object.entries(api.assign || {}).forEach(([k, path]) => {
68
+ this.#configClient.set(k, path ? Util.get(data, path) : data);
69
+ });
70
+
63
71
  return { response, api, key, data };
64
72
  }).catch((error) => {
65
73
  if (opts.emit) this.emit('error', { key, api, error });
@@ -74,8 +82,8 @@ module.exports = class Sandman extends EventEmitter {
74
82
 
75
83
  this.#cli = Object.defineProperties(new Proxy({
76
84
  '/': (...args) => this.#run(...args),
77
- edit: (key, ext = '.yaml') => {
78
- const path = this.#configClient.get('ide', 'open');
85
+ edit: (key) => {
86
+ const path = this.#configClient.get('$ide', 'open');
79
87
  const filePath = Path.join(this.#configDir, ...key.split('.')).concat('.yaml');
80
88
  const child = spawn(path, [filePath], { detached: true, stdio: 'ignore' });
81
89
  child.unref();
@@ -84,7 +92,7 @@ module.exports = class Sandman extends EventEmitter {
84
92
  $: this.#configClient.raw(key),
85
93
  [key]: this.#configClient.get(key),
86
94
  }),
87
- curl: (key) => {
95
+ curl: async (key) => {
88
96
  const { path, params, ...raw } = this.#configClient.get(key, {}).request ?? {};
89
97
  const { url } = FetchService.normalizeRequest({ ...raw, path, params });
90
98
  return CURLService.toCURL({ ...raw, url });
@@ -96,13 +104,10 @@ module.exports = class Sandman extends EventEmitter {
96
104
 
97
105
  if (typeof value === 'function') {
98
106
  return (...args) => {
99
- const result = new Promise((resolve) => { resolve(value.apply(this, args)); });
100
-
101
- if (self.#cliCounter > 0) return result;
102
-
103
107
  self.#cliCounter++;
104
108
  self.#readline.pause();
105
- result.catch(() => null).finally(() => setImmediate(() => {
109
+ const result = value.apply(this, args);
110
+ Promise.resolve(result).catch(() => null).finally(() => setImmediate(() => {
106
111
  if (--self.#cliCounter === 0) self.prompt();
107
112
  }));
108
113
  return result;