import express from 'express'
import bodyParser from 'body-parser'
import generate from './generate/core'
import shelljs from 'shelljs'
import fs from 'fs'
const app = express()
const port = 4000
app.all('*', (req:any, res:any, next:Function) => {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type")
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
  res.header("Access-Control-Allow-Credentials", true)
  res.header("X-Powered-By", ' 4.2.1')
  next()
})
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.post('/generate', (req: any, res: any) => {
  let data = generate(req.body)
  shelljs.touch('./index.vue')
  fs.writeFileSync(process.cwd() + '/index.vue', data)
  res.download(process.cwd() + '/index.vue', `${req.body.id || 'index'}.vue`)
})
app.listen(port, () => {
  console.log(`${port} port is runing`)
})
