@doocs/md-cli 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +18 -7
  2. package/package.json +3 -3
  3. package/util.js +29 -1
package/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  const getPort = require(`get-port`)
4
4
  const {
5
+ portIsOk,
6
+ handleSpace,
5
7
  colors,
6
8
  spawn,
7
9
  parseArgv,
@@ -19,15 +21,24 @@ new Promise(async () => {
19
21
  port,
20
22
  testPort,
21
23
  replayPort,
22
- '--config': `"${__dirname}/mm.config.js"`,
24
+ '--config': handleSpace(`${__dirname}/mm.config.js`),
23
25
  }).map(([key, val]) => `${key}=${val}`).join(` `)
24
- const cliArg = [`"${__dirname}/node_modules/mockm/run.js"`, `--log-line`, line]
26
+ const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, line]
25
27
  spawn(`node`, cliArg)
26
- setTimeout(() => {
27
- // process.stdout.write('\33c\33[3J')
28
+ setTimeout(async () => {
28
29
  console.log(``)
29
- console.log(`doocs/md 服务已启动:`)
30
- console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
30
+ console.log(`doocs/md-cli v${require(`./package.json`).version}`)
31
+ console.log(``)
32
+ try {
33
+ if(await portIsOk(port) === true) {
34
+ throw new Error(`服务 ${port} 初始化失败`)
35
+ }
36
+ console.log(`服务已启动:`)
37
+ console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
38
+ } catch (error) {
39
+ console.error(`启动错误 ${error}`)
40
+ process.exit()
41
+ }
31
42
  console.log(``)
32
43
  }, 3*1e3);
33
- })
44
+ }).catch(err => console.log(err))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doocs/md-cli",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "dependencies": {
23
23
  "form-data": "2.3.3",
24
24
  "get-port": "5.1.1",
25
- "mockm": "^1.1.25",
26
- "node-fetch": "2.6.2"
25
+ "mockm": "^1.1.26-alpha.3",
26
+ "node-fetch": "^3.1.0"
27
27
  }
28
28
  }
package/util.js CHANGED
@@ -1,6 +1,32 @@
1
- const fetch = require('node-fetch')
1
+ const fetch = (...args) => import(`node-fetch`).then(({default: fetch}) => fetch(...args))
2
2
  const FormData = require(`form-data`)
3
3
 
4
+
5
+ /**
6
+ * 判断端口是否可用
7
+ * @param {string|array} port 多个端口用数组
8
+ */
9
+ function portIsOk (port) {
10
+ if(typeof(port) === `object`) { // 判断多个端口
11
+ return Promise.all(port.map(item => portIsOk(item)))
12
+ }
13
+ return new Promise(resolve => {
14
+ const net = require(`net`)
15
+ const server = net.createServer().listen(port)
16
+ server.on(`listening`, () => server.close(resolve(true)))
17
+ server.on(`error`, () => resolve(port))
18
+ })
19
+ }
20
+
21
+ /**
22
+ * 处理不同系统的命令行空格差异, 在 cp.spawn 中的参数中, 如果包含空格, win 平台需要使用双引号包裹, unix 不需要
23
+ * @param {string} str
24
+ */
25
+ function handleSpace(str = ``) {
26
+ const newStr = require('os').type() === 'Windows_NT' ? `"${str}"` : str
27
+ return newStr
28
+ }
29
+
4
30
  /**
5
31
  * 自定义控制台颜色
6
32
  * https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color
@@ -183,6 +209,8 @@ function dcloud(spaceInfo) {
183
209
  }
184
210
 
185
211
  module.exports = {
212
+ portIsOk,
213
+ handleSpace,
186
214
  colors: colors(),
187
215
  spawn,
188
216
  parseArgv,