@coderich/sandman 0.0.9 → 0.0.11
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 +3 -3
- package/src/ReadlineService.js +27 -39
- package/src/Sandman.js +38 -31
- package/src/UtilService.js +17 -0
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
|
@@ -42,7 +42,7 @@ module.exports = class ConfigClient extends Config {
|
|
|
42
42
|
return resolvedData;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
raw(key) {
|
|
45
|
+
raw(key = '') {
|
|
46
46
|
const data = Util.get(this.toObject().config, key);
|
|
47
47
|
return this.#mergeMergeData(key, data);
|
|
48
48
|
}
|
|
@@ -61,7 +61,7 @@ module.exports = class ConfigClient extends Config {
|
|
|
61
61
|
return this.merge(Config.parseDir(dir, (...args) => this.#ignore(...args)));
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
watch(dir = this.#configDir) {
|
|
64
|
+
watch(dir = this.#configDir, onSave) {
|
|
65
65
|
const watcher = Chokidar.watch(dir, {
|
|
66
66
|
awaitWriteFinish: true,
|
|
67
67
|
ignoreInitial: true,
|
|
@@ -75,7 +75,7 @@ module.exports = class ConfigClient extends Config {
|
|
|
75
75
|
const api = Config.parseFile(path);
|
|
76
76
|
if (key) this.set(key, api);
|
|
77
77
|
else this.merge(api); // index.yaml
|
|
78
|
-
|
|
78
|
+
if (api.request) onSave({ key, api });
|
|
79
79
|
} else if (['unlink', 'unlinkDir'].includes(event)) {
|
|
80
80
|
this.del(key);
|
|
81
81
|
}
|
package/src/ReadlineService.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
const Readline = require('readline');
|
|
2
2
|
const Util = require('@coderich/util');
|
|
3
|
+
const UtilService = require('./UtilService');
|
|
3
4
|
|
|
4
5
|
exports.createInterface = (cli, configClient) => {
|
|
5
6
|
const captureInfo = {
|
|
6
7
|
line: '',
|
|
7
|
-
|
|
8
|
+
lastArg: '',
|
|
8
9
|
candidates: [],
|
|
9
10
|
tabCounter: 0,
|
|
10
11
|
candidateIndex: -1,
|
|
@@ -16,53 +17,37 @@ exports.createInterface = (cli, configClient) => {
|
|
|
16
17
|
output: process.stdout,
|
|
17
18
|
terminal: true,
|
|
18
19
|
completer: (line) => {
|
|
20
|
+
const [cmd, ...args] = UtilService.parseArgs(line);
|
|
21
|
+
|
|
19
22
|
// Show available CLI commands
|
|
20
|
-
if (!
|
|
23
|
+
if (!cmd) return [Object.keys(cli), line];
|
|
24
|
+
if (line.split(' ').length < 2) return [Object.keys(cli).filter(key => key.toLowerCase().startsWith(cmd.toLowerCase())), line];
|
|
21
25
|
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
+
const lastArg = args.at(-1) || '';
|
|
27
|
+
const lastPaths = lastArg.split('.');
|
|
28
|
+
const lastPath = lastPaths.at(-1); // current typing
|
|
29
|
+
const previousPath = lastPaths.slice(0, -1).join('.');
|
|
26
30
|
|
|
27
31
|
// Specific request.data selector
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const conf = Util.get(config, tokens.at(-2), {});
|
|
33
|
-
return [Object.keys(conf), path];
|
|
34
|
-
// const dataPath = ['request'].concat(paths.slice(1, -1)).join('.');
|
|
35
|
-
// const data = get(api, dataPath, {});
|
|
36
|
-
// return [Object.keys(data).filter(k => k.toLowerCase().startsWith(path.toLowerCase())), path];
|
|
32
|
+
if (lastArg.startsWith('.')) {
|
|
33
|
+
const requestAttrPath = `${args.at(-2)}.request${previousPath}`;
|
|
34
|
+
const requestAttrObj = configClient.get(requestAttrPath, {});
|
|
35
|
+
return [Object.keys(requestAttrObj).filter(key => key.toLowerCase().startsWith(lastPath.toLowerCase())), lastPath];
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
//
|
|
40
39
|
const flatKeys = Object.keys(Util.flatten(configClient.get()));
|
|
40
|
+
const requestKeys = flatKeys.map(k => k.substring(0, Math.max(k.indexOf('.request'), 0))).filter(Boolean);
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}); // .map(p => p.split('.').at(-1)); // Here!
|
|
48
|
-
|
|
49
|
-
// These are shortcut keys to requests
|
|
50
|
-
const requestKeyCandidates = Array.from(new Set(flatKeys.map((flatKey) => {
|
|
51
|
-
const keys = flatKey.split('.');
|
|
52
|
-
const index = keys.indexOf('request');
|
|
53
|
-
return index && flatKey.split('.').slice(0, index).join('.');
|
|
54
|
-
// const typedPath = keys.slice(0, paths.length - 1).join('.');
|
|
55
|
-
// const autocompletePath = keys.slice(paths.length - 1, index).join('.');
|
|
56
|
-
// return index > 0 && lastToken.toLowerCase().startsWith(typedPath.toLowerCase()) && autocompletePath;
|
|
57
|
-
}).filter(Boolean))).filter((c) => {
|
|
58
|
-
return c.toLowerCase().includes(lastToken.toLowerCase());
|
|
59
|
-
// return c.toLowerCase().includes(path.toLowerCase());
|
|
60
|
-
});
|
|
42
|
+
const candidates = Array.from(new Set(requestKeys
|
|
43
|
+
.filter(requestKey => requestKey.toLowerCase().startsWith(previousPath.toLowerCase()))
|
|
44
|
+
.filter(requestKey => requestKey.split('.').slice(lastPaths.length - 1).join('.').toLowerCase().includes(lastPath.toLowerCase()))
|
|
45
|
+
// .concat(Object.keys(cli).filter(key => key.toLowerCase().startsWith(cmd.toLowerCase())))
|
|
46
|
+
));
|
|
61
47
|
|
|
62
|
-
|
|
63
|
-
if (captureInfo.
|
|
64
|
-
|
|
65
|
-
return [candidates, lastToken];
|
|
48
|
+
if (captureInfo.captureCandidates && lastArg) { captureInfo.candidates = candidates; captureInfo.line = line; captureInfo.lastArg = lastArg; captureInfo.candidateIndex = -1; }
|
|
49
|
+
if (captureInfo.tabbing || (candidates.length === 1 && candidates[0] === lastArg)) return [[], lastArg];
|
|
50
|
+
return [candidates, lastArg];
|
|
66
51
|
},
|
|
67
52
|
});
|
|
68
53
|
|
|
@@ -76,11 +61,14 @@ exports.createInterface = (cli, configClient) => {
|
|
|
76
61
|
Readline.clearLine(process.stdout, 0);
|
|
77
62
|
readline.prompt();
|
|
78
63
|
} else if (captureInfo.tabCounter > 2 && captureInfo.candidates.length) {
|
|
64
|
+
captureInfo.tabbing = true;
|
|
79
65
|
const candidate = captureInfo.candidates[captureInfo.candidateIndex = ++captureInfo.candidateIndex % captureInfo.candidates.length];
|
|
80
|
-
const value = captureInfo.line.replace(captureInfo.
|
|
66
|
+
const value = captureInfo.line.replace(captureInfo.lastArg, candidate);
|
|
81
67
|
readline.line = value;
|
|
82
68
|
readline.cursor = value.length;
|
|
83
69
|
readline.prompt(true);
|
|
70
|
+
} else {
|
|
71
|
+
captureInfo.tabbing = false;
|
|
84
72
|
}
|
|
85
73
|
});
|
|
86
74
|
|
package/src/Sandman.js
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
|
+
const Path = require('path');
|
|
1
2
|
const EventEmitter = require('events');
|
|
3
|
+
const { spawn } = require('child_process');
|
|
2
4
|
const ReadlineService = require('./ReadlineService');
|
|
5
|
+
const UtilService = require('./UtilService');
|
|
3
6
|
const FetchService = require('./FetchService');
|
|
4
7
|
const ConfigClient = require('./ConfigClient');
|
|
5
8
|
|
|
9
|
+
const cmdNotFound = Symbol('cmdNotFound');
|
|
10
|
+
|
|
6
11
|
module.exports = class Sandman extends EventEmitter {
|
|
7
|
-
#configClient; #options; #readline; #cli; #cliCounter = 0;
|
|
12
|
+
#configClient; #options; #readline; #cli; #cliCounter = 0; #configDir;
|
|
8
13
|
|
|
9
14
|
constructor(configDir, options) {
|
|
10
15
|
super();
|
|
11
16
|
this.#options = options;
|
|
12
|
-
this.#
|
|
17
|
+
this.#configDir = configDir;
|
|
18
|
+
this.#configClient = new ConfigClient(this.#configDir);
|
|
13
19
|
this.#createCLI();
|
|
14
20
|
this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
|
|
21
|
+
this.#configClient.watch(this.#configDir, event => this.emit('save', event));
|
|
15
22
|
|
|
16
23
|
this.#readline.on('line', async (line) => {
|
|
17
24
|
if (!line) return this.#prompt();
|
|
18
|
-
const [cmd, ...args] =
|
|
19
|
-
const
|
|
20
|
-
const value = await Promise.resolve(this.#cli[
|
|
21
|
-
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);
|
|
22
29
|
});
|
|
23
30
|
|
|
24
|
-
this.#configClient.watch();
|
|
25
31
|
this.#prompt();
|
|
26
32
|
}
|
|
27
33
|
|
|
@@ -62,10 +68,17 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
62
68
|
const self = this;
|
|
63
69
|
|
|
64
70
|
this.#cli = Object.defineProperties(new Proxy({
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
+
show: key => ({
|
|
79
|
+
$: this.#configClient.raw(key),
|
|
80
|
+
[key]: this.#configClient.get(key),
|
|
81
|
+
}),
|
|
69
82
|
curl: key => FetchService.toCURL(this.#configClient.get(key, {}).request),
|
|
70
83
|
quit: () => process.exit(),
|
|
71
84
|
}, {
|
|
@@ -87,14 +100,26 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
87
100
|
return value;
|
|
88
101
|
},
|
|
89
102
|
}), {
|
|
90
|
-
|
|
103
|
+
get: {
|
|
91
104
|
configurable: true,
|
|
92
|
-
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),
|
|
93
114
|
},
|
|
94
115
|
run: {
|
|
95
116
|
configurable: true,
|
|
96
117
|
value: (...args) => this.#run(...args),
|
|
97
118
|
},
|
|
119
|
+
[cmdNotFound]: {
|
|
120
|
+
configurable: true,
|
|
121
|
+
value: key => this.emit('error', { key, error: 'Command Not Found' }),
|
|
122
|
+
},
|
|
98
123
|
resolve: {
|
|
99
124
|
configurable: true,
|
|
100
125
|
value: (...args) => {
|
|
@@ -104,22 +129,4 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
104
129
|
},
|
|
105
130
|
});
|
|
106
131
|
}
|
|
107
|
-
|
|
108
|
-
static parseArgs(line) {
|
|
109
|
-
const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
110
|
-
const args = [];
|
|
111
|
-
let match;
|
|
112
|
-
|
|
113
|
-
while ((match = regex.exec(line)) !== null) {
|
|
114
|
-
if (match[1] !== undefined) {
|
|
115
|
-
args.push(match[1]); // double-quoted
|
|
116
|
-
} else if (match[2] !== undefined) {
|
|
117
|
-
args.push(match[2]); // single-quoted
|
|
118
|
-
} else if (match[3] !== undefined) {
|
|
119
|
-
args.push(match[3]); // bare word
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return args;
|
|
124
|
-
}
|
|
125
132
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
exports.parseArgs = (line) => {
|
|
2
|
+
const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
3
|
+
const args = [];
|
|
4
|
+
let match;
|
|
5
|
+
|
|
6
|
+
while ((match = regex.exec(line)) !== null) {
|
|
7
|
+
if (match[1] !== undefined) {
|
|
8
|
+
args.push(match[1]); // double-quoted
|
|
9
|
+
} else if (match[2] !== undefined) {
|
|
10
|
+
args.push(match[2]); // single-quoted
|
|
11
|
+
} else if (match[3] !== undefined) {
|
|
12
|
+
args.push(match[3]); // bare word
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return args;
|
|
17
|
+
};
|