fix: Handle command argument path boundary cases on some systems (#128)

* fix: Handle command argument path boundary cases on some systems #126

* feat: Add service startup detection function

* feat: upgrade dependencies

* feat: Add version display function
This commit is contained in:
xw 2022-02-12 21:00:34 +08:00 committed by GitHub
parent 862651fefa
commit 4f296d6145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 11 deletions

2
.gitignore vendored
View File

@ -47,4 +47,4 @@ httpData
package-lock.json
public/upload/**
!public/upload/*.gitkeep
.history

View File

@ -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(`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))

View File

@ -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"
}
}

View File

@ -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,

View File

@ -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",