diff --git a/.gitignore b/.gitignore index 7b3f102..e043bca 100644 --- a/.gitignore +++ b/.gitignore @@ -47,4 +47,4 @@ httpData package-lock.json public/upload/** !public/upload/*.gitkeep - +.history diff --git a/md-cli/index.js b/md-cli/index.js index 56c430e..df791bc 100644 --- a/md-cli/index.js +++ b/md-cli/index.js @@ -2,6 +2,8 @@ const getPort = require(`get-port`) const { + portIsOk, + handleSpace, colors, spawn, parseArgv, @@ -19,15 +21,24 @@ new Promise(async () => { port, testPort, replayPort, - '--config': `"${__dirname}/mm.config.js"`, + '--config': handleSpace(`${__dirname}/mm.config.js`), }).map(([key, val]) => `${key}=${val}`).join(` `) - const cliArg = [`"${__dirname}/node_modules/mockm/run.js"`, `--log-line`, line] + const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, line] spawn(`node`, cliArg) - setTimeout(() => { - // process.stdout.write('\33c\33[3J') + setTimeout(async () => { console.log(``) - console.log(`doocs/md 服务已启动:`) - console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`) + console.log(`doocs/md-cli v${require(`./package.json`).version}`) + console.log(``) + try { + if(await portIsOk(port) === true) { + throw new Error(`服务 ${port} 初始化失败`) + } + console.log(`服务已启动:`) + console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`) + } catch (error) { + console.error(`启动错误 ${error}`) + process.exit() + } console.log(``) }, 3*1e3); -}) +}).catch(err => console.log(err)) diff --git a/md-cli/package.json b/md-cli/package.json index 02bec5d..f459456 100644 --- a/md-cli/package.json +++ b/md-cli/package.json @@ -1,6 +1,6 @@ { "name": "@doocs/md-cli", - "version": "0.0.3", + "version": "0.0.4", "description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性", "main": "index.js", "scripts": { @@ -22,7 +22,7 @@ "dependencies": { "form-data": "2.3.3", "get-port": "5.1.1", - "mockm": "^1.1.25", + "mockm": "^1.1.26-alpha.3", "node-fetch": "^3.1.0" } } diff --git a/md-cli/util.js b/md-cli/util.js index f04cc21..d2549e1 100644 --- a/md-cli/util.js +++ b/md-cli/util.js @@ -1,6 +1,32 @@ const fetch = (...args) => import(`node-fetch`).then(({default: fetch}) => fetch(...args)) const FormData = require(`form-data`) + +/** + * 判断端口是否可用 + * @param {string|array} port 多个端口用数组 + */ +function portIsOk (port) { + if(typeof(port) === `object`) { // 判断多个端口 + return Promise.all(port.map(item => portIsOk(item))) + } + return new Promise(resolve => { + const net = require(`net`) + const server = net.createServer().listen(port) + server.on(`listening`, () => server.close(resolve(true))) + server.on(`error`, () => resolve(port)) + }) +} + +/** + * 处理不同系统的命令行空格差异, 在 cp.spawn 中的参数中, 如果包含空格, win 平台需要使用双引号包裹, unix 不需要 + * @param {string} str + */ +function handleSpace(str = ``) { + const newStr = require('os').type() === 'Windows_NT' ? `"${str}"` : str + return newStr +} + /** * 自定义控制台颜色 * https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color @@ -183,6 +209,8 @@ function dcloud(spaceInfo) { } module.exports = { + portIsOk, + handleSpace, colors: colors(), spawn, parseArgv, diff --git a/package.json b/package.json index fd949de..3bec4d4 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "less-loader": "^7.3.0", "mini-types": "*", "miniprogram-api-typings": "*", - "mockm": "^1.1.25", + "mockm": "^1.1.26-alpha.3", "npm-run-all": "^4.1.5", "postcss-comment": "^2.0.0", "raw-loader": "^4.0.2",