Skip to content

Commit

Permalink
feat: 加入 schema.json文件的判存
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jul 1, 2019
1 parent 540e162 commit c9c33af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
75 changes: 40 additions & 35 deletions src/tasks/generateSchema.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
import * as fs from 'fs'
import * as path from 'path'
import * as readline from 'readline'
import { writeFile } from '../util'
import { writeFile, accessFile, access } from '../util'
import { Receiver } from '../resolver'
import { optionTuple } from '../index'

const SCHEMA_FILE = './schema.json'

export function generateSchema ([option]: optionTuple) {
return new Promise<optionTuple>((resolve, reject) => {
const { swaggerPath, schemaPath } = option
if (!swaggerPath) {
throw new Error('没有接收到 swaggerPath 字段,请检查下')
}
const readStream: fs.ReadStream = fs.createReadStream(swaggerPath)
const rl: readline.Interface = readline.createInterface({
input: readStream
})
const receiver: Receiver = Receiver.instance()
rl.on('line', (line: string) => {
try {
receiver.receive(line)
} catch (e) {
reject(e)
}
})
rl.on('close', () => {
const schemaJson = receiver.getSchemaJson()
if (!schemaPath) {
throw new Error('没有接收到 schemaPath 字段,请检查下')
}
const realPath = path.resolve(schemaPath, './schema.json')
writeFile(realPath, JSON.stringify(schemaJson))
.then(() => {
const schemaFilePath = path.resolve(schemaPath, './schema.json')
console.log('成功创建 schema 文件:%s', schemaFilePath)
resolve([
option,
path.resolve(schemaFilePath)
])
const { swaggerPath, schemaPath } = option
return accessFile(path.resolve(schemaPath as string, SCHEMA_FILE))
.then(([exists, write]: access) => {
return new Promise<optionTuple>((resolve, reject) => {
if (!swaggerPath) {
throw new Error('没有接收到 swaggerPath 字段,请检查下')
}
const readStream: fs.ReadStream = fs.createReadStream(swaggerPath)
const rl: readline.Interface = readline.createInterface({
input: readStream
})
const receiver: Receiver = Receiver.instance()
rl.on('line', (line: string) => {
try {
receiver.receive(line)
} catch (e) {
reject(e)
}
})
.catch((e: Error) => {
reject(e)
rl.on('close', () => {
const schemaJson = receiver.getSchemaJson()
if (!schemaPath) {
throw new Error('没有接收到 schemaPath 字段,请检查下')
}
const realPath = path.resolve(schemaPath, './schema.json')
writeFile(realPath, JSON.stringify(schemaJson))
.then(() => {
const schemaFilePath = path.resolve(schemaPath, './schema.json')
console.log('成功创建 schema 文件:%s', schemaFilePath)
resolve([
option,
path.resolve(schemaFilePath)
])
})
.catch((e: Error) => {
reject(e)
})
})
})
})
})
}
14 changes: 11 additions & 3 deletions src/tasks/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ export function loadConfig (option: option): Promise<optionTuple> {
const explorer = cosmiconfig(name)
return explorer.search()
.then(result => {
let mergeOption: option = {}
if (result) {
return [{
mergeOption = {
...result.config,
...option
}, undefined]
}
} else {
return [option, undefined]
mergeOption = option
}
if (!mergeOption.schemaPath) {
throw new Error('不存在 schemaPath 字段')
}
if (!mergeOption.swaggerPath) {
throw new Error('不存在 swaggerPath 字段')
}
return [mergeOption, option]
})
}
20 changes: 18 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs'

export const writeFile = (path: string, data: string) => {
export const writeFile = (path: fs.PathLike, data: string) => {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, e => {
if (e) {
Expand All @@ -12,7 +12,7 @@ export const writeFile = (path: string, data: string) => {
})
}

export const readJsonFile = (path: string) => {
export const readJsonFile = (path: fs.PathLike) => {
return new Promise<any>((resolve, reject) => {
fs.readFile(path, (e, buf) => {
if (e) {
Expand All @@ -29,3 +29,19 @@ export const readJsonFile = (path: string) => {
})
})
}

// [是否存在, 是否可写]
export type access = [boolean, boolean]

export const accessFile = (path: fs.PathLike) => {
return new Promise<access>(resolve => {
fs.access(path, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
const exists = err.code !== 'ENOENT'
return resolve([exists, !exists])
} else {
return resolve([true, true])
}
})
})
}

0 comments on commit c9c33af

Please sign in to comment.