@@ -5,7 +5,7 @@ import Command from '../core/base';
5
5
import { ValidationError } from '../core/errors/validation-error' ;
6
6
import { load } from '../core/models/SpecificationFile' ;
7
7
import { SpecificationFileNotFound } from '../core/errors/specification-file' ;
8
- import { convert , convertOpenAPI } from '@asyncapi/converter' ;
8
+ import { convert , convertOpenAPI , convertPostman } from '@asyncapi/converter' ;
9
9
import type { AsyncAPIConvertVersion , OpenAPIConvertVersion } from '@asyncapi/converter' ;
10
10
import { cyan , green } from 'picocolors' ;
11
11
@@ -16,7 +16,9 @@ import { convertFlags } from '../core/flags/convert.flags';
16
16
const latestVersion = Object . keys ( specs . schemas ) . pop ( ) as string ;
17
17
18
18
export default class Convert extends Command {
19
- static description = 'Convert asyncapi documents older to newer versions or OpenAPI documents to AsyncAPI' ;
19
+ static specFile : any ;
20
+ static metricsMetadata : any = { } ;
21
+ static description = 'Convert asyncapi documents older to newer versions or OpenAPI/postman-collection documents to AsyncAPI' ;
20
22
21
23
static flags = convertFlags ( latestVersion ) ;
22
24
@@ -37,47 +39,86 @@ export default class Convert extends Command {
37
39
this . metricsMetadata . to_version = flags [ 'target-version' ] ;
38
40
39
41
// Determine if the input is OpenAPI or AsyncAPI
40
- const specJson = this . specFile . toJson ( ) ;
41
42
const isOpenAPI = flags [ 'format' ] === 'openapi' ;
42
43
const isAsyncAPI = flags [ 'format' ] === 'asyncapi' ;
43
44
44
45
// CONVERSION
45
- if ( isOpenAPI ) {
46
- convertedFile = convertOpenAPI ( this . specFile . text ( ) , specJson . openapi as OpenAPIConvertVersion , {
47
- perspective : flags [ 'perspective' ] as 'client' | 'server'
48
- } ) ;
49
- this . log ( `🎉 The OpenAPI document has been successfully converted to AsyncAPI version ${ green ( flags [ 'target-version' ] ) } !` ) ;
50
- } else if ( isAsyncAPI ) {
51
- convertedFile = convert ( this . specFile . text ( ) , flags [ 'target-version' ] as AsyncAPIConvertVersion ) ;
52
- if ( this . specFile . getFilePath ( ) ) {
53
- this . log ( `🎉 The ${ cyan ( this . specFile . getFilePath ( ) ) } file has been successfully converted to version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
54
- } else if ( this . specFile . getFileURL ( ) ) {
55
- this . log ( `🎉 The URL ${ cyan ( this . specFile . getFileURL ( ) ) } has been successfully converted to version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
56
- }
57
- }
58
-
59
- if ( typeof convertedFile === 'object' ) {
60
- convertedFileFormatted = JSON . stringify ( convertedFile , null , 4 ) ;
61
- } else {
62
- convertedFileFormatted = convertedFile ;
63
- }
64
-
65
- if ( flags . output ) {
66
- await fPromises . writeFile ( `${ flags . output } ` , convertedFileFormatted , { encoding : 'utf8' } ) ;
67
- } else {
68
- this . log ( convertedFileFormatted ) ;
69
- }
46
+ convertedFile = this . handleConversion ( isOpenAPI , isAsyncAPI , flags ) ;
47
+
48
+ // Handle file output or log the result
49
+ convertedFileFormatted = this . formatConvertedFile ( convertedFile ) ;
50
+ await this . handleOutput ( flags . output , convertedFileFormatted ) ;
70
51
} catch ( err ) {
71
- if ( err instanceof SpecificationFileNotFound ) {
72
- this . error ( new ValidationError ( {
73
- type : 'invalid-file' ,
74
- filepath : filePath
75
- } ) ) ;
76
- } else if ( this . specFile ?. toJson ( ) . asyncapi > flags [ 'target-version' ] ) {
77
- this . error ( `The ${ cyan ( filePath ) } file cannot be converted to an older version. Downgrading is not supported.` ) ;
78
- } else {
79
- this . error ( err as Error ) ;
80
- }
52
+ this . handleError ( err , filePath ?? 'unknown' , flags ) ;
53
+ }
54
+ }
55
+
56
+ // Helper function to handle conversion logic
57
+ private handleConversion ( isOpenAPI : boolean , isAsyncAPI : boolean , flags : any ) {
58
+ const specJson = this . specFile ?. toJson ( ) ;
59
+ if ( isOpenAPI ) {
60
+ return this . convertOpenAPI ( specJson , flags ) ;
61
+ } else if ( isAsyncAPI ) {
62
+ return this . convertAsyncAPI ( flags ) ;
63
+ }
64
+ return this . convertPostman ( flags ) ;
65
+ }
66
+
67
+ private convertOpenAPI ( specJson : any , flags : any ) {
68
+ const convertedFile = convertOpenAPI ( this . specFile ?. text ( ) ?? '' , specJson . openapi as OpenAPIConvertVersion , {
69
+ perspective : flags [ 'perspective' ] as 'client' | 'server'
70
+ } ) ;
71
+ this . log ( `🎉 The OpenAPI document has been successfully converted to AsyncAPI version ${ green ( flags [ 'target-version' ] ) } !` ) ;
72
+ return convertedFile ;
73
+ }
74
+
75
+ private convertAsyncAPI ( flags : any ) {
76
+ const convertedFile = convert ( this . specFile ?. text ( ) ?? '' , flags [ 'target-version' ] as AsyncAPIConvertVersion ) ;
77
+ if ( this . specFile ?. getFilePath ( ) ) {
78
+ this . log ( `🎉 The ${ cyan ( this . specFile ?. getFilePath ( ) ) } file has been successfully converted to version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
79
+ } else if ( this . specFile ?. getFileURL ( ) ) {
80
+ this . log ( `🎉 The URL ${ cyan ( this . specFile ?. getFileURL ( ) ) } has been successfully converted to version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
81
+ }
82
+ return convertedFile ;
83
+ }
84
+
85
+ private convertPostman ( flags : any ) {
86
+ const convertedFile = convertPostman ( this . specFile ?. text ( ) ?? '' , '3.0.0' , {
87
+ perspective : flags [ 'perspective' ] as 'client' | 'server'
88
+ } ) ;
89
+ if ( this . specFile ?. getFilePath ( ) ) {
90
+ this . log ( `🎉 The ${ cyan ( this . specFile ?. getFilePath ( ) ) } file has been successfully converted to asyncapi of version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
91
+ } else if ( this . specFile ?. getFileURL ( ) ) {
92
+ this . log ( `🎉 The URL ${ cyan ( this . specFile ?. getFileURL ( ) ) } has been successfully converted to asyncapi of version ${ green ( flags [ 'target-version' ] ) } !!` ) ;
93
+ }
94
+ return convertedFile ;
95
+ }
96
+
97
+ // Helper function to format the converted file
98
+ private formatConvertedFile ( convertedFile : any ) {
99
+ return typeof convertedFile === 'object' ? JSON . stringify ( convertedFile , null , 4 ) : convertedFile ;
100
+ }
101
+
102
+ // Helper function to handle output
103
+ private async handleOutput ( outputPath : string | undefined , convertedFileFormatted : string ) {
104
+ if ( outputPath ) {
105
+ await fPromises . writeFile ( `${ outputPath } ` , convertedFileFormatted , { encoding : 'utf8' } ) ;
106
+ } else {
107
+ this . log ( convertedFileFormatted ) ;
108
+ }
109
+ }
110
+
111
+ // Helper function to handle errors
112
+ private handleError ( err : any , filePath : string , flags : any ) {
113
+ if ( err instanceof SpecificationFileNotFound ) {
114
+ this . error ( new ValidationError ( {
115
+ type : 'invalid-file' ,
116
+ filepath : filePath
117
+ } ) ) ;
118
+ } else if ( this . specFile ?. toJson ( ) . asyncapi > flags [ 'target-version' ] ) {
119
+ this . error ( `The ${ cyan ( filePath ) } file cannot be converted to an older version. Downgrading is not supported.` ) ;
120
+ } else {
121
+ this . error ( err as Error ) ;
81
122
}
82
123
}
83
124
}
0 commit comments