From 19d87ce67b709edfa85065e86c994f8039cdcd3e Mon Sep 17 00:00:00 2001 From: xw Date: Thu, 4 Aug 2022 10:26:40 +0800 Subject: [PATCH] fix(cli): More suitable service detection method (#169) --- md-cli/index.js | 22 ++++++++++------------ md-cli/package.json | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/md-cli/index.js b/md-cli/index.js index df791bc..4dc278e 100644 --- a/md-cli/index.js +++ b/md-cli/index.js @@ -25,20 +25,18 @@ new Promise(async () => { }).map(([key, val]) => `${key}=${val}`).join(` `) const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, line] spawn(`node`, cliArg) - setTimeout(async () => { - console.log(``) - console.log(`doocs/md-cli v${require(`./package.json`).version}`) - console.log(``) - try { - if(await portIsOk(port) === true) { - throw new Error(`服务 ${port} 初始化失败`) - } + 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/`)} 即刻使用吧~`) - } catch (error) { - console.error(`启动错误 ${error}`) + } else if(Date.now() - oldTime > 10 * 1e3) { + clearInterval(timer) + console.log(`服务 ${port} 初始化失败, 请重试.`) process.exit() } - console.log(``) - }, 3*1e3); + }, 1e3); }).catch(err => console.log(err)) diff --git a/md-cli/package.json b/md-cli/package.json index e906690..6bd3197 100644 --- a/md-cli/package.json +++ b/md-cli/package.json @@ -1,6 +1,6 @@ { "name": "@doocs/md-cli", - "version": "0.0.4", + "version": "0.0.5", "description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性", "main": "index.js", "scripts": {