Skip to content

Commit ee76424

Browse files
committed
[ISSUE#51]: to_json done
igned-off-by: ashish <ashishpatel0720@gmail.com> Signed-off-by: ashish <ashishpatel0720@gmail.com>
1 parent 3e5abe8 commit ee76424

File tree

4 files changed

+144048
-28
lines changed

4 files changed

+144048
-28
lines changed

src/commands/avro.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Command, flags} from '@oclif/command'
2-
import * as avro from 'avsc' // includes all from avro-js and some more
2+
import * as avro from 'avsc'
3+
import * as chalk from 'chalk' // includes all from avro-js and some more
34

45
import Logger from '../utilities/logger'
56
import Utilities from '../utilities/utilities'
@@ -60,14 +61,21 @@ export default class Avro extends Command {
6061

6162
}
6263
private toJson(flags: any, args: any) {
64+
Utilities.truncateFile(this, flags.output)
6365
avro.createFileDecoder(flags.file)
64-
.on('data', function (data) {
65-
return data
66+
.on('data', function (recordStr) {
67+
Utilities.appendStringToFile(this, flags.output, JSON.stringify(recordStr, null, '\t'))
6668
})
67-
69+
Logger.success(this, `output written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
6870
}
6971
private toAvro(flags: any, args: any) {
7072

7173
}
7274

75+
// private checkValidAvsc(){
76+
// // Or we can specify a path to a schema file (not in the browser):
77+
// var type = avro.parse('./Person.avsc');
78+
// var person = {name: 'Bob', address: {city: 'Cambridge', zip: '02139'}};
79+
// var status = type.isValid(person); // Boolean status.
80+
// }
7381
}

src/utilities/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,16 @@ export default class Utilities {
5050
// return `${chalk.red(pkg)} ${message}`
5151

5252
}
53+
54+
public static appendStringToFile(thisRef: any, filePath: string, string: string) {
55+
if (!fs.existsSync(filePath))
56+
Logger.info(thisRef, `Could not find file: ${chalk.yellow(filePath + ', creating new one')}`) // this will output error and exit command
57+
fs.appendFileSync(filePath, string)
58+
}
59+
60+
public static truncateFile(thisRef: any, filePath: string) {
61+
if (fs.existsSync(filePath))
62+
Logger.info(thisRef, `file found: ${chalk.yellow(filePath + ', truncating')}`) // this will output error and exit command
63+
Utilities.writeStringToFile(thisRef, filePath, '') // write nothing
64+
}
5365
}

0 commit comments

Comments
 (0)