Git Product home page Git Product logo

adonisjs-repl's Introduction

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

adonisjs-repl's People

Contributors

julien-r44 avatar snyk-bot avatar thetutlage avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.