I'm working on a Node.js CLI script that, as part of its duties, will sometimes need to take a large block of input text from the user. Right now, I'm just using the very basic readline.prompt(). I'd like some better editing capabilities. Rather than reinvent the wheel, I figure I could just do as crontab -e or visudo do and have the script launch a text editor that writes data to a temporary file, and read from that file once it exits. I've tried some things from the child_process library, but they all launch applications in the background, and don't give them control of stdin or the cursor. In my case, I need an application like vim or nano to take up the entire console while running. Is there a way to do this, or am I out of luck?
Note: This is for an internal project that will run on a single machine and whose source will likely never see the light of day. Hackish workarounds are welcome, assuming there's not an existing package to do what I need.
Have you set the stdio option of child_process.spawn to inherit?
This way, the child process will use same stdin and stdout as the top node process.
This code works for me (node 0.8.16):
var fs = require('fs'),
child_process = require('child_process');
file = '/tmp/node-editor-test';
fs.writeFileSync(file, "Node Editor");
var child = child_process.spawn('vim', [file], {stdio: 'inherit'});
child.on('exit', function () {
console.log('File content is now', fs.readFileSync(file, 'utf8'));
});