3

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.

1 Answer 1

5
+50

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 v4.3.2):

'use strict';

var fs = require('fs');
var child_process = require('child_process');
var file = '/tmp/node-editor-test';

fs.writeFile(file, "Node Editor", function () {
  var child = child_process.spawn('vim', [file], {stdio: 'inherit'});

  child.on('close', function () {
    fs.readFile(file, 'utf8', function (err, text) {
      console.log('File content is now', text);
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.