-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
540e162
commit c9c33af
Showing
3 changed files
with
69 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters