Skip to content

Commit

Permalink
feat: 减少db.json文件的重复生成
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jul 10, 2019
1 parent 682763c commit 03746e1
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions src/tasks/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@ import { success } from '../util/commonUtil'

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> {
return Promise.resolve()
// todo: 解决db.json文件的重复多次生成
.then(() => readJsonFile(schemaPath))
.then((schema): any => {
if (!schema[ENTRY]) {
throw new Error(ERROR_PATH + '不存在 Result 字段,无法解析')
}
return parse(schema[ENTRY] as {[key: string]: Ischema}, schema)
})
.then(result => {
const dataPath = path.resolve(schemaPath, '..', './db.json')
const data = {
result: result
}
const writePromise = writeFile(dataPath, JSON.stringify(data))
return Promise.all([dataPath, writePromise])
})
.then(([dataPath, _]) => {
success(`${ERROR_PATH}成功生成数据文件:${dataPath}`,)
return [
option,
dataPath
]
})
const dataPath = path.resolve(schemaPath, '..', DB_JSON_FILE)
if (!force) {
return accessFile(DB_JSON_FILE)
.then(([exists]: access) => {
if (!exists) {
return _generateData()
} else {
success(`${DB_JSON_FILE}文件已经存在,跳过生成 db.json 文件步骤`)
return [option, dataPath]
}
})
} else {
return _generateData()
}
function _generateData(): Promise<optionTuple> {
return Promise.resolve()
.then(() => readJsonFile(schemaPath))
.then((schema): any => {
if (!schema[ENTRY]) {
throw new Error(ERROR_PATH + '不存在 Result 字段,无法解析')
}
return parse(schema[ENTRY] as {[key: string]: Ischema}, schema)
})
.then(result => {
const data = {
result: result
}
const writePromise = writeFile(dataPath, JSON.stringify(data))
return Promise.all([dataPath, writePromise])
})
.then(([dataPath]) => {
success(`${ERROR_PATH}成功生成数据文件:${dataPath}`,)
return [
option,
dataPath
]
})
}
}

0 comments on commit 03746e1

Please sign in to comment.