@@ -7,7 +7,6 @@ const os = require('os');
7
7
const path = require ( 'path' ) ;
8
8
const Table = require ( 'cli-table' ) ;
9
9
const program = require ( 'commander' ) ;
10
- const debug = require ( 'debug' ) ( 'json2csv:cli' ) ;
11
10
const json2csv = require ( '../lib/json2csv' ) ;
12
11
const parseNdJson = require ( '../lib/parse-ndjson' ) ;
13
12
const pkg = require ( '../package' ) ;
@@ -72,36 +71,46 @@ function getFields(fieldList, fields) {
72
71
: undefined ) ;
73
72
}
74
73
75
- function getInput ( input , ndjson ) {
76
- if ( inputPath ) {
77
- if ( ndjson ) {
78
- return new Promise ( ( resolve , reject ) => {
79
- fs . readFile ( inputPath , 'utf8' , ( err , data ) => {
80
- if ( err ) {
81
- reject ( err ) ;
82
- return ;
83
- }
84
-
85
- resolve ( parseNdJson ( data ) ) ;
86
- } ) ;
87
- } ) ;
88
- }
74
+ function getInput ( ) {
75
+ if ( ! inputPath ) {
76
+ return getInputFromStdin ( ) ;
77
+ }
89
78
90
- return Promise . resolve ( require ( inputPath ) ) ;
79
+ if ( program . ndjson ) {
80
+ return getInputFromNDJSON ( ) ;
91
81
}
92
82
93
- process . stdin . resume ( ) ;
94
- process . stdin . setEncoding ( 'utf8' ) ;
83
+ return Promise . resolve ( require ( inputPath ) ) ;
84
+ }
85
+
86
+ function getInputFromNDJSON ( ) {
87
+ return new Promise ( ( resolve , reject ) => {
88
+ fs . readFile ( inputPath , 'utf8' , ( err , data ) => {
89
+ if ( err ) {
90
+ reject ( err ) ;
91
+ return ;
92
+ }
95
93
96
- let inputData = '' ;
97
- process . stdin . on ( 'data' , chunk => ( inputData += chunk ) ) ;
98
- process . stdin . on ( 'error' , err => debug ( 'Could not read from stdin' , err ) ) ;
99
- process . stdin . on ( 'end' , ( ) => {
100
- const rows = ndjson
101
- ? parseNdJson ( inputData )
102
- : JSON . parse ( inputData ) ;
94
+ resolve ( parseNdJson ( data ) ) ;
95
+ } ) ;
96
+ } ) ;
97
+ }
103
98
104
- return Promise . resolve ( rows ) ;
99
+ function getInputFromStdin ( ) {
100
+ return new Promise ( ( resolve , reject ) => {
101
+ process . stdin . resume ( ) ;
102
+ process . stdin . setEncoding ( 'utf8' ) ;
103
+
104
+ let inputData = '' ;
105
+ process . stdin . on ( 'data' , chunk => ( inputData += chunk ) ) ;
106
+ process . stdin . on ( 'error' , err => reject ( new Error ( 'Could not read from stdin' , err ) ) ) ;
107
+ process . stdin . on ( 'end' , ( ) => {
108
+ const rows = program . ndjson
109
+ ? parseNdJson ( inputData )
110
+ : JSON . parse ( inputData ) ;
111
+
112
+ resolve ( rows ) ;
113
+ } ) ;
105
114
} ) ;
106
115
}
107
116
@@ -119,22 +128,21 @@ function logPretty(csv) {
119
128
}
120
129
121
130
function processOutput ( csv ) {
122
- if ( outputPath ) {
123
- return new Promise ( ( resolve , reject ) => {
124
- fs . writeFile ( outputPath , csv , ( err ) => {
125
- if ( err ) {
126
- reject ( new Error ( 'Cannot save to ' + program . output + ': ' + err ) ) ;
127
- return ;
128
- }
129
-
130
- debug ( program . input + ' successfully converted to ' + program . output ) ;
131
- resolve ( ) ;
132
- } ) ;
133
- } ) ;
131
+ if ( ! outputPath ) {
132
+ // eslint-disable-next-line no-console
133
+ console . log ( program . pretty ? logPretty ( csv ) : csv ) ;
134
134
}
135
135
136
- // eslint-disable-next-line no-console
137
- console . log ( program . pretty ? logPretty ( csv ) : csv ) ;
136
+ return new Promise ( ( resolve , reject ) => {
137
+ fs . writeFile ( outputPath , csv , ( err ) => {
138
+ if ( err ) {
139
+ reject ( new Error ( 'Cannot save to ' + program . output + ': ' + err ) ) ;
140
+ return ;
141
+ }
142
+
143
+ resolve ( ) ;
144
+ } ) ;
145
+ } ) ;
138
146
}
139
147
140
148
getFields ( program . fieldList , program . fields )
@@ -154,8 +162,8 @@ getFields(program.fieldList, program.fields)
154
162
withBOM : program . withBom
155
163
} ;
156
164
157
- if ( program . streamming === false ) {
158
- return getInput ( program . input , program . ndjson )
165
+ if ( ! inputPath || program . streamming === false ) {
166
+ return getInput ( )
159
167
. then ( input => new JSON2CSVParser ( opts ) . parse ( input ) )
160
168
. then ( processOutput ) ;
161
169
}
0 commit comments