@@ -59,7 +59,7 @@ class ParserService {
5959 parseFiles ( req , res ) {
6060 const file = req . files [ 0 ] ;
6161 const dataType = req . body . dataType ;
62- this . getFileLocation ( file )
62+ this . getFileLocation ( file , dataType )
6363 . then ( ( file ) => {
6464 file = `file://${ file } ` ;
6565 return this . processFile ( file , dataType ) ;
@@ -88,13 +88,15 @@ class ParserService {
8888 * @return {Promise }
8989 */
9090 processFile ( file , from ) {
91- let parser ;
91+ let type ;
9292 switch ( from ) {
93- case 'raml' : parser = ramlParser ; break ;
94- case 'raml8' : parser = ramlParser8 ; break ;
95- case 'oas' : parser = openAPIParser ; break ;
96- case 'amf' : parser = apiModelParser ; break ;
93+ case 'raml' : type = 'RAML 1.0' ; break ;
94+ case 'raml8' : type = 'RAML 0.8' ; break ;
95+ case 'oas' : type = 'OAS 2.0' ; break ;
96+ case 'amf' : type = 'AMF Graph' ; break ;
9797 }
98+ console . log ( 'Parsing API' ) ;
99+ const parser = amf . Core . parser ( type , 'application/yaml' ) ;
98100 console . log ( 'Processing' , file , ', format' , from ) ;
99101 return parser . parseFileAsync ( file )
100102 . then ( ( doc ) => this . generateModel ( doc , from ) ) ;
@@ -125,6 +127,7 @@ class ParserService {
125127 * @return {Promise }
126128 */
127129 generateModel ( doc , from ) {
130+ console . log ( 'Generating model' ) ;
128131 let resolver ;
129132 switch ( from ) {
130133 case 'raml' : resolver = amf . Core . resolver ( 'RAML 1.0' ) ; break ;
@@ -142,9 +145,10 @@ class ParserService {
142145 * Gets file contents
143146 *
144147 * @param {Object } file
148+ * @param {String } dataType Expected data type.
145149 * @return {Promise }
146150 */
147- getFileLocation ( file ) {
151+ getFileLocation ( file , dataType ) {
148152 this . tmpobj = tmp . dirSync ( ) ;
149153 return new Promise ( ( resolve , reject ) => {
150154 if ( file . mimetype === 'application/zip' ) {
@@ -156,7 +160,7 @@ class ParserService {
156160 } ) ;
157161 extractor . on ( 'close' , ( ) => {
158162 this . _removeZipMainFolder ( this . tmpobj . name )
159- . then ( ( ) => this . _findApiFile ( this . tmpobj . name ) )
163+ . then ( ( ) => this . _findApiFile ( this . tmpobj . name , dataType ) )
160164 . then ( ( file ) => resolve ( path . join ( this . tmpobj . name , file ) ) )
161165 . catch ( ( ) => resolve ( path . join ( this . tmpobj . name , 'api.raml' ) ) ) ;
162166 } ) ;
@@ -217,26 +221,38 @@ class ParserService {
217221 * If not then, if any RAML file exists it points to first raml file.
218222 * If not then,it returns `api.raml`
219223 * @param {String } destination Path where to look for the files.
224+ * @param {String } dataType API data type.
220225 * @return {Promise<String> }
221226 */
222- _findApiFile ( destination ) {
227+ _findApiFile ( destination , dataType ) {
223228 return fs . readdir ( destination )
224229 . then ( ( items ) => {
225- const def = 'api.raml' ;
230+ const defs = [ ] ;
231+ const exts = [ ] ;
232+ if ( dataType === 'raml' || dataType === 'raml8' ) {
233+ defs [ defs . length ] = 'api.raml' ;
234+ exts [ exts . length ] = '.raml' ;
235+ }
236+ if ( dataType === 'oas' ) {
237+ defs [ defs . length ] = 'api.yaml' ;
238+ defs [ defs . length ] = 'api.json' ;
239+ exts [ exts . length ] = '.json' ;
240+ exts [ exts . length ] = '.yaml' ;
241+ }
226242 const _files = [ ] ;
227243 for ( let i = 0 ; i < items . length ; i ++ ) {
228244 let lower = items [ i ] . toLowerCase ( ) ;
229- if ( lower === def ) {
230- return def ;
245+ if ( defs . indexOf ( lower ) !== - 1 ) {
246+ return items [ i ] ;
231247 }
232- if ( path . extname ( lower ) === '.raml' ) {
248+ if ( exts . indexOf ( path . extname ( lower ) ) !== - 1 ) {
233249 _files . push ( items [ i ] ) ;
234250 }
235251 }
236252 if ( _files . length ) {
237253 return _files [ 0 ] ;
238254 }
239- return def ;
255+ return defs [ 0 ] ;
240256 } ) ;
241257 }
242258}
0 commit comments