Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readline output to file Node.js

How can I write output to file? I tried instead of process.stdout use fs.createWriteStream(temp + '/export2.json'), but it's not working.

var rl = readline.createInterface({
    input: fs.createReadStream(temp + '/export.json'),
    output: process.stdout,
    terminal: false
});

rl.on('line', function(line) {
    rl.write(line);
});
like image 526
noone Avatar asked Apr 02 '15 10:04

noone


2 Answers

Referring to node/readline

line 313:

Interface.prototype.write = function(d, key) {
    if (this.paused) this.resume();
    this.terminal ? this._ttyWrite(d, key) : this._normalWrite(d);
};

by calling rl.write() you either write to tty or call _normalWrite() whose definition follows the block.

Interface.prototype._normalWrite = function(b) {
  // some code 
  // .......

  if (newPartContainsEnding) {
    this._sawReturn = /\r$/.test(string);
    // got one or more newlines; process into "line" events
    var lines = string.split(lineEnding);
    // either '' or (concievably) the unfinished portion of the next line
    string = lines.pop();
    this._line_buffer = string;
    lines.forEach(function(line) {
      this._onLine(line);
    }, this);
  } else if (string) {
    // no newlines this time, save what we have for next time
    this._line_buffer = string;
  }
};

Output is written into _line_buffer.

line 96:

 function onend() {
    if (util.isString(self._line_buffer) && self._line_buffer.length > 0) {
      self.emit('line', self._line_buffer);
    }
    self.close();
 }

We find, _line_buffer is emitted to line event eventually. That's why you cannot write output to writeStream. To solve this problem, you can simply open a file using fs.openSync(), and fs.write() in rl.on('line', function(line){}) callback.

Sample code:

var rl = readline.createInterface({
    input: fs.createReadStream(temp + '/export.json'),
    output: process.stdout,
    terminal: false
});

fd = fs.openSync('filename', 'w');
rl.on('line', function(line) {
    fs.write(fd, line);
});
like image 73
Max Luan Avatar answered Oct 21 '22 19:10

Max Luan


readline does not write to file using with output option. However you can create a write stream as normal and write to it as usual.

Example:

const { EOL } = require("os");
const rl = readline.createInterface({
    input: fs.createReadStream(temp + '/export.json'),
});

const writeStream = fs.createWriteStream(temp + '/export2.json', { encoding: "utf8" })

rl.on('line', function(line) {
    writeStream.write(`${line}${EOL}`);
});
like image 22
ozm Avatar answered Oct 21 '22 21:10

ozm