@@ -14,7 +14,13 @@ const swagger2openapi = require('swagger2openapi');
1414const stools = require ( 'swagger-tools' ) ;
1515const admzip = require ( 'adm-zip' ) ;
1616
17- const processor = require ( './index.js' ) ;
17+ const processor = require ( './local.js' ) ;
18+ const remote = require ( './remote.js' ) ;
19+
20+ async function list ( provider ) {
21+ await remote . list ( provider ) ;
22+ process . exit ( 1 ) ;
23+ }
1824
1925var argv = require ( 'yargs' )
2026 . usage ( 'cg [options] {[path]configName} {openapi-definition}' )
@@ -27,6 +33,8 @@ var argv = require('yargs')
2733 . boolean ( 'lint' )
2834 . alias ( 'l' , 'lint' )
2935 . describe ( 'lint' , 'Lint input definition' )
36+ . string ( 'list' )
37+ . describe ( 'list' , 'List available templates for provider (og or sc)' )
3038 . string ( 'output' )
3139 . alias ( 'o' , 'output' )
3240 . describe ( 'output' , 'Specify output directory' )
@@ -46,14 +54,21 @@ var argv = require('yargs')
4654 . version ( )
4755 . argv ;
4856
57+ if ( argv . list ) {
58+ list ( argv . list ) ;
59+ }
60+
4961let configStr = argv . _ [ 0 ] || 'nodejs' ;
5062let configName = path . basename ( configStr ) ;
63+ let remoteConfig = configName . indexOf ( ':' ) > - 1 ;
5164let configPath = path . dirname ( configStr ) ;
5265if ( ! configPath || ( configPath === '.' ) ) configPath = './configs' ;
5366let configFile = path . resolve ( configPath , configName ) + '.json' ;
5467let config = require ( configFile ) ;
5568let defName = argv . _ [ 1 ] || './defs/petstore3.json' ;
5669
70+ let finish = remoteConfig ? finishRemote : finishLocal ;
71+
5772config . outputDir = argv . output ;
5873config . templateDir = argv . templates ;
5974
@@ -70,7 +85,7 @@ function zipFile(filename,contents,encoding) {
7085 zipFiles [ filename ] = contents ;
7186}
7287
73- function finish ( err , result ) {
88+ function finishLocal ( err , result ) {
7489 if ( argv . zip ) {
7590 // create archive
7691 var zip = new admzip ( ) ;
@@ -84,6 +99,24 @@ function finish(err,result) {
8499 }
85100}
86101
102+ function finishRemote ( err , result ) {
103+ if ( argv . zip ) {
104+ // just save the zip file
105+ }
106+ else {
107+ // unpack the zip file
108+ }
109+ }
110+
111+ function despatch ( obj , config , configName , callback ) {
112+ if ( remoteConfig ) {
113+ remote . main ( obj , config , configName , callback ) ;
114+ }
115+ else {
116+ processor . main ( obj , config , configName , callback ) ;
117+ }
118+ }
119+
87120function convert20 ( obj ) {
88121 if ( argv . verbose ) console . log ( 'Converting OpenAPI 2.0 definition' ) ;
89122 swagger2openapi . convertObj ( obj , { patch :true , warnOnly :true , direct :true } , function ( err , openapi ) {
@@ -92,7 +125,7 @@ function convert20(obj){
92125 }
93126 else {
94127 config . defaults . swagger = obj ;
95- processor . main ( openapi , config , configName , finish ) ;
128+ despatch ( openapi , config , configName , finish ) ;
96129 }
97130 } ) ;
98131}
@@ -176,14 +209,19 @@ function main(s) {
176209 if ( argv . verbose ) console . log ( 'Loaded definition ' + defName ) ;
177210
178211 if ( o && o . openapi ) {
179- processor . main ( o , config , configName , finish ) ;
212+ despatch ( o , config , configName , finish ) ;
180213 }
181214 else {
182215 if ( o && o . swaggerVersion && o . swaggerVersion === '1.2' ) {
183216 convert12 ( o ) ;
184217 }
185218 else if ( o && o . swagger && o . swagger === '2.0' ) {
186- convert20 ( o ) ;
219+ if ( remoteConfig ) {
220+ despatch ( o , config , configName , finish ) ;
221+ }
222+ else {
223+ convert20 ( o ) ;
224+ }
187225 }
188226 else {
189227 console . error ( 'Unrecognised OpenAPI/Swagger version' ) ;
0 commit comments