1919var fs = require ( 'fs' ) ;
2020var path = require ( 'path' ) ;
2121var request = require ( 'request' ) ;
22- var yamljs = require ( 'yamljs' ) ;
22+
2323var argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
2424var colors = require ( 'colors' ) ;
25- var Converter = require ( 'api-spec-converter' ) ;
2625var nodegen = require ( '../lib/nodegen.js' ) ;
2726
2827// Command options
@@ -39,7 +38,8 @@ var data = {
3938 category : argv . category || argv . c ,
4039 icon : argv . icon ,
4140 color : argv . color ,
42- dst : argv . output || argv . o || '.'
41+ dst : argv . output || argv . o || '.' ,
42+ lang : argv . lang
4343} ;
4444
4545function help ( ) {
@@ -108,115 +108,42 @@ if (argv.help || argv.h) {
108108} else if ( argv . v ) {
109109 version ( ) ;
110110} else {
111+ var promise ;
111112 var sourcePath = argv . _ [ 0 ] ;
112113 if ( sourcePath ) {
113- if ( ! argv . wottd && ( sourcePath . startsWith ( 'http://' ) || sourcePath . startsWith ( 'https://' ) ) ) {
114- request ( sourcePath , function ( error , response , body ) {
115- if ( ! error ) {
116- data . src = JSON . parse ( body ) ;
117- Converter . convert ( {
118- from : data . src . openapi && data . src . openapi . startsWith ( '3.0' ) ? 'openapi_3' : 'swagger_2' ,
119- to : 'swagger_2' ,
120- source : data . src ,
121- } ) . then ( function ( convertedData ) {
122- data . src = convertedData . spec ;
123- nodegen . swagger2node ( data , options ) . then ( function ( result ) {
124- console . log ( 'Success: ' + result ) ;
125- } ) . catch ( function ( error ) {
126- console . log ( 'Error: ' + error ) ;
127- } ) ;
128- } ) ;
129- } else {
130- console . error ( error ) ;
131- }
132- } ) ;
133- } else if ( argv . wottd && ( sourcePath . startsWith ( 'http://' ) || sourcePath . startsWith ( 'https://' ) ) ) {
134- const req = {
135- url : sourcePath ,
136- }
137- if ( argv . lang ) {
138- req . headers = {
139- 'Accept-Language' : argv . lang
140- }
141- }
142- request ( req , function ( error , response , body ) {
143- if ( ! error ) {
144- data . src = JSON . parse ( skipBom ( body ) ) ;
145- nodegen . wottd2node ( data , options ) . then ( function ( result ) {
146- console . log ( 'Success: ' + result ) ;
147- } ) . catch ( function ( error ) {
148- console . log ( 'Error: ' + error ) ;
149- } ) ;
150- } else {
151- console . error ( error ) ;
152- }
153- } ) ;
154- } else if ( sourcePath . endsWith ( '.json' ) && ! argv . wottd ) {
155- data . src = JSON . parse ( fs . readFileSync ( sourcePath ) ) ;
156- // if it's a .json flow file with one function node in...
157- if ( Array . isArray ( data . src ) && data . src [ 0 ] . hasOwnProperty ( "type" ) && data . src [ 0 ] . type == "function" ) {
158- var f = data . src [ 0 ] ;
159- if ( ! f . name || f . name . length == 0 ) { console . log ( 'Error: No function name supplied.' ) ; return ; }
160- data . name = f . name . toLowerCase ( ) ;
161- data . icon = f . icon ;
162- data . info = f . info ;
163- data . outputs = f . outputs ;
164- data . inputLabels = f . inputLabels ;
165- data . outputLabels = f . outputLabels ;
166- data . src = Buffer . from ( f . func ) ;
167- nodegen . function2node ( data , options ) . then ( function ( result ) {
168- console . log ( 'Success: ' + result ) ;
169- } ) . catch ( function ( error ) {
170- console . log ( 'Error: ' + error ) ;
171- } ) ;
114+ data . src = sourcePath ;
115+ if ( argv . wottd || / \. j s o n l d $ / . test ( sourcePath ) ) {
116+ // Explicitly a Web Of Things request
117+ promise = nodegen . wottd2node ( data , options ) ;
118+ } else if ( / ^ h t t p s ? : / . test ( sourcePath ) || / .y a m l $ / . test ( sourcePath ) ) {
119+ // URL/yaml -> swagger
120+ promise = nodegen . swagger2node ( data , options ) ;
121+ } else if ( / \. j s o n $ / . test ( sourcePath ) ) {
122+ // JSON could be a Function node, or Swagger
123+ var content = JSON . parse ( fs . readFileSync ( sourcePath ) ) ;
124+ if ( Array . isArray ( content ) ) {
125+ data . src = content ;
126+ promise = nodegen . function2node ( data , options ) ;
127+ } else {
128+ promise = nodegen . swagger2node ( data , options ) ;
172129 }
173- else {
174- Converter . convert ( {
175- from : data . src . openapi && data . src . openapi . startsWith ( '3.0' ) ? 'openapi_3' : 'swagger_2' ,
176- to : 'swagger_2' ,
177- source : data . src ,
178- } ) . then ( function ( convertedData ) {
179- data . src = convertedData . spec ;
180- nodegen . swagger2node ( data , options ) . then ( function ( result ) {
181- console . log ( 'Success: ' + result ) ;
182- } ) . catch ( function ( error ) {
183- console . log ( 'Error: ' + error ) ;
184- } ) ;
185- } ) ;
186- }
187- } else if ( sourcePath . endsWith ( '.yaml' ) ) {
188- data . src = yamljs . load ( sourcePath ) ;
189- console . log ( JSON . stringify ( data . src , null , 4 ) ) ; // hoge
190- Converter . convert ( {
191- from : data . src . openapi && data . src . openapi . startsWith ( '3.0' ) ? 'openapi_3' : 'swagger_2' ,
192- to : 'swagger_2' ,
193- source : data . src ,
194- } ) . then ( function ( convertedData ) {
195- data . src = convertedData . spec ;
196- nodegen . swagger2node ( data , options ) . then ( function ( result ) {
197- console . log ( 'Success: ' + result ) ;
198- } ) . catch ( function ( error ) {
199- console . log ( 'Error: ' + error ) ;
200- } ) ;
201- } ) ;
202- } else if ( sourcePath . endsWith ( '.js' ) ) {
203- data . src = fs . readFileSync ( sourcePath ) ;
204- nodegen . function2node ( data , options ) . then ( function ( result ) {
205- console . log ( 'Success: ' + result ) ;
206- } ) . catch ( function ( error ) {
207- console . log ( 'Error: ' + error ) ;
208- } ) ;
209- } else if ( sourcePath . endsWith ( '.jsonld' ) || argv . wottd ) {
210- data . src = JSON . parse ( skipBom ( fs . readFileSync ( sourcePath ) ) ) ;
211- nodegen . wottd2node ( data , options ) . then ( function ( result ) {
130+ } else if ( / \. j s $ / . test ( sourcePath ) ) {
131+ // .js -> Function node
132+ promise = nodegen . function2node ( data , options ) ;
133+ } else {
134+ console . error ( 'error: Unsupported file type' ) ;
135+ help ( ) ;
136+ return ;
137+ }
138+ if ( promise ) {
139+ promise . then ( function ( result ) {
212140 console . log ( 'Success: ' + result ) ;
213141 } ) . catch ( function ( error ) {
214142 console . log ( 'Error: ' + error ) ;
143+ console . log ( error . stack ) ;
215144 } ) ;
216- } else {
217- console . error ( 'error: Unsupported file type' ) ;
218145 }
219146 } else {
220147 help ( ) ;
221148 }
222- }
149+ }
0 commit comments