@@ -87,65 +87,80 @@ export default class Avro extends Command {
87
87
88
88
// tslint:disable-next-line:no-unused
89
89
private toJson ( flags : any , args : any ) {
90
- Utilities . truncateFile ( this , flags . output )
91
- avro . createFileDecoder ( flags . file )
92
- . on ( 'data' , function ( recordStr ) {
90
+ Logger . progressStart ( this , 'Converting Avro To Json' )
91
+ setTimeout ( ( ) => {
92
+ Logger . progressStop ( this , ' Done' )
93
+ Utilities . truncateFile ( this , flags . output )
94
+ avro . createFileDecoder ( flags . file )
95
+ . on ( 'data' , function ( recordStr ) {
93
96
// @ts -ignore
94
- Utilities . appendStringToFile ( this , flags . output , JSON . stringify ( recordStr ) )
95
- } )
96
- Logger . success ( this , `${ chalk . blue ( 'Json' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
97
+ Utilities . appendStringToFile ( this , flags . output , JSON . stringify ( recordStr ) )
98
+ } )
99
+ Logger . success ( this , `${ chalk . blue ( 'Json' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
100
+ } , 1000 )
97
101
}
98
102
99
- // // tslint:disable-next-line:no-unused
103
+ // tslint:disable-next-line:no-unused
100
104
private toCsv ( flags : any , args : any ) {
101
- Logger . progressStart ( this , '' )
102
- Utilities . truncateFile ( this , flags . output )
103
-
104
- let prependHeader = true // only write on the first line
105
-
106
- avro . createFileDecoder ( flags . file )
107
- . on ( 'data' , function ( recordStr ) {
108
- // @ts -ignore
109
- let json = JSON . parse ( JSON . stringify ( recordStr ) )
110
- Json2Csv . json2csv ( json , ( err ?: Error , csv ?: string ) => {
111
- if ( csv )
112
- Utilities . appendStringToFile ( this , flags . output , csv + '\n' )
113
- if ( err )
114
- Logger . error ( this , err )
115
- } , { prependHeader} )
116
- prependHeader = false
117
- } )
118
- Logger . progressStop ( this , "done" )
119
- Logger . success ( this , `${ chalk . blue ( 'Csv' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
105
+ Logger . progressStart ( this , 'Converting Avro To Csv' )
106
+
107
+ setTimeout ( ( ) => {
108
+ Logger . progressStop ( this , ' Done' )
109
+ Utilities . truncateFile ( this , flags . output )
110
+ let prependHeader = true // only write on the first line
111
+ avro . createFileDecoder ( flags . file )
112
+ . on ( 'data' , function ( recordStr ) {
113
+ // @ts -ignore
114
+ let json = JSON . parse ( JSON . stringify ( recordStr ) )
115
+ Json2Csv . json2csv ( json , ( err ?: Error , csv ?: string ) => {
116
+ if ( csv ) {
117
+ // @ts -ignore
118
+ Utilities . appendStringToFile ( this , flags . output , csv + '\n' )
119
+ }
120
+ if ( err ) {
121
+ // @ts -ignore
122
+ Logger . error ( this , err . toString ( ) )
123
+ }
124
+ } , { prependHeader} )
125
+ prependHeader = false
126
+ } )
127
+ Logger . success ( this , `${ chalk . blue ( 'Csv' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
128
+ } , 1000 )
120
129
}
121
130
122
131
private toAvro ( flags : any , args : any ) {
123
132
if ( ! flags . schemaType )
124
133
Logger . error ( this , 'Schema file is not provided' )
125
134
126
- let schema = avro . parse ( flags . schemaType )
127
- let avroEncoder = new avro . streams . BlockEncoder ( schema )
135
+ Logger . progressStart ( this , 'Generating Avro' )
136
+ setTimeout ( ( ) => {
137
+ Logger . progressStop ( this , ' Done' )
138
+
139
+ let schema = avro . parse ( flags . schemaType )
140
+ let avroEncoder = new avro . streams . BlockEncoder ( schema )
128
141
129
- avroEncoder . pipe ( fs . createWriteStream ( flags . output ) )
142
+ avroEncoder . pipe ( fs . createWriteStream ( flags . output ) )
130
143
131
144
// We write the records to the block encoder, which will take care of serializing them
132
145
// into an object container file.
133
146
134
- let inputString = Utilities . getInputString ( this , flags , args )
135
- let jsonStr = this . convertAvroJsonToValidJson ( inputString )
147
+ let inputString = Utilities . getInputString ( this , flags , args )
148
+ let jsonStr = this . convertAvroJsonToValidJson ( inputString )
136
149
137
- let jsonObjects = JSON . parse ( jsonStr )
150
+ let jsonObjects = JSON . parse ( jsonStr )
138
151
139
- jsonObjects . forEach ( function ( data : any ) {
140
- if ( schema . isValid ( data ) ) {
141
- avroEncoder . write ( data )
142
- } else {
152
+ jsonObjects . forEach ( function ( data : any ) {
153
+ if ( schema . isValid ( data ) ) {
154
+ avroEncoder . write ( data )
155
+ } else {
143
156
// @ts -ignore
144
- Logger . warn ( this , `${ chalk . yellow ( '[SKIPPING RECORD]' ) } schema is invalid: ${ chalk . yellowBright ( JSON . stringify ( data ) ) } ` )
145
- }
146
- } )
147
- Logger . success ( this , `${ chalk . blue ( 'Avro' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
148
- avroEncoder . end ( )
157
+ Logger . warn ( this , `${ chalk . yellow ( '[SKIPPING RECORD]' ) } schema is invalid: ${ chalk . yellowBright ( JSON . stringify ( data ) ) } ` )
158
+ }
159
+ } )
160
+ Logger . success ( this , `${ chalk . blue ( 'Avro' ) } written to file: ${ chalk . green ( flags . output ) } ` ) // this will output error and exit command
161
+ avroEncoder . end ( )
162
+ } , 1000 )
163
+
149
164
}
150
165
151
166
private convertAvroJsonToValidJson ( json : string ) {
0 commit comments