Skip to content

Commit

Permalink
feat: 优化服务启动显示
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jul 13, 2019
1 parent 050884d commit a8fab3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/tasks/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {Ischema} from '../core/resolver'
const ERROR_PATH = '3. 生成db.json: '
const DB_JSON_FILE = './db.json'

export function generateData ([option, schemaPaths]: optionTuple<string[]>, force: boolean = true): Promise<optionTuple<string>> {
export function generateData ([option, schemaPaths]: optionTuple<string[]>, force: boolean = true): Promise<optionTuple<[string, string[]]>> {
const schemaNames = schemaPaths.map(schemaFile => path.parse(schemaFile).name)
const dataPath = path.resolve(option.baseOption!.mockPath!, DB_JSON_FILE)
if (!force) {
return accessFile(dataPath)
Expand All @@ -17,14 +18,14 @@ export function generateData ([option, schemaPaths]: optionTuple<string[]>, forc
return _generateData()
} else {
success(`${ERROR_PATH}${dataPath}文件已经存在,跳过生成 db.json 文件步骤`)
return [option, dataPath]
return [option, [dataPath, schemaNames]]
}
})
} else {
return _generateData()
}
// 生成data数据
function _generateData(): Promise<optionTuple<string>> {
function _generateData(): Promise<optionTuple<[string, string[]]>> {
return Promise.resolve()
.then(() => Promise.all(schemaPaths.map(schemaPath => generateSingleData(schemaPath, path.parse(schemaPath).name))))
.then((dataList) => {
Expand All @@ -39,7 +40,7 @@ export function generateData ([option, schemaPaths]: optionTuple<string[]>, forc
success(`${ERROR_PATH}成功生成数据文件:${dataPath}`,)
return [
option,
dataPath
[dataPath, schemaNames]
]
})
}
Expand Down
6 changes: 5 additions & 1 deletion src/tasks/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { optionTuple } from '../index'
import { success } from '../util/commonUtil'
import * as fs from 'fs'

export const startServer = ([option, dataPath]: optionTuple<fs.PathLike>) => {
export const startServer = ([option, [dataPath, schemaNames]]: optionTuple<[string, string[]]>) => {
const { serverOption } = option
const { port } = serverOption!
const app = jsonServer.create()
Expand All @@ -15,5 +15,9 @@ export const startServer = ([option, dataPath]: optionTuple<fs.PathLike>) => {
app.use(router)
app.listen(port, () => {
success(`mock server is start localhost: ${port}`, true)
console.log(' you can access those links: ')
schemaNames.forEach(name => {
success(`http://localhost:${port}/${name}`)
})
})
}

0 comments on commit a8fab3d

Please sign in to comment.