-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env node
const getPort = require(`get-port`)
const {
portIsOk,
handleSpace,
colors,
spawn,
parseArgv,
} = require(`./util.js`)
const arg = parseArgv()
new Promise(async () => {
let { port = 8800, testPort, replayPort } = arg
port = Number(port)
;[port, testPort, replayPort] = await Promise.all([port, port+1, port+2].map(item => getPort({port: item}) )).catch(err => console.log(`err`, err))
const line = Object.entries({
...arg,
proxy: `https://doocs.gitee.io/`,
port,
testPort,
replayPort,
'--config': handleSpace(`${__dirname}/mm.config.js`),
}).map(([key, val]) => `${key}=${val}`).join(` `)
const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, line]
spawn(`node`, cliArg)
let oldTime = Date.now()
console.log(`服务启动中...`)
console.log(`doocs/md-cli v${require(`./package.json`).version}`)
let timer = setInterval(async () => {
if((await portIsOk(port)) !== true) { // 服务启动成功
clearInterval(timer)
console.log(`服务已启动:`)
console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
} else if(Date.now() - oldTime > 10 * 1e3) {
clearInterval(timer)
console.log(`服务 ${port} 初始化失败, 请重试.`)
process.exit()
}
}, 1e3);
}).catch(err => console.log(err))