import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

import { db } from './db';
import { initDB } from './init';

import { PubSub } from 'graphql-subscriptions';
import {NestExpressApplication} from '@nestjs/platform-express'

export const pubsub = new PubSub();

async function bootstrap(argv: string[]) {
  await db.connect();
  await initDB(argv.findIndex(a => a.toLowerCase() === 'init') !== -1, argv.findIndex(a => a.toLowerCase() === 'reset') !== -1);

  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  await app.listen(3000);
}

let args: string[];
if (process.env.npm_config_argv) {
  args = JSON.parse(process.env.npm_config_argv || '').remain || [];
} else {
  args = process.argv.slice(2);
}

process.on('uncaughtException', (error) => {
  // tslint:disable-next-line:no-console
  console.error(error);
});

bootstrap(args).then();
