@coderich/sandman 0.0.9 → 0.0.10
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 +2 -2
- package/src/ReadlineService.js +27 -39
- package/src/Sandman.js +3 -20
- package/src/UtilService.js +17 -0
package/package.json
CHANGED
package/src/ConfigClient.js
CHANGED
|
@@ -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,5 +1,6 @@
|
|
|
1
1
|
const EventEmitter = require('events');
|
|
2
2
|
const ReadlineService = require('./ReadlineService');
|
|
3
|
+
const UtilService = require('./UtilService');
|
|
3
4
|
const FetchService = require('./FetchService');
|
|
4
5
|
const ConfigClient = require('./ConfigClient');
|
|
5
6
|
|
|
@@ -12,16 +13,16 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
12
13
|
this.#configClient = new ConfigClient(configDir);
|
|
13
14
|
this.#createCLI();
|
|
14
15
|
this.#readline = ReadlineService.createInterface(this.#cli, this.#configClient);
|
|
16
|
+
this.#configClient.watch(configDir, event => this.emit('save', event));
|
|
15
17
|
|
|
16
18
|
this.#readline.on('line', async (line) => {
|
|
17
19
|
if (!line) return this.#prompt();
|
|
18
|
-
const [cmd, ...args] =
|
|
20
|
+
const [cmd, ...args] = UtilService.parseArgs(line.trim());
|
|
19
21
|
const info = this.#cli[cmd] ? { cmd, args } : { cmd: 'run', args: [cmd, ...args] };
|
|
20
22
|
const value = await Promise.resolve(this.#cli[info.cmd](...info.args)).catch(e => e);
|
|
21
23
|
return this.emit(cmd, value);
|
|
22
24
|
});
|
|
23
25
|
|
|
24
|
-
this.#configClient.watch();
|
|
25
26
|
this.#prompt();
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -104,22 +105,4 @@ module.exports = class Sandman extends EventEmitter {
|
|
|
104
105
|
},
|
|
105
106
|
});
|
|
106
107
|
}
|
|
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
108
|
};
|
|
@@ -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
|
+
};
|