UNPKG

pb-to-ts-api

Version:

help you transfer protobuf file to your custom d.ts and api request method

34 lines (31 loc) 1.57 kB
import { Express } from 'express'; import fs from 'fs'; import path from 'path'; import { IOptions } from '../interfaces/IOptions'; import { getPbjsFile } from './getPbjsFile'; import { getPbtsFile } from './getPbtsFile'; import { generateMockRoute } from './mockServer'; import { saveApiFile } from './saveApiFile'; import { saveJSONSchemaFile } from './saveJSONSchemaFile'; import { saveMockJSONFile } from './saveMockJSONFile'; import { saveTypeScriptDefineFile } from './saveTypeScriptDefineFile'; /** * 转换protobuf定义文件为ts定义文件和api请求文件 * @param {String} filePath protobuf定义文件的路径 * @param {Express} mockServer mockServer对象,是一个express实例化的对象 * @param {Object} options 用户自定义配置 */ export async function transferTSFile(filePath: string, mockServer: Express, options: IOptions) { const pbjsFilePath = await getPbjsFile(filePath, options); const pbtsFilePath = await getPbtsFile(pbjsFilePath, options); await fs.promises.unlink(pbjsFilePath); await saveTypeScriptDefineFile(pbtsFilePath, options); await saveApiFile(pbtsFilePath, options); const jsonSchemaFilePath = await saveJSONSchemaFile(pbtsFilePath); const mockFilePath = await saveMockJSONFile(jsonSchemaFilePath); console.log(`success generate ${filePath} to ${path.resolve(options.folder, filePath)}.d.ts and ${path.resolve(options.folder, filePath)}.ts`); if (options.mock && mockServer) { console.log('begin open mock server'); await generateMockRoute(mockFilePath, mockServer, options); } }