@@ -3,17 +3,35 @@ import fs from "fs";
33import _ from "lodash" ;
44import path from "path" ;
55import { authParamsConfig , retrieveSpec } from "../src/plugins/openApi/parse" ;
6+ import postManToOpenApi from "postman-to-openapi" ;
67import { program } from "commander" ;
78
8- async function gen ( name : string , specUrl : string , pluginId ?: string ) {
9+ interface Options {
10+ name : string ;
11+ url : string ;
12+ postMan : boolean ;
13+ postManCollectionFile : string ;
14+ id ?: string ;
15+ }
16+
17+ async function gen ( options : Options ) {
18+ const { postMan, postManCollectionFile, name, url : specUrl , id : pluginId } = options ;
919 const id = pluginId ?? _ . camelCase ( name ) ;
1020 const pluginDir = path . join ( path . dirname ( __dirname ) , "src/plugins" , id ) ;
1121 const pluginEntryFile = path . join ( pluginDir , "index.ts" ) ;
1222 const pluginSpecYamlFile = path . join ( pluginDir , `${ id } .spec.yaml` ) ;
1323 const pluginSpecJsonFile = path . join ( pluginDir , `${ id } .spec.json` ) ;
24+ const pluginDefaultCollectionFile = path . join ( pluginDir , `${ id } .collection.json` ) ;
1425
26+ console . info ( ) ;
1527 console . info ( "start generate plugin, id:" , id , "name:" , name ) ;
1628
29+ if ( postMan ) {
30+ console . info ( "is PostMan Collection start transforming..." ) ;
31+ const collection = postManCollectionFile ?? pluginDefaultCollectionFile ;
32+ await postManToOpenApi ( collection , pluginSpecYamlFile , { defaultTag : "General" } ) ;
33+ }
34+
1735 if ( ! fs . existsSync ( pluginDir ) ) {
1836 fs . mkdirSync ( pluginDir , { recursive : true } ) ;
1937 console . info ( `plugin dir ${ id } created.` ) ;
@@ -72,6 +90,7 @@ async function gen(name: string, specUrl: string, pluginId?: string) {
7290 const code = compiledTemplate ( data ) ;
7391 fs . writeFileSync ( pluginEntryFile , code ) ;
7492 console . info ( "success generate plugin:" , pluginDir ) ;
93+ console . info ( ) ;
7594}
7695
7796const plugins = [
@@ -81,14 +100,15 @@ const plugins = [
81100
82101program
83102 . option ( "--post-man" )
103+ . option ( "-f, --collection-file [postman collection file path]" )
84104 . option ( "-n, --name <char>" )
85105 . option ( "-i, --id [plugin id]" )
86106 . option ( "--url [spec-download-url]" ) ;
87107
88108program . parse ( ) ;
89109
90- const { name, url, postMan, pluginId } = program . opts ( ) ;
110+ const options = program . opts < Options > ( ) ;
111+
112+ console . info ( options ) ;
91113
92- console . info ( ) ;
93- gen ( name , url , pluginId ) ;
94- console . info ( ) ;
114+ gen ( options ) ;
0 commit comments