zoro-cli
Version:
https://github.com/vuejs/vue-cli
50 lines (46 loc) • 1.33 kB
JavaScript
const watch = require('watch')
const execa = require('execa')
const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const { info } = require('zoro-cli-util/logger')
const portFile = path.join(process.cwd(), '.port')
if (!fs.existsSync(portFile)) {
console.error(`请先运行 ${chalk.cyan('npm run dev')} 启动 local 服务器`)
process.exit(0)
}
let anyproxyProcess
const startAnyproxy = () => {
let action = 'start'
if (anyproxyProcess) {
action = 'restart'
// .off is not available until node 10...
anyproxyProcess.removeListener('exit', startAnyproxy)
anyproxyProcess.kill('SIGINT')
anyproxyProcess = null
}
if (!anyproxyProcess) {
info(`${action}: proxy server at port 6001, web server at port 6002`)
info()
anyproxyProcess = execa.shell(
'anyproxy --rule ./mock/anyproxy.config.js --port 6001 --web 6002',
{
stdio: 'inherit',
}
)
anyproxyProcess.on('exit', startAnyproxy)
}
}
// watch file tree and restart anyproxy
watch.watchTree(
__dirname,
{
// Specifies the interval duration in seconds, the time period between polling for file changes.
interval: 3,
// do not ignore .port
ignoreDotFiles: false,
ignoreUnreadableDir: true,
ignoreNotPermitted: true,
},
startAnyproxy
)