@adonisjs/repl

REPL for AdonisJS

Usage no npm install needed!

<script type="module">
  import adonisjsRepl from 'https://cdn.skypack.dev/@adonisjs/repl';
</script>

README

AdonisJS REPL

A slick framework agnostic REPL for Node.js with first class support for
top level await, typescript compilation, accurate stack traces and a lot more.


gh-workflow-image npm-image license-image synk-image

Built with ❤︎ by Harminder Virk


AdonisJS REPL is a standalone and framework agnostic package to create custom Node.js REPL with first class support for:

👉 Execute typescript code with in-memory compilation.
👉 Support for top level await keyword.
👉 Ability to define custom method with a help description.

Table of contents

Installation

Install the package from the npm registry as follows:

npm i @adonisjs/repl

# Yarn
yarn add @adonisjs/repl

Usage

Import the Repl class from the standalone module.

import { Repl } from '@adonisjs/repl/build/standalone'
const repl = new Repl()

repl.start()

Typescript support

You will have to make use of @adonisjs/require-ts in order for the REPL to compile and run the typescript code. For example:

import { loadCompiler } from '@adonisjs/require-ts'
import { Repl } from '@adonisjs/repl/build/standalone'

const compilerOptions = {
  target: 'es2019',
  module: 'commonjs',
  allowSyntheticDefaultImports: true,
  esModuleInterop: true,
}

const repl = new Repl(loadCompiler(compilerOptions))

If you are using @adonisjs/require-ts as a require hook, then there is no need to instantiate another instance of the compiler as you can reference the compiler instance from the global object.

const compiler = global[Symbol.for('REQUIRE_TS_COMPILER')]
const repl = new Repl(compiler)

And now run the file containing the above code as follows:

node -r @adonisjs/require-ts/build/register repl.ts

History file

AdonisJS REPL allows you store the commands history inside a file so that the subsequent sessions can reference the commands executed in an earlier session.

You need to just pass the path to the history file and rest is taken care for you.

import { join } from 'path'
import { homedir } from 'os'
import { Repl } from '@adonisjs/repl/build/standalone'

const repl = new Repl(compiler, join(homedir(), '.adonis_repl_history'))

repl.start()

Accurate Stack Trace

The stack trace for the Typescript files points back to the correct file, line and the column number.

The .ls command

The .ls command prints the REPL session context. The output is divided to two sections.

  • Global Methods are the methods in the repl context object, but has some description associated with them.
  • Context properties: are the properties/methods in the context object. Only the first level of properties are printed on the console (to avoid noisy output).

Adding custom properties

If you are aware about the Node.js repl context, then you would know that you can add properties to the context as follows:

// NODE.JS EXAMPLE
const { start } = require('repl')

const server = start({})
server.context.foo = 'bar'

Similarly, you can add properties to the AdonisJS repl context by referencing the underlying server property.

import { Repl } from '@adonisjs/repl/build/standalone'

const repl = new Repl().start()
repl.server.context.foo = 'bar'

Global methods

In addition to adding properties to the context directly. You can also define custom methods with a description and its usage text. For example:

import { Repl } from '@adonisjs/repl/build/standalone'
const repl = new Repl()

repl.addMethod(
  'getUsers',
  () => {
    return [
      { id: 1, name: 'virk' },
      { id: 2, name: 'romain' },
    ]
  },
  {
    description: 'Returns a list of users',
  }
)

repl.start()

There is no technical advantage for using addMethod over adding properties to the context directly. It's just that addMethod properties are given special treatment during the .ls command.

Checkout the following example