@coderich/sandman 0.0.10 → 0.0.12
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/ConfigClient.js +1 -1
- package/src/ReadlineService.js +10 -4
- package/src/Sandman.js +37 -13
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
package/src/ReadlineService.js
CHANGED
|
@@ -26,7 +26,8 @@ exports.createInterface = (cli, configClient) => {
|
|
|
26
26
|
const lastArg = args.at(-1) || '';
|
|
27
27
|
const lastPaths = lastArg.split('.');
|
|
28
28
|
const lastPath = lastPaths.at(-1); // current typing
|
|
29
|
-
const
|
|
29
|
+
const previousPaths = lastPaths.slice(0, -1);
|
|
30
|
+
const previousPath = previousPaths.join('.');
|
|
30
31
|
|
|
31
32
|
// Specific request.data selector
|
|
32
33
|
if (lastArg.startsWith('.')) {
|
|
@@ -40,9 +41,14 @@ exports.createInterface = (cli, configClient) => {
|
|
|
40
41
|
const requestKeys = flatKeys.map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean);
|
|
41
42
|
|
|
42
43
|
const candidates = Array.from(new Set(requestKeys
|
|
43
|
-
.filter(requestKey =>
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
.filter((requestKey) => {
|
|
45
|
+
const requestPaths = requestKey.split('.').map(rp => rp.toLowerCase());
|
|
46
|
+
return previousPaths.every(path => requestPaths.includes(path.toLowerCase()));
|
|
47
|
+
})
|
|
48
|
+
.filter((requestKey) => {
|
|
49
|
+
const requestPaths = requestKey.split('.');
|
|
50
|
+
return requestPaths.slice(lastPaths.length - 1).join('.').toLowerCase().includes(lastPath.toLowerCase());
|
|
51
|
+
})
|
|
46
52
|
));
|
|
47
53
|
|
|
48
54
|
if (captureInfo.captureCandidates && lastArg) { captureInfo.candidates = candidates; captureInfo.line = line; captureInfo.lastArg = lastArg; captureInfo.candidateIndex = -1; }
|
package/src/Sandman.js
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
+
const Path = require('path');
|
|
1
2
|
const EventEmitter = require('events');
|
|
3
|
+
const { spawn } = require('child_process');
|
|
2
4
|
const ReadlineService = require('./ReadlineService');
|
|
3
5
|
const UtilService = require('./UtilService');
|
|
4
6
|
const FetchService = require('./FetchService');
|
|
5
7
|
const ConfigClient = require('./ConfigClient');
|
|
6
8
|
|
|
9
|
+
const cmdNotFound = Symbol('cmdNotFound');
|
|
10
|
+
|
|
7
11
|
module.exports = class Sandman extends EventEmitter {
|
|
8
|
-
#configClient; #options; #readline; #cli; #cliCounter = 0;
|
|
12
|
+
#configClient; #options; #readline; #cli; #cliCounter = 0; #configDir;
|
|
9
13
|
|
|
10
14
|
constructor(configDir, options) {
|
|
11
15
|
super();
|
|
12
16
|
this.#options = options;
|
|
13
|
-
this.#
|
|
17
|
+
this.#configDir = configDir;
|
|
18
|
+
this.#configClient = new ConfigClient(this.#configDir);
|
|
14
19
|
this.#createCLI();
|
|
15
20
|
this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
|
|
16
|
-
this.#configClient.watch(configDir, event => this.emit('save', event));
|
|
21
|
+
this.#configClient.watch(this.#configDir, event => this.emit('save', event));
|
|
17
22
|
|
|
18
23
|
this.#readline.on('line', async (line) => {
|
|
19
24
|
if (!line) return this.#prompt();
|
|
20
|
-
const [cmd, ...args] = UtilService.parseArgs(line.trim());
|
|
21
|
-
const
|
|
22
|
-
const value = await Promise.resolve(this.#cli[
|
|
23
|
-
return this.emit(cmd, value);
|
|
25
|
+
const [cmd, key, ...args] = UtilService.parseArgs(line.trim());
|
|
26
|
+
const $cmd = Object.keys(this.#cli).includes(cmd) ? cmd : cmdNotFound;
|
|
27
|
+
const value = await Promise.resolve(this.#cli[$cmd](key, ...args)).catch(e => e);
|
|
28
|
+
return this.emit($cmd, value);
|
|
24
29
|
});
|
|
25
30
|
|
|
26
31
|
this.#prompt();
|
|
@@ -63,10 +68,17 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
63
68
|
const self = this;
|
|
64
69
|
|
|
65
70
|
this.#cli = Object.defineProperties(new Proxy({
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
'/': (...args) => this.#run(...args),
|
|
72
|
+
edit: (key, ext = '.yaml') => {
|
|
73
|
+
const path = this.#configClient.get('ide', 'open');
|
|
74
|
+
const filePath = Path.join(this.#configDir, ...key.split('.')).concat('.yaml');
|
|
75
|
+
const child = spawn(path, [filePath], { detached: true, stdio: 'ignore' });
|
|
76
|
+
child.unref();
|
|
77
|
+
},
|
|
78
|
+
view: key => ({
|
|
79
|
+
$: this.#configClient.raw(key),
|
|
80
|
+
[key]: this.#configClient.get(key),
|
|
81
|
+
}),
|
|
70
82
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
71
83
|
quit: () => process.exit(),
|
|
72
84
|
}, {
|
|
@@ -88,14 +100,26 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
88
100
|
return value;
|
|
89
101
|
},
|
|
90
102
|
}), {
|
|
91
|
-
|
|
103
|
+
get: {
|
|
92
104
|
configurable: true,
|
|
93
|
-
value: (...args) => this.#
|
|
105
|
+
value: (...args) => this.#configClient.get(...args),
|
|
106
|
+
},
|
|
107
|
+
set: {
|
|
108
|
+
configurable: true,
|
|
109
|
+
value: (...args) => this.#configClient.set(...args),
|
|
110
|
+
},
|
|
111
|
+
del: {
|
|
112
|
+
configurable: true,
|
|
113
|
+
value: (...args) => this.#configClient.del(...args),
|
|
94
114
|
},
|
|
95
115
|
run: {
|
|
96
116
|
configurable: true,
|
|
97
117
|
value: (...args) => this.#run(...args),
|
|
98
118
|
},
|
|
119
|
+
[cmdNotFound]: {
|
|
120
|
+
configurable: true,
|
|
121
|
+
value: key => this.emit('error', { key, error: 'Command Not Found' }),
|
|
122
|
+
},
|
|
99
123
|
resolve: {
|
|
100
124
|
configurable: true,
|
|
101
125
|
value: (...args) => {
|