Review:
This is a script that allows you wrote command and execute it while this script will be listening stdin! You can add any handlers for any keys and any commands for run as written below. In js console you can use "tab" for auto complete names 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... Enjoy!
Run demo with:
npm run demo
Why?
Exists situations, especially in development, when you need send to running process some control commands, for example for restart build task (like "rs" in nodemon https://www.npmjs.com/package/nodemon) or something good else. What exactly? You can add every command that you want!
Requirements:
- Node 4+
- Npm 2+
Install:
npm i --save-dev js_console_command_executor
Usage:
First of all you need define your commands:
const availableCommands = 'k': { console; // some logic } 'exit': { console; // some logic } ;
Add help for each commands, symbol "<>" needed as separator between command and text explanation (for pretty input):
availableCommandskusage = 'k [PID, [SIGNAL]] <> kill process by its PID';availableCommandsexitusage = 'exit <> stop watching for commands and exit script';
require script:
const execConsole = availableCommands;
Now you can access for this objects:
* execConsolekeys // objects with defined keys and handlers;* execConsoleactions // object with all standard functions such as executeCommand and etc;* execConsolecommands // your object with commands;* execConsolecontrols // object with state of line buffer, cursor position etc;
Optionally you can add new key handler. Adding new handler for "Ctrl + q":
execConsolekeys'\u0011' = { console;};
And this we're adding action move cursor left for two position for key "{" => "Shift + [":
execConsolekeys'\u007B' = { if controlscursorPosition > 1 controlscursorPosition = controlscursorPosition - 2; processstdout; };
Optionally you can rebind standard handler. Rebind logic for "handleCombineActionsForEnterKeyAction":
execConsoleactions { console; execConsoleactions; execConsoleactions;};
run script:
;
and you can even add a new command after script run (optional)!
execConsolecommands'n' = { console; } usage: 'n <> just a new added command!';
Try use it!
For demo run:
npm run demo
For tests run:
npm run test
For code coverage run:
npm run coverage