replico

1.0.1 • Public • Published

replico NPM version

A REPL with support for await

  • Track Promise results
  • Support for await keyword (expl: user = await db.User.get('foobar') will populate user variable once promise is resolved)

Installation

# For the command line "replico" 
npm install --global replico
 
# And/or to use replico api in a project 
npm install --save replico

Command line usage

replico

Api usage

Basic replacement of repl (see https://nodejs.org/dist/latest-v5.x/docs/api/repl.html#repl_class_replserver)

var replico = require('replico')
 
let replServer = replico({
  prompt: `${CBLUE}\u2234 ${CRESET}`,
  terminal: true
})

Define your own eval

var replico = require('replico')
 
let replServer = replico({
  prompt: `MyRepl> `,
  terminal: true
})
 
replServer.eval = function (cmd, context, filename, callback) {
  if (cmd === 'foo') {
    // ... your own implementation
    return callback(null, 'bar')
  } else if (/yield/.test(cmd)) {
    // Or use replico's eval (with bluebird-co):
    return replico.coEval.call(this, cmd, context, filename, callback)
  } else {
    // If you need it, superEval is a reference to the default repl eval
    return this.superEval(cmd, context, filename, callback)
  }
}

Plug it in your project

'use strict'
const net = require('net')
const replico = require('replico')
 
const CRESET = '\x1b[0m'
const CBLUE = '\x1b[34m'
 
var history = ['console.log("ok")']
 
net.createServer((socket) => {
  socket.write(`${CBLUE}Welcome${CRESET}\n`)
 
  function extendContext (context) {
    context.actions = {
      hello (who) {
        socket.write('OK ' + who + '\n')
      }
    }
  }
 
  let replServer = replico({
    prompt: `${CBLUE}\u2234 ${CRESET}`,
    input: socket,
    output: socket,
    terminal: true,
    history: history
  })
 
  replServer.on('exit', () => {
    history = replServer.history.slice()
    socket.end()
  })
 
  replServer.on('reset', extendContext)
 
  replServer.defineCommand('chist', {
    help: 'Clear history',
    action: function () {
      history = []
      replServer.history = []
      this.displayPrompt()
    }
  })
 
  extendContext(replServer.context)
}).listen(5423, function () {
  console.log('Ready on 5423')
})
 

Then you can use netcat or repli:

nc localhost 5423
# Or repli 
repli localhost 5423

The MIT License • By Novadiscovery

/replico/

    Package Sidebar

    Install

    npm i replico

    Weekly Downloads

    0

    Version

    1.0.1

    License

    none

    Unpacked Size

    11.2 kB

    Total Files

    12

    Last publish

    Collaborators

    • jponchon