Skip to content

Commit

Permalink
feat: 调整整体解析路口,改为mock文件夹路口去处理
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jul 12, 2019
1 parent 4061177 commit 43d89bd
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 19 deletions.
142 changes: 142 additions & 0 deletions example/mock/swagger/.swagger
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
Result«WorkOrderView» {
success (boolean, optional),
data (WorkOrderView, optional),
msg (string, optional),
code (string, optional),
traceId (string, optional)
}
WorkOrderView {
id (string, optional): 工单信息: 工单id,
cityCode (string, optional): 工单信息: 城市code,
cityName (string, optional): 工单信息: 城市名称,
province (string, optional): 工单信息: 省份名称,
type (string, optional): 工单信息: 工单类型,
number (string, optional): 工单信息: 工单编号,
dateCreate (string, optional): 工单信息: 创建时间,
dateUpdate (string, optional): 工单信息: 最后更新时间,
transactor (string, optional): 工单信息: 处理人id,
transactorName (string, optional): 工单信息: 处理人name,
status (integer, optional): 工单信息: 工单状态 0:待处理 1:处理中 2:已解决,
description (string, optional): 工单信息: 问题描述,
creatorId (string, optional): 工单信息: 创建人id,
creatorName (string, optional): 工单信息: 创建人名称,
source (string, optional): 工单信息: 工单来源,
newlyReply (string, optional): 工单信息: 最近回复,
customerName (string, optional): 工单信息:客户姓名,
orderType (string, optional): 订单信息: 订单类型,
modelName (string, optional): 订单信息: 车型,
orderNo (string, optional): 订单信息: 订单编号,
orderStatus (string, optional): 订单信息: 订单状态,
vin (string, optional): 订单信息: vin码,
overTimeDay (integer, optional): 订单信息: 超时天数,
saleDealerCode (string, optional): 订单信息: 销售车商code,
saleDealerName (string, optional): 订单信息: 销售车商名称,
downPaymentDate (string, optional): 订单信息: 首付支付日期,
returnCarDate (string, optional): 订单信息: 应交车日期,
leaseOrderDetail (string, optional): 订单信息: 订单详情链接,
leaseOrderMonitor (string, optional): 订单信息: 订单监控链接,
phone (string, optional): 工单信息: 手机号,
carType (string, optional): 订单信息: 车型,
belong (integer, optional): 问题归属,
belongString (string, optional): 问题归属字符串,
orderTypeString (string, optional): 订单类型字符串,
typeString (string, optional): 工单类型字符串,
orderStatusString (string, optional): 订单状态中文,
allowSend2SF (boolean, optional): 是否允许转交SF,
feedbackDos (array[FeedbackDo], optional): 回复记录列表(工单反馈列表),
otherFeedbacks (array[FeedbackDo], optional),
workOrders (array[WorkOrderDo], optional): 订单相关的工单列表,
statusString (string, optional),
sourceString (string, optional)
}
FeedbackDo {
id (string, optional),
dateCreate (string, optional),
dateUpdate (string, optional),
dateDelete (string, optional),
userCode (string, optional): 人员code,
userName (string, optional): 人员名字,
type (string, optional): 对应工单类型,
number (string): 工单code,
orderCode (string, optional): 订单code,
question (string, optional): 问题分类,
feedback (string): 工单反馈,
picture (string, optional): 图片,
memo (string, optional): 备注,
source (string, optional): 反馈来源,
portrait (string, optional): 头像,
expectFinishTime (string, optional): 预计完成时间,
count (integer, optional): 统计值,
inc (integer): 0-公开,1-内部,
action (integer, optional): 0-回复,1-创建,2-认领,3-内部备注,4-转交,5-关闭,
file (string, optional): 文件地址,
fileName (string, optional): 文件名称,
pictures (array[string], optional)
}
WorkOrderDo {
id (string, optional),
dateCreate (string, optional),
dateUpdate (string, optional),
dateDelete (string, optional),
cityCode (string, optional): 城市code,
overTime (string, optional): 超时日期,
type (string, optional): 工单类型,
orderType (string, optional): 订单类型,
feedback (integer, optional): 0未反馈,1已反馈,
extra (string, optional): 扩展字段,
number (string, optional): 工单编号,
vin (string, optional): vin码,
shopCode (string, optional): 店铺code,
shopName (string, optional): 店铺名称,
cityName (string, optional): 城市名称,
overTimeDay (integer, optional): 超时天数,
dateFinish (string, optional): 结束时间,
orderNo (string, optional): 订单编号,
transactor (string, optional): 处理人,
transactorName (string, optional): 处理人name,
status (integer, optional): 工单状态 0:待处理(如果处理人为空则为待认领) 1:处理中 2:已解决 3:处理中(SF),
source (string, optional): 工单来源,
description (string, optional): 问题描述,
creatorId (string, optional): 创建人id,
creatorName (string, optional): 创建人名称,
questionType (string, optional): 问题分类,
pic (string, optional): 上传图片,
orderTypeString (string, optional): 订单类型,
typeString (string, optional): 工单类型,
orderExtra (string, optional): 订单额外字段类型,
carExtra (string, optional): 车辆类型,
dealerExtra (string, optional): 车商类型,
dateLastReply (string, optional): 最后回复日期,
portrait (string, optional): 头像<来源弹B>,
phone (string, optional): 手机,
customerName (string, optional): 客户姓名,
lastReply (string, optional): 最后回复内容,
appReplyNumber (integer, optional): 客户端回复次数,
webReplyNumber (integer, optional): web端回复次数,
belong (integer, optional): 问题归属,
belongString (string, optional): 问题归属文字,
firstReplyDate (string, optional): 最初回复日期,
send2SFDate (string, optional): 最初回复日期,
allowSend2SF (boolean, optional): 是否允许转交SF,
feedbacks (array[FeedbackDo], optional): 回复列表,
leaseConsiceOrderView (LeaseConsiceOrderView, optional): 订单信息,
file (string, optional): 文件地址,
provinceCode (string, optional): 省份code,
statusString (string, optional),
sourceString (string, optional)
}
LeaseConsiceOrderView {
orderNo (string, optional): 订单编号,
orderType (string, optional): 订单类型,
orderStatus (string, optional): 订单状态,
orderStatusString (string, optional): 订单状态中文,
name (string, optional): 用户姓名,
mobile (string, optional): 用户手机号,
modelName (string, optional): 车型,
vin (string, optional): VIN码,
saleDealerName (string, optional): 销售车商名称,
downPaymentDate (string, optional): 首付支付日期,
returnCarDate (string, optional): 应交车日期,
leaseOrderDetail (string, optional): 订单详情链接,
leaseOrderMonitor (string, optional): 订单监控链接
}
6 changes: 3 additions & 3 deletions src/core/generateSingleSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as readline from 'readline'
import * as path from 'path'
import * as fs from 'fs'
import {access, accessFile, writeFile} from '../util/fsUtil'
import {success} from '../util/commonUtil'
import * as fs from 'fs'
import * as readline from 'readline'
import {Receiver} from './resolver'
import * as path from 'path'

