-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_running.js
86 lines (75 loc) · 2.8 KB
/
example_running.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
'use strict';
const terminate = require('terminate');
const availableCommands = {
'k': {
run: function(pid) {
if (!pid || !/^\d+$/.test(pid)) {
console.error(`Wrong argument pid for command k - kill`);
return false;
}
let signal = 'SIGKILL';
if (arguments[1] && /^[a-zA-Z]+$/.test(pid)) {
signal = arguments[1];
}
terminate(pid, signal, err => {
if (err) {
console.error(err);
} else {
console.log(`Command KILL for process "${ pid }" executed with signal [${ signal }]!`);
}
});
}
},
'exit': {
run: function() {
const signal = 'SIGINT';
console.log(`Command EXIT for process "${ process.pid }" will be executed with signal [${ signal }]!`);
terminate(process.pid, 'SIGINT', err => {
if (err) {
console.error(err);
}
});
}
}
};
// adding help for each commands, symbol "<>" needed as separator between command and text explanation (for pretty input)
availableCommands.k.usage = 'k [PID, [SIGNAL]] <> kill process by its PID';
availableCommands.exit.usage = 'exit <> stop watching for commands and exit script';
const execConsole = require('./index')(availableCommands);
// adding new key "Cntr + q"
execConsole.keys['\u0011'] = (controls, commands) => {
console.log('This is handler for Cntrl + q! Another exit action!');
};
// adding move cursor left for two position key "{"
execConsole.keys['\u007B'] = (controls, commands) => {
if (controls.cursorPosition > 1) {
controls.cursorPosition = controls.cursorPosition - 2;
execConsole.controls.stdout.cursorTo(controls.cursorPosition);
}
};
// rebind standard handler "handleCombineActionsForEnterKeyAction"
const savedAction = execConsole.actions.handleCombineActionsForEnterKeyAction;
execConsole.actions.handleCombineActionsForEnterKeyAction = function() {
console.log(`\r\n\r\nThis is a logger! From rebind standard handler "handleCombineActionsForEnterKeyAction"!`);
savedAction(execConsole.controls, execConsole.commands);
};
// run script
execConsole();
// and even add a new command after script run!
execConsole.commands['n'] = {
run: function() {
console.log('I\'m a new command added after running script!');
},
usage: 'n <> just a new added command!'
};
console.log(`
This is a demo!
Available commands:
- help
- k
- exit
You can use "tab" for autocomplete name folders and files, "backspace", move cursor position by
arrow keys "left" and "right", view history of typed commands by using arrow keys "up" and "down"
and much more...
Let's go! Type command!
`);