I want to build a tiny script that being run should create a bash-like session (in the current bash session, where the process is created), that can be used later for some mad science (e.g. piping to browser).
我想构建一个运行的小脚本应该创建一个类似bash的会话(在当前的bash会话中,创建进程),以后可以用于某些疯狂的科学(例如管道到浏览器)。
I tried using pty.js, piping stdin
to the bash
process, and data from the bash session to the stdout
stream:
我尝试使用pty.js,管道stdin到bash进程,以及从bash会话到stdout流的数据:
var pty = require("pty.js");
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: ".",
env: process.env
});
term.pipe(process.stdout);
process.stdin.pipe(term);
term.on("close", function () {
process.exit();
});
This works, but it's very buggy:
这有效,但它非常错误:
For example, the non-characters (directional keys, tab etc) are not caught.
例如,未捕获非字符(方向键,选项卡等)。
I also tried using spawn
, this not being so bad, but still buggy.
我也试过使用spawn,这不是那么糟糕,但仍然是马车。
var spawn = require("child_process").spawn;
var bash = spawn("bash");
bash.stdout.pipe(process.stdout);
process.stdin.pipe(bash.stdin);
Is there a better solution how to create a bash wrapper in NodeJS?
有没有更好的解决方案如何在NodeJS中创建bash包装器?
1 个解决方案
#1
4
You might want to put the standard input into raw mode. This way all the key strokes will be reported as a data event. Otherwise you only get lines (for every press of the return
key).
您可能希望将标准输入置于原始模式。这样,所有击键都将被报告为数据事件。否则你只能获得一行(每按一次返回键)。
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
var pty = require('pty.js');
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: '.',
env: process.env
});
term.pipe(process.stdout);
process.stdin.pipe(term);
term.on('close', function () {
process.exit();
});
Maybe you also find some more experience from other in this related question.
也许你也在这个相关问题中找到了其他人的更多经验。
PS: Vim runs now much better :P
PS:Vim现在运行得更好:P
#1
4
You might want to put the standard input into raw mode. This way all the key strokes will be reported as a data event. Otherwise you only get lines (for every press of the return
key).
您可能希望将标准输入置于原始模式。这样,所有击键都将被报告为数据事件。否则你只能获得一行(每按一次返回键)。
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
var pty = require('pty.js');
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: '.',
env: process.env
});
term.pipe(process.stdout);
process.stdin.pipe(term);
term.on('close', function () {
process.exit();
});
Maybe you also find some more experience from other in this related question.
也许你也在这个相关问题中找到了其他人的更多经验。
PS: Vim runs now much better :P
PS:Vim现在运行得更好:P