const ERROR_PATH = '2. 生成 schema.json: '

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const defaultOption: option = {
}
}

export type optionTuple = [option, any]
export type optionTuple<T> = [option, T]

const mock = function (option: option = {}): void {
Promise.resolve({...defaultOption, ...option})
Expand All @@ -58,8 +58,8 @@ const mock = function (option: option = {}): void {
// 4. 开启本地服务
.then(startServer)
.catch((e: Error) => {
error(e.message)
error(e.stack, false)
// error(e.message)
error(e.message, true)
process.exit(-1)
})
}
Expand Down
7 changes: 4 additions & 3 deletions src/tasks/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { writeFile, accessFile, access } from '../util/fsUtil'
import {optionTuple} from '../index'
import { success } from '../util/commonUtil'
import { generateSingleData } from '../core/generateSingleData'
import * as fs from 'fs'

const ENTRY = 'Result'
const ERROR_PATH = '3. 生成db.json: '
const DB_JSON_FILE = './db.json'

export function generateData ([option, schemaPath]: optionTuple, force: boolean = false): Promise<optionTuple> {
const dataPath = path.resolve(schemaPath, '..', DB_JSON_FILE)
export function generateData ([option, schemaPath]: optionTuple<fs.PathLike>, force: boolean = false): Promise<optionTuple<fs.PathLike>> {
const dataPath = path.resolve(schemaPath as string, '..', DB_JSON_FILE)
if (!force) {
return accessFile(DB_JSON_FILE)
.then(([exists]: access) => {
Expand All @@ -23,7 +24,7 @@ export function generateData ([option, schemaPath]: optionTuple, force: boolean
} else {
return _generateData()
}
function _generateData(): Promise<optionTuple> {
function _generateData(): Promise<optionTuple<fs.PathLike>> {
return Promise.resolve()
.then(() => generateSingleData(schemaPath, ENTRY))
.then(([result, name]) => {
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/generateSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as path from 'path'
import * as fs from 'fs'
import { generateSingleSchema } from '../core/generateSingleSchema'
import { optionTuple } from '../index'

const SCHEMA_FILE = './schema.json'

export function generateSchema ([option]: optionTuple): Promise<optionTuple> {
export function generateSchema ([option, [swaggerFiles, schemaFiles]]: optionTuple<[string[], string[]]>): Promise<optionTuple<fs.PathLike>> {
const { schemaOption } = option
const { schemaPath } = schemaOption!
const SCHEMA_FILE_PATH = path.resolve(schemaPath as string, SCHEMA_FILE)
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {success} from '../util/commonUtil'

const ERROR_PATH = '1. 加载 config: '

export function loadConfig (option: option = {}): Promise<optionTuple> {
export function loadConfig (option: option = {}): Promise<optionTuple<undefined>> {
const explorer = cosmiconfig(name)
return explorer.search()
.then(result => {
Expand All @@ -28,6 +28,6 @@ export function loadConfig (option: option = {}): Promise<optionTuple> {
throw new Error(ERROR_PATH + '不存在 mockPath 字段')
}
success(`${ERROR_PATH} 成功解析配置文件`)
return [mergeOption, option]
return [mergeOption, undefined]
})
}
28 changes: 24 additions & 4 deletions src/tasks/loadDir.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import * as fs from 'fs'
import * as path from 'path'
import {optionTuple} from '../index'
import { mkdir, readdir } from '../util/fsUtil'

const SWAGGER_PATH = './swagger'
const SCHEMA_PATH = './schema'

export function loadDir([option]: optionTuple): Promise<optionTuple> {

export function loadDir([option]: optionTuple<undefined>): Promise<optionTuple<[string[], string[]]>> {
const { baseOption } = option
const { mockPath } = baseOption!
const mockRealPath = path.join(process.cwd(), mockPath!)
const swaggerDirPath = path.resolve(mockRealPath, SWAGGER_PATH)
const schemaDirPath = path.resolve(mockRealPath, SCHEMA_PATH)
return Promise.resolve()
.then(() => {
fs.mkdirSync(path.join(process.cwd(), mockPath!))
// 尝试去创建一下目录
const mkSwaggerDirPromise = mkdir(swaggerDirPath)
const mkSchemaDirPromise = mkdir(schemaDirPath)
return Promise.all([mkSwaggerDirPromise, mkSchemaDirPromise])
})
.then(() => {
return [option, undefined]
// 不管这个文件夹到底有没有创建成功,直接去看里面有没有内容
const swaggerFilePromise = readdir(swaggerDirPath)
const schemaFilePromise = readdir(schemaDirPath)
return Promise.all([swaggerFilePromise, schemaFilePromise])
})
.then(([swaggerFiles, schemaFiles]) => {
if (!schemaFiles.length && !swaggerFiles.length) {
// 两个文件都没有
throw new Error(`${schemaDirPath}目录下没有schema文件,并且${swaggerDirPath}目录下也没有 swagger 文件,尝试在${swaggerDirPath}目录下添加swagger文件之后重试`)
} else {
return [option, [swaggerFiles, schemaFiles]]
}
})
}
3 changes: 2 additions & 1 deletion src/tasks/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as jsonServer from 'json-server'
import { optionTuple } from '../index'
import { success } from '../util/commonUtil'
import * as fs from 'fs'

export const startServer = ([option, dataPath]: optionTuple) => {
export const startServer = ([option, dataPath]: optionTuple<fs.PathLike>) => {
const { serverOption } = option
const { port } = serverOption!
const app = jsonServer.create()
Expand Down
3 changes: 1 addition & 2 deletions src/util/commonUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export const success = (text: string, withTag: boolean = false): void => {

export const error = (text: string = '', withTag: boolean = true): void => {
if (withTag) {
console.log(SPACE, chalk.bgRed.rgb(255, 255, 255)('ERROR'), '\n')
console.log('\n')
console.log(SPACE, chalk.bgRed.rgb(255, 255, 255)('ERROR'))
}
console.log(SPACE, chalk.red(text))
}
Expand Down
23 changes: 23 additions & 0 deletions src/util/fsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs'
import {rejects} from "assert";

export const writeFile = (path: fs.PathLike, data: string) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -46,6 +47,28 @@ export const accessFile = (path: fs.PathLike) => {
})
}

export const mkdir = (path: fs.PathLike) => {
return new Promise<[boolean, any]>(resolve => {
fs.mkdir(path, {recursive: true}, err => {
if (err) {
return resolve([false, err.message])
}
return resolve([true, undefined])
})
})
}

export const readdir = (path: fs.PathLike) => {
return new Promise<string[]>((resolve, reject) => {
fs.readdir(path, ((err, files) => {
if (err) {
reject(err)
}
resolve(files)
}))
})
}

export const fsStats = (path: fs.PathLike): fs.Stats => {
return fs.statSync(path)
}
Expand Down

0 comments on commit 43d89bd

Please sign in to comment.