2021-11-28 20:11:00 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2023-12-06 18:46:27 +08:00
|
|
|
const { ProcessManager } = require(`@wll8/process-manager`);
|
2021-11-28 20:11:00 +08:00
|
|
|
const getPort = require(`get-port`)
|
|
|
|
const {
|
2022-02-12 21:00:34 +08:00
|
|
|
handleSpace,
|
2021-11-28 20:11:00 +08:00
|
|
|
colors,
|
|
|
|
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,
|
2022-02-12 21:00:34 +08:00
|
|
|
'--config': handleSpace(`${__dirname}/mm.config.js`),
|
2023-12-06 18:46:27 +08:00
|
|
|
}).map(([key, val]) => `${key}=${val}`)
|
|
|
|
const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, ...line]
|
2022-08-04 10:26:40 +08:00
|
|
|
console.log(`doocs/md-cli v${require(`./package.json`).version}`)
|
2023-12-06 18:46:27 +08:00
|
|
|
console.log(`服务启动中...`)
|
|
|
|
const cp = new ProcessManager(cliArg)
|
|
|
|
cp.on(`stdout`, (info = ``) => {
|
|
|
|
if(info.match(`:${port}/`)) {
|
2022-02-12 21:00:34 +08:00
|
|
|
console.log(`服务已启动:`)
|
|
|
|
console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
|
2023-12-06 18:46:27 +08:00
|
|
|
}
|
|
|
|
if(info.match(`Port is occupied`)) {
|
2022-02-12 21:00:34 +08:00
|
|
|
process.exit()
|
|
|
|
}
|
2023-12-06 18:46:27 +08:00
|
|
|
})
|
2022-02-12 21:00:34 +08:00
|
|
|
}).catch(err => console.log(err))
|