11#!/usr/bin/env node
22
33/**
4- * Copyright JS Foundation and other contributors, http://js.foundation
4+ * Copyright OpenJS Foundation and other contributors
55 *
66 * Licensed under the Apache License, Version 2.0 (the "License");
77 * you may not use this file except in compliance with the License.
1818
1919var fs = require ( 'fs' ) ;
2020var path = require ( 'path' ) ;
21- var request = require ( 'request' ) ;
22- var yamljs = require ( 'yamljs' ) ;
21+
2322var argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
2423var colors = require ( 'colors' ) ;
25- var Converter = require ( 'api-spec-converter' ) ;
2624var nodegen = require ( '../lib/nodegen.js' ) ;
2725
2826// Command options
@@ -39,7 +37,8 @@ var data = {
3937 category : argv . category || argv . c ,
4038 icon : argv . icon ,
4139 color : argv . color ,
42- dst : argv . output || argv . o || '.'
40+ dst : argv . output || argv . o || '.' ,
41+ lang : argv . lang
4342} ;
4443
4544function help ( ) {
@@ -108,115 +107,42 @@ if (argv.help || argv.h) {
108107} else if ( argv . v ) {
109108 version ( ) ;
110109} else {
110+ var promise ;
111111 var sourcePath = argv . _ [ 0 ] ;
112112 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- } ) ;
113+ data . src = sourcePath ;
114+ if ( argv . wottd || / \. j s o n l d $ / . test ( sourcePath ) ) {
115+ // Explicitly a Web Of Things request
116+ promise = nodegen . WebOfThingsGenerator ( data , options ) ;
117+ } else if ( / ^ h t t p s ? : / . test ( sourcePath ) || / .y a m l $ / . test ( sourcePath ) ) {
118+ // URL/yaml -> swagger
119+ promise = nodegen . SwaggerNodeGenerator ( data , options ) ;
120+ } else if ( / \. j s o n $ / . test ( sourcePath ) ) {
121+ // JSON could be a Function node, or Swagger
122+ var content = JSON . parse ( fs . readFileSync ( sourcePath ) ) ;
123+ if ( Array . isArray ( content ) ) {
124+ data . src = content ;
125+ promise = nodegen . FunctionNodeGenerator ( data , options ) ;
126+ } else {
127+ promise = nodegen . SwaggerNodeGenerator ( data , options ) ;
172128 }
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 ) {
129+ } else if ( / \. j s $ / . test ( sourcePath ) ) {
130+ // .js -> Function node
131+ promise = nodegen . FunctionNodeGenerator ( data , options ) ;
132+ } else {
133+ console . error ( 'error: Unsupported file type' ) ;
134+ help ( ) ;
135+ return ;
136+ }
137+ if ( promise ) {
138+ promise . then ( function ( result ) {
212139 console . log ( 'Success: ' + result ) ;
213140 } ) . catch ( function ( error ) {
214141 console . log ( 'Error: ' + error ) ;
142+ console . log ( error . stack ) ;
215143 } ) ;
216- } else {
217- console . error ( 'error: Unsupported file type' ) ;
218144 }
219145 } else {
220146 help ( ) ;
221147 }
222- }
148+ }
0 commit comments