Skip to content

Commit

Permalink
fix: Handle command argument path boundary cases on some systems (doo…
Browse files Browse the repository at this point in the history
…cs#128)

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

* feat: Add service startup detection function

* feat: upgrade dependencies

* feat: Add version display function
  • Loading branch information
wll8 authored Feb 12, 2022
1 parent 862651f commit 4f296d6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ httpData
package-lock.json
public/upload/**
!public/upload/*.gitkeep

.history
25 changes: 18 additions & 7 deletions md-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const getPort = require(`get-port`)
const {
portIsOk,
handleSpace,
colors,
spawn,
parseArgv,
Expand All @@ -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))
4 changes: 2 additions & 2 deletions md-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@doocs/md-cli",
"version": "0.0.3",
"version": "0.0.4",
"description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性",
"main": "index.js",
"scripts": {
Expand All @@ -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"
}
}
28 changes: 28 additions & 0 deletions md-cli/util.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -183,6 +209,8 @@ function dcloud(spaceInfo) {
}

module.exports = {
portIsOk,
handleSpace,
colors: colors(),
spawn,
parseArgv,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4f296d6

Please sign in to comment.