refactor: 小优化

pull/180/head
xiaojunnuo 2020-11-08 01:59:00 +08:00
parent 574cc69911
commit 45c63a7dcc
7 changed files with 73 additions and 55 deletions

View File

@ -9,7 +9,7 @@
"license": "MPL-2.0", "license": "MPL-2.0",
"private": false, "private": false,
"scripts": { "scripts": {
"start": "node start.js", "start": "node ./start",
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"

View File

@ -53,22 +53,26 @@ module.exports = {
console.error('开启系统代理失败:', err) console.error('开启系统代理失败:', err)
} }
} }
const plugins = [] try {
for (const key in plugin) { const plugins = []
if (conf.plugin[key].enabled) { for (const key in plugin) {
const start = async () => { if (conf.plugin[key].enabled) {
try { const start = async () => {
await plugin[key].start() try {
console.log(`插件【${key}】已启动`) await plugin[key].start()
} catch (err) { console.log(`插件【${key}】已启动`)
console.log(`插件【${key}】启动失败`, err) } catch (err) {
console.log(`插件【${key}】启动失败`, err)
}
} }
plugins.push(start())
} }
plugins.push(start())
} }
} if (plugins && plugins.length > 0) {
if (plugins && plugins.length > 0) { await Promise.all(plugins)
await Promise.all(plugins) }
} catch (err) {
console.error('开启插件失败:', err)
} }
}, },
shutdown: async () => { shutdown: async () => {
@ -90,25 +94,25 @@ module.exports = {
if (plugins.length > 0) { if (plugins.length > 0) {
await Promise.all(plugins) await Promise.all(plugins)
} }
if (status.proxy.enabled) {
try {
await proxy.close()
console.log('系统代理已关闭')
} catch (err) {
console.log('系统代理关闭失败', err)
}
}
if (status.server.enabled) {
try {
await server.close()
console.log('代理服务已关闭')
} catch (err) {
console.log('代理服务关闭失败', err)
}
}
} catch (error) { } catch (error) {
console.log(error) console.error('插件关闭失败'.error)
}
if (status.proxy.enabled) {
try {
await proxy.close()
console.log('系统代理已关闭')
} catch (err) {
console.error('系统代理关闭失败', err)
}
}
if (status.server.enabled) {
try {
await server.close()
console.log('代理服务已关闭')
} catch (err) {
console.error('代理服务关闭失败', err)
}
} }
}, },
status: { status: {

View File

@ -78,7 +78,7 @@ function childExec (composeCmds) {
console.error('cmd 命令执行错误:', composeCmds, error, stderr) console.error('cmd 命令执行错误:', composeCmds, error, stderr)
reject(error) reject(error)
} else { } else {
console.log('cmd 命令完成:', stdout) // console.log('cmd 命令完成:', stdout)
resolve(stdout) resolve(stdout)
} }
// console.log('关闭 cmd') // console.log('关闭 cmd')

View File

@ -1,22 +0,0 @@
const DevSidercar = require('.')
const fs = require('fs')
// require('json5/lib/register')
// const config = require('../../config/index.json5')
// 启动服务
const mitmproxyPath = './mitmproxy'
async function startup () {
const banner = fs.readFileSync('./banner.txt')
console.log(banner.toString())
await DevSidercar.api.startup({ mitmproxyPath })
console.log('dev-sidecar 已启动')
}
async function onClose () {
console.log('on sigint ')
await DevSidercar.api.shutdown()
console.log('on closed ')
process.exit(0)
}
process.on('SIGINT', onClose)
startup()

View File

@ -0,0 +1,29 @@
const JSON5 = require('json5')
const DevSidecar = require('../index')
const fs = require('fs')
// 启动服务
const mitmproxyPath = './start/mitmproxy'
async function startup () {
const banner = fs.readFileSync('./banner.txt')
console.log(banner.toString())
const configPath = './start/user_config.json5'
if (fs.existsSync(configPath)) {
const file = fs.readFileSync(configPath)
const userConfig = JSON5.parse(file.toString())
DevSidecar.api.config.set(userConfig)
}
await DevSidecar.api.startup({ mitmproxyPath })
console.log('dev-sidecar 已启动')
}
async function onClose () {
console.log('on sigint ')
await DevSidecar.api.shutdown()
console.log('on closed ')
process.exit(0)
}
process.on('SIGINT', onClose)
startup()

View File

@ -0,0 +1,7 @@
{
plugin: {
node: {
enabled: true
}
}
}