

import Console from '@gem-mine/console'
import Koa = require('koa')
import fs = require('fs')
import { fatal } from './util/logger'
import { ErrnoException } from './@types/exception'
import config from './config'

if (config.IS_LOCAL) {
  const pid = process.pid
  const file = `${config.STORAGE_DIR}/.pid`
  if (fs.existsSync(file)) {
    const prevPid = parseInt(fs.readFileSync(file).toString(), 10)
    try {
      process.kill(prevPid)
      console.log(`kill non exit process: ${prevPid}`)
    } catch (e) {
      console.log(`kill process failed: ${prevPid}`)
    }
  }
  fs.writeFileSync(file, `${pid}`)

  process.on('exit', function() {
    console.log(`process exit: ${pid}`)
    fs.unlinkSync(file)
  })
}

process.on('uncaughtException', function(e: ErrnoException) {
  fatal.error(e)
  console.log(e.stack)
  // TODO, 日志写入是异步, 先用 setTimeout
  setTimeout(() => {
    process.exit(1)
  }, 1000)
})

// 将 debug 环境下 在终端打印更有好的日志，非 debug 环境下不打印
Console.config({
  debug: config.IS_LOCAL
})

export default async function(app: Koa) {}
