📜 ⬆️ ⬇️

REPL - setting for saving history and loading modules by default

The note is designed for beginners and mid-level users, so all the gurus at once please do not waste your precious time and skip this post.

In this short note, I describe how to set up a REPL (read-eval-print loop) or Node JS “console” without losing a nice bun — preserving history. The latest versions of Node automatically save the history between sessions at the REPL in the ~ / .node_repl_history file. But there is one catch, if you want to configure the REPL “for yourself”, then the session history is automatically saved.

Previously, a separate rlwrap package (ReadLine Wrap) was used to save history.
Which allows for example to decorate the console promt, but when using rlwrap, the autocomple of commands on the Tab key stops working. For this I will not use it.
')
In addition to the context of the REPL immediately load frequently used modules, such as axios and lodash.

Sometimes an underscore is used for lodash or underscore modules.
You should not do this in the REPL, since this symbol has a special meaning — the result of a previous operation.

By default, if you enter

let a=1; 

or any other code as a result of which nothing is returned or, more correctly, undefined will return, in the console this is the most undefined, which is annoying for me.
The parameter is responsible for this behavior.
ignoreUndefined: true

Another option: replMode: Repl.REPL_MODE_STRICT,. This is the equivalent of 'use strict'; . That is, now, for example, it will not be possible to assign a value to a variable without its declaration.

In other words

 b=2; 

will give an error and need to write

 let b=2; 

All REPL parameters are described on the Node website.

The rest of the code is intuitive.

 'use strict'; const Repl = require('repl'), fs = require('fs'), os = require('os'), path = require('path'), lo = require('lodash'), axios = require('axios'), sa = require('superagent'); const historyFile = path.resolve(os.homedir(), ".node_repl_history"); const repl = Repl.start({ prompt: 'node> ', useColors: true, replMode: Repl.REPL_MODE_STRICT, ignoreUndefined: true }); try { if (fs.existsSync(historyFile)) { fs.readFileSync(historyFile, 'utf-8') .split('\n') .reverse() .filter(line => line.trim()) .map(line => repl.history.push(line)); } } catch (err) { console.error(err); } repl.context.axios = axios; repl.context.fs = fs; repl.context.lo = lo; repl.context.os = os; repl.context.path = path; repl.context.sa = sa; repl.on('line', newLine =>{ const line = newLine.trim(); if (line) { fs.appendFileSync(historyFile, line + '\n'); } }); 


OUTDATED
In the previous version, the save occurred only when exiting the REPL.
 repl.on('exit', ()=>{ fs.appendFileSync(historyFile, repl.lines.join('\n')); }); 



Save this code for example in tools / repl.js

To run node tools / repl.js

You can write alias in .bashrc , but I prefer the script in package.json

  "scripts": { "repl": "node tools/repl " } 

And run yarn repl

Source: https://habr.com/ru/post/339576/


All Articles