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 package-lock.json
public/upload/** public/upload/**
!public/upload/*.gitkeep !public/upload/*.gitkeep
.history

View File

@ -2,6 +2,8 @@
const getPort = require(`get-port`) const getPort = require(`get-port`)
const { const {
portIsOk,
handleSpace,
colors, colors,
spawn, spawn,
parseArgv, parseArgv,
@ -19,15 +21,24 @@ new Promise(async () => {
port, port,
testPort, testPort,
replayPort, replayPort,
'--config': `"${__dirname}/mm.config.js"`, '--config': handleSpace(`${__dirname}/mm.config.js`),
}).map(([key, val]) => `${key}=${val}`).join(` `) }).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) spawn(`node`, cliArg)
setTimeout(() => { setTimeout(async () => {
// process.stdout.write('\33c\33[3J')
console.log(``) 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/`)} 即刻使用吧~`) console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
} catch (error) {
console.error(`启动错误 ${error}`)
process.exit()
}
console.log(``) console.log(``)
}, 3*1e3); }, 3*1e3);
}) }).catch(err => console.log(err))

View File

@ -1,6 +1,6 @@
{ {
"name": "@doocs/md-cli", "name": "@doocs/md-cli",
"version": "0.0.3", "version": "0.0.4",
"description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性", "description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -22,7 +22,7 @@
"dependencies": { "dependencies": {
"form-data": "2.3.3", "form-data": "2.3.3",
"get-port": "5.1.1", "get-port": "5.1.1",
"mockm": "^1.1.25", "mockm": "^1.1.26-alpha.3",
"node-fetch": "^3.1.0" "node-fetch": "^3.1.0"
} }
} }

View File

@ -1,6 +1,32 @@
const fetch = (...args) => import(`node-fetch`).then(({default: fetch}) => fetch(...args)) const fetch = (...args) => import(`node-fetch`).then(({default: fetch}) => fetch(...args))
const FormData = require(`form-data`) 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 * https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color
@ -183,6 +209,8 @@ function dcloud(spaceInfo) {
} }
module.exports = { module.exports = {
portIsOk,
handleSpace,
colors: colors(), colors: colors(),
spawn, spawn,
parseArgv, parseArgv,

View File

@ -47,7 +47,7 @@
"less-loader": "^7.3.0", "less-loader": "^7.3.0",
"mini-types": "*", "mini-types": "*",
"miniprogram-api-typings": "*", "miniprogram-api-typings": "*",
"mockm": "^1.1.25", "mockm": "^1.1.26-alpha.3",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"postcss-comment": "^2.0.0", "postcss-comment": "^2.0.0",
"raw-loader": "^4.0.2", "raw-loader": "^4.0.2",