Skip to content

Commit

Permalink
feat(mastr): 批量读取mock列表
Browse files Browse the repository at this point in the history
  • Loading branch information
陈家敬 committed Jul 13, 2020
1 parent 354d89d commit 5a857ed
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
2 changes: 1 addition & 1 deletion mock/api.ts → mock/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
'GET /api/user/1': {
name: '陈家敬'
},
Expand Down
9 changes: 0 additions & 9 deletions mock/data/a.ts

This file was deleted.

9 changes: 9 additions & 0 deletions mock/data/abc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
'GET /api/info/1': {
name: '陈家敬'
},

'POST /api/info': {
file: 'xxxx'
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"express": "^4.17.1",
"get-port": "^5.1.1",
"glob": "^7.1.6"
"glob": "^7.1.6",
"path-to-regexp": "^6.1.0"
},
"devDependencies": {
"@babel/core": "^7.10.4",
Expand All @@ -47,6 +48,7 @@
"@types/express": "^4.17.7",
"@types/http-errors": "^1.8.0",
"@types/node": "^14.0.22",
"@types/path-to-regexp": "^1.7.0",
"commitizen": "^4.1.2",
"core-js": "^3.6.5",
"cross-env": "^7.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import express from "express";
import getPort from "get-port";
import path from "path";

import { getMockConfigs } from "./utils";
import { getMockApis } from "./utils";

getMockConfigs({ appPath: "./" })
getMockApis();

const app = express();
export const HTTP_METHODS = ["GET", "POST", "HEAD", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"];
Expand Down
67 changes: 52 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path";
import glob from "glob";
import fs from "fs";
import { pathToRegexp } from "path-to-regexp";

export const MOCK_DIR = "mock"; // mock数据目录
export const MOCK_DIR = "mock";
export const HTTP_METHODS = ["GET", "POST", "HEAD", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"];


Expand All @@ -13,22 +14,58 @@ export function getMockConfigs({ appPath, mocks }: { appPath: string, mocks?: {
const mockFiles = glob.sync("**/*.[tj]s", {
cwd: mockDir
})

if (mockFiles.length) {
const absMockFiles = mockFiles.map(file => path.join(mockDir, file))
console.log(absMockFiles)
// absMockFiles.forEach(absFile => {
// let mockConfig = {}
// try {
// delete require.cache[absFile]
// } catch (err) {
// throw err
// }
// mockConfigs = Object.assign({}, mockConfigs, mockConfig)
// })

absMockFiles.forEach(absFile => {
try {
const file = require(`../${absFile}`)
mockConfigs = Object.assign({}, mockConfigs, file)
} catch (error) {
console.error(error)
}
})
}
}
// if (mocks) {
// mockConfigs = Object.assign({}, mockConfigs, mocks)
// }
// return mockConfigs
if (mocks) {
mockConfigs = Object.assign({}, mockConfigs, mocks)
}

return mockConfigs
}

export function parseMockApis(mockConfigs: { [x: string]: any; }) {
const apiList: any[] = []
Object.keys(mockConfigs).map(key => {
const result = mockConfigs[key]
let method = "GET"
let apiPath = ""
const keySplit = key.split(/\s+/g)
if (keySplit.length === 2) {
method = keySplit[0] || method
apiPath = keySplit[1]
if (!HTTP_METHODS.includes(method)) {
throw `配置的 HTTP 方法名 ${method} 不正确,应该是 ${HTTP_METHODS.toString()} 中的一员!`
}
} else if (keySplit.length === 1) {
apiPath = keySplit[0]
}
const keys: import("path-to-regexp").Key[] | undefined = []
const reg = pathToRegexp(apiPath, keys)
apiList.push({
apiPath,
reg,
keys,
method,
result
})
})
console.log(apiList)
return apiList
}

export function getMockApis() {
const mockConfigs = getMockConfigs({ appPath: "./" })
return parseMockApis(mockConfigs)
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,13 @@
resolved "https://registry.npm.taobao.org/@types/parse-json/download/@types/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha1-L4u0QUNNFjs1+4/9zNcTiSf/uMA=

"@types/path-to-regexp@^1.7.0":
version "1.7.0"
resolved "https://registry.npm.taobao.org/@types/path-to-regexp/download/@types/path-to-regexp-1.7.0.tgz#f1af9d9c9db5f8ad80aa75ed424db665c48d6cd4"
integrity sha1-8a+dnJ21+K2AqnXtQk22ZcSNbNQ=
dependencies:
path-to-regexp "*"

"@types/q@^1.5.1":
version "1.5.4"
resolved "https://registry.npm.taobao.org/@types/q/download/@types/q-1.5.4.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fq%2Fdownload%2F%40types%2Fq-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
Expand Down Expand Up @@ -5145,6 +5152,11 @@ path-parse@^1.0.6:
resolved "https://registry.npm.taobao.org/path-parse/download/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
integrity sha1-1i27VnlAXXLEc37FhgDp3c8G0kw=

path-to-regexp@*, path-to-regexp@^6.1.0:
version "6.1.0"
resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-6.1.0.tgz#0b18f88b7a0ce0bfae6a25990c909ab86f512427"
integrity sha1-Cxj4i3oM4L+uaiWZDJCauG9RJCc=

path-to-regexp@0.1.7:
version "0.1.7"
resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
Expand Down

0 comments on commit 5a857ed

Please sign in to comment.