Skip to content

Commit e2f8829

Browse files
committed
[cdt-63] : to_csv added
Signed-off-by: ashish <[email protected]>
1 parent e363017 commit e2f8829

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"detect-character-encoding": "^0.8.0",
2525
"encoding-japanese": "^1.0.30",
2626
"jshashes": "^1.0.7",
27+
"json-2-csv": "^3.5.9",
2728
"minify": "^5.0.0",
2829
"moment": "^2.24.0",
2930
"moment-timezone": "^0.5.27",

src/commands/avro.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Command, flags} from '@oclif/command'
22
import * as avro from 'avsc'
33
import * as chalk from 'chalk'
44
import * as fs from 'fs' // includes all from avro-js and some more
5+
import * as Json2Csv from 'json-2-csv'
56

67
import Logger from '../utilities/logger'
78
import Utilities from '../utilities/utilities'
@@ -59,8 +60,8 @@ export default class Avro extends Command {
5960
return this.toJson(flags, args)
6061
case Avro.TO_AVRO:
6162
return this.toAvro(flags, args)
62-
// case Avro.TO_CSV:
63-
// return this.toCsv(flags, args)
63+
case Avro.TO_CSV:
64+
return this.toCsv(flags, args)
6465
default:
6566
Logger.error(this, 'Unsupported Command, supported: ' + Avro.SupportedCommands)
6667
}
@@ -96,26 +97,27 @@ export default class Avro extends Command {
9697
}
9798

9899
// // tslint:disable-next-line:no-unused
99-
// private toCsv(flags: any, args: any) {
100-
// var json2Csv = require("json-2-csv")
101-
//
102-
// let json=`
103-
// {
104-
// "created_at": "Thu May 10 15:24:15 +0000 2018",
105-
// "id_str": "850006245121695744",
106-
// "text": "Here is the Tweet message.",
107-
// "user": {
108-
// },
109-
// "place": {
110-
// },
111-
// "entities": {
112-
// },
113-
// "extended_entities": {
114-
// }
115-
// }
116-
// `
117-
//
118-
// }
100+
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
120+
}
119121

120122
private toAvro(flags: any, args: any) {
121123
if (!flags.schemaType)
@@ -129,9 +131,9 @@ export default class Avro extends Command {
129131
// We write the records to the block encoder, which will take care of serializing them
130132
// into an object container file.
131133

132-
let jsonStr = '[' + Utilities.getInputString(this, flags, args) + ']'
133-
jsonStr = jsonStr.replace(/[\s\n]+/mg, '')
134-
jsonStr = jsonStr.replace(/\}\{/mg, '},{')
134+
let inputString = Utilities.getInputString(this, flags, args)
135+
let jsonStr = this.convertAvroJsonToValidJson(inputString)
136+
135137
let jsonObjects = JSON.parse(jsonStr)
136138

137139
jsonObjects.forEach(function (data: any) {
@@ -145,4 +147,11 @@ export default class Avro extends Command {
145147
Logger.success(this, `${chalk.blue('Avro')} written to file: ${chalk.green(flags.output)}`) // this will output error and exit command
146148
avroEncoder.end()
147149
}
150+
151+
private convertAvroJsonToValidJson(json: string) {
152+
let jsonStr = '[' + json + ']'
153+
jsonStr = jsonStr.replace(/[\s\n]+/mg, '')
154+
jsonStr = jsonStr.replace(/\}\{/mg, '},{')
155+
return jsonStr
156+
}
148157
}

src/commands/bundlephobia.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class Bundlephobia extends Command {
2222

2323
static args = [{name: 'package'}] // only one can be passed club which one passed through flag and arg
2424

25-
private static getErrorMessage(pkg: string, message: string) {
25+
private getErrorMessage(pkg: string, message: string) {
2626
// replacing will be useful when we do not have specific version
2727
// output will be like below
2828
/*
@@ -104,7 +104,7 @@ export default class Bundlephobia extends Command {
104104
dependencyCount += successResponse.data.dependencyCount
105105
Logger.progressStop(this, this.getSuccessMessage(successResponse.data))
106106
}).catch(errorResponse => {
107-
Logger.progressStopError(this, Bundlephobia.getErrorMessage(packageInfo.pkg, errorResponse.response.data.error.message))
107+
Logger.progressStopError(this, this.getErrorMessage(packageInfo.pkg, errorResponse.response.data.error.message))
108108
})
109109
// tslint:disable-next-line:no-unused
110110
}))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
username,tweet,timestamp
2+
miguno,"Rock:Nerfpaper,scissorsisfine.",1366150681
3+
BlizzardCS,Worksasintended.TerranisIMBA.,1366154481

0 commit comments

Comments
 (0)