const { exec } = require('child_process')
const path =require('path')
const axios = require('axios')

function startClient(originLog, options = {}){
    // return
    const clientPort = options.clientPort || 8848
    const serverPort = options.serverWsPort || 8888
    const UIURL = `http://localhost:${clientPort}?port=${serverPort}`

    axios({ method:'GET', url: UIURL
    }).then(res=>{
        originLog('🔵 node-conosle => UI界面已开启')
    }).catch(e=>{
        originLog('🔵 node-conosle => UI界面未开启',e.message)
        originLog(`🔵 node-conosle => 正在开启UI界面,浏览器打开 ${UIURL} 进行调试`)
        if(options && options.dev){
            originLog('🔵 node-conosle => 开发模式已开启,请手动启动前端服务')
            return
        }
        const clientPath = path.resolve(__dirname, '../client/')
        const cmd = `cd ${clientPath} && npx http-server --cors -p${clientPort}`
        exec(cmd, (err, stdout, stderr) => {
            if(err){
                originLog(`🔵 node-conosle => 开启前端界面错误:${err}`)
            }
        })
    })
}


module.exports = startClient

