-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
302 lines (283 loc) · 12.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict';
const fs = require('fs');
const path = require('path');
const terminate = require('terminate');
const parse = require('shell-quote').parse;
const IS_WINDOWS = /^win/.test(process.platform);
const FILENAME = `js-console-command-executor: pid[${ process.pid }]`;
const controls = {
stdin: process.stdin,
stdout: process.stdout,
buffer: '',
commandLine: '',
cursorPosition: 0,
typedCommands: [],
typedCommandsPointer: -1,
separatorOfAutoCompletedList: ' '
};
const actions = {
doExit: function (controls, commands, key) {
process.exit(0);
},
doHardExit: function (controls, commands, key) {
terminate(process.pid, 'SIGKILL', err => {
if (err) {
controls.stdout.write(`${ FILENAME } ERROR: ` + "\r\n" + err + "\r\n");
}
});
},
moveCursorToRight: function (controls, commands, key) {
if (controls.cursorPosition < controls.buffer.length) {
controls.cursorPosition++;
controls.stdout.cursorTo(controls.cursorPosition);
}
},
moveCursorToLeft: function (controls, commands, key) {
if (controls.cursorPosition > 0) {
controls.cursorPosition--;
controls.stdout.cursorTo(controls.cursorPosition);
}
},
goUpToCommandsHistory: function (controls, commands, key) {
if (controls.typedCommandsPointer >= 0) {
controls.stdout.clearLine(); // clear current text
controls.stdout.cursorTo(0);
controls.stdout.write(controls.typedCommands[controls.typedCommandsPointer]);
controls.buffer = controls.typedCommands[controls.typedCommandsPointer];
controls.typedCommandsPointer--;
controls.cursorPosition = controls.buffer.length;
} else {
actions.clearStdOut(controls, commands, key);
}
},
goDownToCommandsHistory: function (controls, commands, key) {
if (controls.typedCommandsPointer < controls.typedCommands.length - 1) {
controls.typedCommandsPointer++;
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
controls.stdout.write(controls.typedCommands[controls.typedCommandsPointer]);
controls.buffer = controls.typedCommands[controls.typedCommandsPointer];
controls.cursorPosition = controls.buffer.length;
} else {
actions.clearStdOut(controls, commands, key);
}
},
handleEnterKeyAction: function (controls, commands, key) {
controls.stdout.write('\r\n');
controls.commandLine = controls.buffer.trim().replace(/[\s\t]+/g, ' ');
controls.buffer = '';
controls.cursorPosition = 0;
controls.typedCommands.push(controls.commandLine);
controls.typedCommandsPointer = controls.typedCommands.length - 1;
},
handleBackSpaceKeyAction: function (controls, commands, key) {
if (controls.cursorPosition < 1) {
controls.cursorPosition = 0;
return;
}
controls.cursorPosition--;
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
let first = controls.buffer.substring(0, controls.cursorPosition);
let second = controls.buffer.substring(controls.cursorPosition + 1);
controls.buffer = first + second;
controls.stdout.write(controls.buffer);
controls.stdout.cursorTo(controls.cursorPosition);
},
handleHelpCommand: function(controls, commands, key) {
let commandsList = [];
let maxLongString = 0;
for (let key in commands) {
if (commands[key].usage) {
let strLine = commands[key].usage.split("<>");
if (strLine[0].length > maxLongString) {
maxLongString = strLine[0].length;
}
}
}
for (let key in commands) {
if (commands[key].usage) {
let strLine = commands[key].usage.split('<>');
let textLine = strLine[0] + ' '.repeat(maxLongString - strLine[0].length) + ' '.repeat(3);
if (strLine[1] && '' !== strLine[1]) {
textLine = textLine + strLine[1];
}
commandsList.push(` ${ textLine } \r\n`);
} else {
commandsList.push(` Not declared help for command "${ key }" in object key "usage"! \r\n`);
}
}
controls.stdout.write(`${ FILENAME }: Available commands are: \r\n\r\n${ commandsList.join('') }` + "\r\n");
},
executeCommand: function (controls, commands, key) {
const commandChunks = parse(controls.commandLine);
const command = commandChunks[0];
if (commands[command]) {
commands[command].run.apply(this, commandChunks.slice(1));
} else if ('help' === command) {
actions.handleHelpCommand(controls, commands);
} else {
controls.stdout.write(`${ FILENAME }: Command "${ controls.commandLine }" not recognized! Use help command!` + "\r\n");
}
controls.commandLine = '';
},
testKeyForAvailableToStdout: function (key) {
// require('fs').writeFileSync('key.txt', actions.toUnicode(key));
return (
/^[-\w'"\\/\[\]\.{},;<>|:?!@#%\^&\$*\(\)+=~`]+$/.test(key)
|| '\u0020' === key // space
);
},
writeSymbolToStdout: function (controls, commands, key) {
if (actions.testKeyForAvailableToStdout(key)) {
if (controls.buffer.length !== controls.cursorPosition) {
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
let first = controls.buffer.substring(0, controls.cursorPosition );
let second = controls.buffer.substring(controls.cursorPosition);
controls.buffer = first + key + second;
controls.stdout.write(controls.buffer);
controls.cursorPosition++;
controls.stdout.cursorTo(controls.cursorPosition);
} else {
controls.buffer = controls.buffer + key;
controls.cursorPosition = controls.buffer.length;
controls.stdout.write(key);
}
}
},
handleCombineActionsForEnterKeyAction: function(controls, commands, key) {
actions.handleEnterKeyAction(controls, commands, key);
actions.executeCommand(controls, commands, key);
},
toUnicode: function (theString) {
let unicodeString = '';
for (let i = 0; i < theString.length; i++) {
let theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
while (theUnicode.length < 4) {
theUnicode = '0' + theUnicode;
}
theUnicode = '\\u' + theUnicode;
unicodeString += theUnicode;
}
return unicodeString;
},
keyHandler: function (key, keys, commands, controls) {
// console.log(actions.toUnicode(key));
if (keys[key] && typeof keys[key] === 'function') {
keys[key](controls, commands, key);
} else {
actions.writeSymbolToStdout(controls, commands, key);
}
},
clearStdOut: function(controls, commands, key) {
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
controls.buffer = '';
controls.cursorPosition = 0;
},
searchFolderByStdin: function(controls, commands, key) {
// if last buffer symbol isn't space or tab
if (/[\s\t"']+/.test(controls.buffer[controls.buffer.length - 1])) {
actions.showAutoCompletedListOfMatched(fs.readdirSync(__dirname));
return;
}
let matched = []
,parsedBuffer = parse(controls.buffer)
;
if (parsedBuffer.length < 1) {
actions.showAutoCompletedListOfMatched(fs.readdirSync(__dirname));
return;
}
let folders = parsedBuffer.pop().split('/');
if (folders[folders.length - 1] === '') { // do list of all folders in way
let foldersWay = folders[0];
for (let i = 1; i < folders.length - 1; i++) {
foldersWay = foldersWay + `/${ folders[i] }`;
}
let way = path.normalize(`${ process.env.PWD }/${ foldersWay }`);
if (!fs.existsSync(way)) return;
actions.showAutoCompletedListOfMatched(fs.readdirSync(way));
} else {
let folderName = folders[folders.length - 1]
,foldersWay = ''
;
for (let i = 0; i < folders.length - 1; i++) {
foldersWay = foldersWay + `/${ folders[i] }`;
}
let way = path.normalize(`${ process.env.PWD }/${ foldersWay }`);
if (!fs.existsSync(way)) return;
const folderContent = fs.readdirSync(way);
folderContent.forEach(name => {
let regex = new RegExp(`^${ folderName }`, "i");
if (regex.test(name)) {
if (fs.lstatSync(`${ process.env.PWD }/${ foldersWay }/${ name }`).isDirectory()) {
matched.push(`${ name }/`);
} else {
matched.push(name);
}
}
});
if (matched.length < 1) return;
if (matched.length === 1) {
controls.buffer = controls.buffer.replace(new RegExp(`${ folderName }$`, "i") , matched[0]);
controls.cursorPosition = controls.buffer.length;
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
controls.stdout.write(controls.buffer);
} else {
actions.showAutoCompletedListOfMatched(matched);
}
}
},
showAutoCompletedListOfMatched: function(listArr) {
actions.putInfoInStdOut(listArr.join(controls.separatorOfAutoCompletedList));
controls.stdout.write(controls.buffer);
controls.cursorPosition = controls.buffer.length;
},
putInfoInStdOut: function(str) {
let stdoutLine = `\r\n${ str }\r\n\r\n`;
controls.stdout.clearLine();
controls.stdout.cursorTo(0);
controls.stdout.write(stdoutLine);
return stdoutLine;
}
};
let execConsole = function() {
controls.stdin.setRawMode(true);
controls.stdin.resume();
controls.stdin.setEncoding('utf8');
controls.stdin.on('data', (key) => {
actions.keyHandler(key, execConsole.keys, execConsole.commands, execConsole.controls);
});
controls.stdout.write(`${ FILENAME }: watching for commands! type help for list of commands!` + "\r\n");
};
execConsole.keys = {};
execConsole.commands = {};
execConsole.actions = actions;
execConsole.controls = controls;
execConsole.keys['\u0003'] = (controls, commands, key) => actions.doExit(controls, commands, key); // Ctrl + C
execConsole.keys['\u001B'] = (controls, commands, key) => actions.clearStdOut(controls, commands, key); // Esc
if (IS_WINDOWS) {
execConsole.keys['\u0008'] = (controls, commands, key) => actions.handleBackSpaceKeyAction(controls, commands, key); // backspace
} else {
execConsole.keys['\u007F'] = (controls, commands, key) => actions.handleBackSpaceKeyAction(controls, commands, key); // backspace
}
execConsole.keys['\u0009'] = (controls, commands, key) => actions.searchFolderByStdin(controls, commands,key); // tab
execConsole.keys['\u000D'] = (controls, commands, key) => actions.handleCombineActionsForEnterKeyAction(controls, commands, key); // enter
execConsole.keys['\u001B\u005B\u0041'] = (controls, commands, key) => actions.goUpToCommandsHistory(controls, commands, key); // up
execConsole.keys['\u001B\u005B\u0042'] = (controls, commands, key) => actions.goDownToCommandsHistory(controls, commands, key); // down
execConsole.keys['\u001B\u005B\u0044'] = (controls, commands, key) => actions.moveCursorToLeft(controls, commands, key); // left
execConsole.keys['\u001B\u005B\u0043'] = (controls, commands, key) => actions.moveCursorToRight(controls, commands,key); // right
module.exports = (commands) => {
let isExistCommand = false;
for (let command in commands) {
isExistCommand = true;
}
if (!isExistCommand) {
console.log(`${ FILENAME }: Object of given commands is empty!`); actions.doExit(); return {};
} else {
execConsole.commands = commands;
return execConsole;
}
};