Skip to content

Instantly share code, notes, and snippets.

@felipepodesta
Forked from royshouvik/console.ts
Created August 18, 2022 06:00
Show Gist options
  • Save felipepodesta/2faa7993dcd08ef78aa0f2758ed25e3f to your computer and use it in GitHub Desktop.
Save felipepodesta/2faa7993dcd08ef78aa0f2758ed25e3f to your computer and use it in GitHub Desktop.
Nestjs REPL
import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import * as repl from 'repl';
import * as Logger from 'purdy';
const LOGGER_OPTIONS = {
indent: 2,
depth: 1,
};
class InteractiveNestJS {
async run() {
// create the application context
const targetModule = require(`${__dirname}/app.module`);
const applicationContext = await NestFactory.createApplicationContext(
// tslint:disable-next-line: no-string-literal
targetModule['AppModule'],
);
const awaitOutside = require('await-outside');
// start node repl
const server = repl.start({
useColors: true,
prompt: '> ',
writer: replWriter,
ignoreUndefined: true,
});
server.context.app = applicationContext;
awaitOutside.addAwaitOutsideToReplServer(server);
}
}
function replWriter(value: object): string {
return Logger.stringify(value, LOGGER_OPTIONS);
}
const session = new InteractiveNestJS();
session.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment