mirror of
https://github.com/doocs/md.git
synced 2024-11-24 19:10:34 +08:00
chore: upgrade md-cli to v0.0.6 (#257)
* fix: Update the command line parameter passing method * chore: Upgrade mockm * chore: Only use mockm with md-cli * chore: Update the version number of md-cli * feat: Optimize process management * fix: Optimize space paths
This commit is contained in:
parent
0af8054e9f
commit
5dec24f819
@ -1,11 +1,10 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const { ProcessManager } = require(`@wll8/process-manager`);
|
||||||
const getPort = require(`get-port`)
|
const getPort = require(`get-port`)
|
||||||
const {
|
const {
|
||||||
portIsOk,
|
|
||||||
handleSpace,
|
handleSpace,
|
||||||
colors,
|
colors,
|
||||||
spawn,
|
|
||||||
parseArgv,
|
parseArgv,
|
||||||
} = require(`./util.js`)
|
} = require(`./util.js`)
|
||||||
|
|
||||||
@ -22,21 +21,18 @@ new Promise(async () => {
|
|||||||
testPort,
|
testPort,
|
||||||
replayPort,
|
replayPort,
|
||||||
'--config': handleSpace(`${__dirname}/mm.config.js`),
|
'--config': handleSpace(`${__dirname}/mm.config.js`),
|
||||||
}).map(([key, val]) => `${key}=${val}`).join(` `)
|
}).map(([key, val]) => `${key}=${val}`)
|
||||||
const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, line]
|
const cliArg = [handleSpace(`${__dirname}/node_modules/mockm/run.js`), `--log-line`, ...line]
|
||||||
spawn(`node`, cliArg)
|
|
||||||
let oldTime = Date.now()
|
|
||||||
console.log(`服务启动中...`)
|
|
||||||
console.log(`doocs/md-cli v${require(`./package.json`).version}`)
|
console.log(`doocs/md-cli v${require(`./package.json`).version}`)
|
||||||
let timer = setInterval(async () => {
|
console.log(`服务启动中...`)
|
||||||
if((await portIsOk(port)) !== true) { // 服务启动成功
|
const cp = new ProcessManager(cliArg)
|
||||||
clearInterval(timer)
|
cp.on(`stdout`, (info = ``) => {
|
||||||
|
if(info.match(`:${port}/`)) {
|
||||||
console.log(`服务已启动:`)
|
console.log(`服务已启动:`)
|
||||||
console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
|
console.log(`打开链接 ${colors.green(`http://127.0.0.1:${port}/md/`)} 即刻使用吧~`)
|
||||||
} else if(Date.now() - oldTime > 10 * 1e3) {
|
}
|
||||||
clearInterval(timer)
|
if(info.match(`Port is occupied`)) {
|
||||||
console.log(`服务 ${port} 初始化失败, 请重试.`)
|
|
||||||
process.exit()
|
process.exit()
|
||||||
}
|
}
|
||||||
}, 1e3);
|
})
|
||||||
}).catch(err => console.log(err))
|
}).catch(err => console.log(err))
|
||||||
|
4700
md-cli/package-lock.json
generated
4700
md-cli/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@doocs/md-cli",
|
"name": "@doocs/md-cli",
|
||||||
"version": "0.0.5",
|
"version": "0.0.6",
|
||||||
"description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性",
|
"description": "✍ 一款高度简洁的微信 Markdown 编辑器:支持 Markdown 所有基础语法、色盘取色、一键复制并粘贴到公众号后台、多图上传、一键下载文档、自定义 CSS 样式、一键重置等特性",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -20,9 +20,10 @@
|
|||||||
"author": "wll8",
|
"author": "wll8",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@wll8/process-manager": "^1.0.1",
|
||||||
"form-data": "2.3.3",
|
"form-data": "2.3.3",
|
||||||
"get-port": "5.1.1",
|
"get-port": "5.1.1",
|
||||||
"mockm": "^1.1.26-alpha.29",
|
"mockm": "^1.1.27-alpha.2",
|
||||||
"node-fetch": "^3.1.0"
|
"node-fetch": "^3.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ function portIsOk (port) {
|
|||||||
* @param {string} str
|
* @param {string} str
|
||||||
*/
|
*/
|
||||||
function handleSpace(str = ``) {
|
function handleSpace(str = ``) {
|
||||||
const newStr = require('os').type() === 'Windows_NT' ? `"${str}"` : str
|
const newStr = require('os').type() === 'Windows_NT' && str.match(` `) ? `"${str}"` : str
|
||||||
return newStr
|
return newStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
mm/.gitignore
vendored
1
mm/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
httpData/
|
|
@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"paths": {},
|
|
||||||
"disable": []
|
|
||||||
}
|
|
@ -1,65 +0,0 @@
|
|||||||
const fs = require(`fs`)
|
|
||||||
const path = require(`path`)
|
|
||||||
|
|
||||||
const { dcloud } = require(`./util.js`)
|
|
||||||
|
|
||||||
// unicloud 服务空间配置
|
|
||||||
const spaceInfo = {
|
|
||||||
spaceId: ``,
|
|
||||||
clientSecret: ``,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置说明请参考文档:
|
|
||||||
* https://hongqiye.com/doc/mockm/config/option.html
|
|
||||||
* @type {import('mockm/@types/config').Config}
|
|
||||||
*/
|
|
||||||
module.exports = (util) => {
|
|
||||||
const port = 9000
|
|
||||||
return {
|
|
||||||
port,
|
|
||||||
testPort: 9005,
|
|
||||||
replayPort: 9001,
|
|
||||||
watch: [`./util.js`],
|
|
||||||
api: {
|
|
||||||
async '/upload'(req, res) {
|
|
||||||
const multiparty = await util.toolObj.generate.initPackge(`multiparty`)
|
|
||||||
const form = new multiparty.Form({
|
|
||||||
uploadDir: `../public/upload/`,
|
|
||||||
})
|
|
||||||
form.parse(req, async (err, fields = [], files) => {
|
|
||||||
const file = files.file[0]
|
|
||||||
let url = `http://127.0.0.1:${port}/public/upload/${
|
|
||||||
path.parse(file.path).base
|
|
||||||
}`
|
|
||||||
try {
|
|
||||||
url = await dcloud(spaceInfo)({
|
|
||||||
name: file.originalFilename,
|
|
||||||
file: fs.createReadStream(file.path),
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
// console.log(err)
|
|
||||||
}
|
|
||||||
res.json({ url })
|
|
||||||
})
|
|
||||||
},
|
|
||||||
},
|
|
||||||
static: [
|
|
||||||
{
|
|
||||||
// 测试 netlify 部署
|
|
||||||
fileDir: `../dist`,
|
|
||||||
path: `/`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// 测试 gitee/github 部署
|
|
||||||
fileDir: `../dist`,
|
|
||||||
path: `/md`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// 访问公共目录
|
|
||||||
fileDir: `../public`,
|
|
||||||
path: `/public`,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
22
mm/readme.md
22
mm/readme.md
@ -1,22 +0,0 @@
|
|||||||
# 说明
|
|
||||||
|
|
||||||
此目录是运行命令 `mm --template` 之后生成的 mockm 常用配置, 该命令做了以下事情:
|
|
||||||
|
|
||||||
在运行目录的 package.json 的 scripts 中添加命令 `"mm": "npx mockm --cwd=mm"`, 如果没有 package.json 文件, 会自动创建.
|
|
||||||
|
|
||||||
创建名为 mm 的目录, 文件说明如下, 如果存在则不覆盖:
|
|
||||||
|
|
||||||
```
|
|
||||||
mm/
|
|
||||||
- api/ -- 手动创建的 api
|
|
||||||
- httpData/ -- 请求记录, 一般不提交到版本库
|
|
||||||
- apiWeb.json -- 从 UI 界面上创建的接口信息
|
|
||||||
- util.js -- 一些公用方法
|
|
||||||
- mm.config.js -- mockm 的配置文件
|
|
||||||
```
|
|
||||||
|
|
||||||
## 参考
|
|
||||||
|
|
||||||
- [mm 代码仓库](https://github.com/wll8/mockm/)
|
|
||||||
- [mm 文档](https://hongqiye.com/doc/mockm/)
|
|
||||||
- [mockjs 文档](http://wll8.gitee.io/mockjs-examples/)
|
|
112
mm/util.js
112
mm/util.js
@ -1,112 +0,0 @@
|
|||||||
const fetch = (...args) =>
|
|
||||||
import(`node-fetch`).then(({ default: fetch }) => fetch(...args))
|
|
||||||
const FormData = require(`form-data`)
|
|
||||||
|
|
||||||
function dcloud(spaceInfo) {
|
|
||||||
if (Boolean(spaceInfo.spaceId && spaceInfo.clientSecret) === false) {
|
|
||||||
throw new Error(`请填写 spaceInfo`)
|
|
||||||
}
|
|
||||||
|
|
||||||
function sign(data, secret) {
|
|
||||||
const hmac = require(`crypto`).createHmac(`md5`, secret)
|
|
||||||
// 排序 obj 再转换为 key=val&key=val 的格式
|
|
||||||
const str = Object.keys(data)
|
|
||||||
.sort()
|
|
||||||
.reduce((acc, cur) => `${acc}&${cur}=${data[cur]}`, ``)
|
|
||||||
.slice(1)
|
|
||||||
hmac.update(str)
|
|
||||||
return hmac.digest(`hex`)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function anonymousAuthorize() {
|
|
||||||
const data = {
|
|
||||||
method: `serverless.auth.user.anonymousAuthorize`,
|
|
||||||
params: `{}`,
|
|
||||||
spaceId: spaceInfo.spaceId,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
}
|
|
||||||
return await fetch(`https://api.bspapp.com/client`, {
|
|
||||||
headers: {
|
|
||||||
'x-serverless-sign': sign(data, spaceInfo.clientSecret),
|
|
||||||
},
|
|
||||||
body: `{"method":"serverless.auth.user.anonymousAuthorize","params":"{}","spaceId":"${spaceInfo.spaceId}","timestamp":${data.timestamp}}`,
|
|
||||||
method: `POST`,
|
|
||||||
}).then((res) => res.json())
|
|
||||||
}
|
|
||||||
|
|
||||||
async function report({ id, token }) {
|
|
||||||
const reportReq = {
|
|
||||||
method: `serverless.file.resource.report`,
|
|
||||||
params: `{"id":"${id}"}`,
|
|
||||||
spaceId: spaceInfo.spaceId,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
token: token,
|
|
||||||
}
|
|
||||||
return await fetch(`https://api.bspapp.com/client`, {
|
|
||||||
headers: {
|
|
||||||
'x-basement-token': reportReq.token,
|
|
||||||
'x-serverless-sign': sign(reportReq, spaceInfo.clientSecret),
|
|
||||||
},
|
|
||||||
body: JSON.stringify(reportReq),
|
|
||||||
method: `POST`,
|
|
||||||
}).then((res) => res.json())
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateProximalSign({ name, token }) {
|
|
||||||
const data = {
|
|
||||||
method: `serverless.file.resource.generateProximalSign`,
|
|
||||||
params: `{"env":"public","filename":"${name}"}`,
|
|
||||||
spaceId: spaceInfo.spaceId,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
token,
|
|
||||||
}
|
|
||||||
const res = await fetch(`https://api.bspapp.com/client`, {
|
|
||||||
headers: {
|
|
||||||
'x-basement-token': data.token,
|
|
||||||
'x-serverless-sign': sign(data, spaceInfo.clientSecret),
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
method: `POST`,
|
|
||||||
}).then((res) => res.json())
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
async function upload({ data, file }) {
|
|
||||||
const formdata = new FormData()
|
|
||||||
Object.entries({
|
|
||||||
'Cache-Control': `max-age=2592000`,
|
|
||||||
'Content-Disposition': `attachment`,
|
|
||||||
OSSAccessKeyId: data.accessKeyId,
|
|
||||||
Signature: data.signature,
|
|
||||||
host: data.host,
|
|
||||||
id: data.id,
|
|
||||||
key: data.ossPath,
|
|
||||||
policy: data.policy,
|
|
||||||
success_action_status: 200,
|
|
||||||
file,
|
|
||||||
}).forEach(([key, val]) => formdata.append(key, val))
|
|
||||||
|
|
||||||
return await fetch(`https://${data.host}`, {
|
|
||||||
headers: {
|
|
||||||
'X-OSS-server-side-encrpytion': `AES256`,
|
|
||||||
},
|
|
||||||
body: formdata,
|
|
||||||
method: `POST`,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function uploadFile({ name = `unnamed.file`, file }) {
|
|
||||||
const token = (await anonymousAuthorize()).data.accessToken
|
|
||||||
const res = await generateProximalSign({ name, token })
|
|
||||||
await upload({ data: res.data, file })
|
|
||||||
await report({ id: res.data.id, token })
|
|
||||||
const fileUrl = `https://${res.data.cdnDomain}/${res.data.ossPath}`
|
|
||||||
return fileUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
return uploadFile
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
dcloud,
|
|
||||||
}
|
|
10095
package-lock.json
generated
10095
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -4,13 +4,12 @@
|
|||||||
"private": false,
|
"private": false,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "husky install",
|
"prepare": "husky install",
|
||||||
"lint": "vue-cli-service lint src && vue-cli-service lint mm",
|
"lint": "vue-cli-service lint src",
|
||||||
"start": "npm run lint -- --fix && run-p serve mm",
|
"start": "npm run lint -- --fix && run-p serve",
|
||||||
"serve": "vue-cli-service serve",
|
"serve": "vue-cli-service serve",
|
||||||
"build:h5-netlify": "cross-env SERVER_ENV=NETLIFY vue-cli-service build",
|
"build:h5-netlify": "cross-env SERVER_ENV=NETLIFY vue-cli-service build",
|
||||||
"build": "vue-cli-service build",
|
"build": "vue-cli-service build",
|
||||||
"build-cli": "npm run build && npx shx rm -rf md-cli/dist && npx shx rm -rf dist/**/*.map && npx shx cp -r dist md-cli/ && cd md-cli && npm pack",
|
"build-cli": "npm run build && npx shx rm -rf md-cli/dist && npx shx rm -rf dist/**/*.map && npx shx cp -r dist md-cli/ && cd md-cli && npm pack"
|
||||||
"mm": "npx mockm --cwd=mm"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ali-oss": "^6.17.1",
|
"ali-oss": "^6.17.1",
|
||||||
@ -52,7 +51,6 @@
|
|||||||
"less-loader": "^7.3.0",
|
"less-loader": "^7.3.0",
|
||||||
"mini-types": "*",
|
"mini-types": "*",
|
||||||
"miniprogram-api-typings": "*",
|
"miniprogram-api-typings": "*",
|
||||||
"mockm": "^1.1.26-alpha.29",
|
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"postcss-comment": "^2.0.0",
|
"postcss-comment": "^2.0.0",
|
||||||
"prettier": "^2.8.8",
|
"prettier": "^2.8.8",
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
const isProd = process.env.NODE_ENV === `production`
|
const isProd = process.env.NODE_ENV === `production`
|
||||||
|
|
||||||
const crypto = require('crypto');
|
const crypto = require(`crypto`)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* md4 algorithm is not available anymore in NodeJS 17+ (because of lib SSL 3).
|
* md4 algorithm is not available anymore in NodeJS 17+ (because of lib SSL 3).
|
||||||
* In that case, silently replace md4 by md5 algorithm.
|
* In that case, silently replace md4 by md5 algorithm.
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
crypto.createHash('md4');
|
crypto.createHash(`md4`)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const origCreateHash = crypto.createHash;
|
const origCreateHash = crypto.createHash
|
||||||
crypto.createHash = (alg, opts) => {
|
crypto.createHash = (alg, opts) => {
|
||||||
return origCreateHash(alg === 'md4' ? 'md5' : alg, opts);
|
return origCreateHash(alg === `md4` ? `md5` : alg, opts)
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Loading…
Reference in New Issue
Block a user