Tuesday 14 February 2017

readline module does not work with console log in node-repl

i was working on a project few days ago, it worked properly. Two days later i came back to the code and it does not work again. After trying to find the bug i discovered that it was how i use the module.'use strict'; const fs = require('fs'); const path = require('path'); const rl = require('readline'); const cp = require('child_process').execSync; const Writable = require('stream').Writable; const vm = require('vm'); const util = require('util'); class Appendable extends Writable { constructor(source,options) { super(options); this.source = () => source; } _write(chunk, encoding, callback) { callback(fs.appendFileSync(this.source(),chunk)); } } class History { constructor() { let nodeHistFile = path.join(process.env.HOME, ".node_repl_history"); let nFile = ( fs.existsSync(nodeHistFile) ) ? nodeHistFile : process.env.NODE_REPL_HISTORY; this.histFile = () => nFile; let rd = rl.createInterface({ input: fs.createReadStream(this.histFile()) }); this.histSize = () => process.env.NODE_REPL_HISTORY_SIZE || 1000; this.rd = () => rd; } static ShowContent(histSize,rd) { let i = 0; rd.on('line', line => { i = ++i; console.log(` ${i} ${line}\n`); if ( Number(histSize) === i ) { rd.close(); } }); } history() { History.ShowContent(this.histSize(), this.rd()); } info(num = this.histSize()) { if ( (typeof num) !== 'number' ) { throw new TypeError(`expected a type of number as the first argument but got type of ${typeof(num)}`); } if ( num === this.histSize() ) { History.ShowContent(this.histSize(), this.rd()); return ; } let p = []; this.rd().on('line', line => { p.unshift(line); }); this.rd().on('close', () => { for ( let i = 1; i <= num ; ++i ) { let histNum = this.histSize() - i; if ( histNum ) { console.log(` ${histNum} ${p[i - 1]}\n`); continue; } } }); } add(histEvent,exec = false) { if ( arguments.length === 0 ) { throw new Error(`add method requres at least 1 argument which should be an Event to add to the history`); } { fs.writeFileSync(this.histFile() + ".bak", fs.readFileSync(this.histFile())); fs.writeFileSync(this.histFile(), histEvent + "\n"); let f = new Appendable(this.histFile()); fs.createReadStream(this.histFile() + ".bak").pipe(f); } if ( exec ) { const code = new vm.Script(histEvent); const context = vm.createContext({}); code.runInContext(context); console.log(util.inspect(context)); } } } let hist = new History(); module.exports = hist; if i run it as a command line program it works perfectly, if i want to use it as a module it does not log out anything i.e whenever i do> const h = require('./history.js'); > h.info() i expect h.info() to log out all the content in history because i called console.log inside this.rd().onThe only method that prints to stdout is the add method.After deep taught i added the output property to rl.createInterface , but whenever i require the package ( without even calling any method ) it just prints out the history content to stdout. I want the info method and history method to print to stdout when i call them from node repl

Submitted February 15, 2017 at 02:27AM by 73mp74710n

No comments:

Post a Comment