@coderich/sandman 0.1.0 → 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.
- package/package.json +1 -1
- package/src/Sandman.js +21 -12
package/package.json
CHANGED
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
|
-
|
|
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
|
|
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,11 @@ 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) => {
|
|
96
|
+
const { path, params, ...raw } = this.#configClient.get(key, {}).request ?? {};
|
|
97
|
+
const { url } = FetchService.normalizeRequest({ ...raw, path, params });
|
|
98
|
+
return CURLService.toCURL({ ...raw, url });
|
|
99
|
+
},
|
|
88
100
|
quit: () => process.exit(),
|
|
89
101
|
}, {
|
|
90
102
|
get(obj, prop, receiver) {
|
|
@@ -92,13 +104,10 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
92
104
|
|
|
93
105
|
if (typeof value === 'function') {
|
|
94
106
|
return (...args) => {
|
|
95
|
-
const result = new Promise((resolve) => { resolve(value.apply(this, args)); });
|
|
96
|
-
|
|
97
|
-
if (self.#cliCounter > 0) return result;
|
|
98
|
-
|
|
99
107
|
self.#cliCounter++;
|
|
100
108
|
self.#readline.pause();
|
|
101
|
-
result
|
|
109
|
+
const result = value.apply(this, args);
|
|
110
|
+
Promise.resolve(result).catch(() => null).finally(() => setImmediate(() => {
|
|
102
111
|
if (--self.#cliCounter === 0) self.prompt();
|
|
103
112
|
}));
|
|
104
113
|
return result;
|