Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adonisjs/repl

Package Overview
Dependencies
Maintainers
2
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adonisjs/repl

REPL for AdonisJS

  • 3.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created
Source

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

Usage | Contributing | Checkout AdonisJS

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

Keywords

FAQs

Package last updated on 17 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc