summaryrefslogtreecommitdiff
path: root/server/console.ts
blob: 485789a5a308b06820e9a6d9c1bae97203b91c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const targetModule = require(`${__dirname}/app.module`);
    const applicationContext = await NestFactory.createApplicationContext(
      // tslint:disable-next-line: no-string-literal
      targetModule['AppModule'],
    );
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    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: any): string {
  return Logger.stringify(value, LOGGER_OPTIONS);
}

const session = new InteractiveNestJS();
session.run();