@@ -21,7 +21,7 @@ program
21
21
. option ( '-n, --ndjson' , 'Treat the input as NewLine-Delimited JSON.' )
22
22
. option ( '-s, --no-streamming' , 'Process the whole JSON array in memory instead of doing it line by line.' )
23
23
. option ( '-f, --fields <fields>' , 'Specify the fields to convert.' )
24
- . option ( '-l , --field-list [list] ' , 'Specify a file with a list of fields to include. One field per line .' )
24
+ . option ( '-c , --fields-config <path> ' , 'Specify a file with a fields configuration as a JSON array .' )
25
25
. option ( '-u, --unwind <paths>' , 'Creates multiple rows from a single JSON document similar to MongoDB unwind.' )
26
26
. option ( '-F, --flatten' , 'Flatten nested objects' )
27
27
. option ( '-v, --default-value [defaultValue]' , 'Specify a default value other than empty string.' )
@@ -36,13 +36,15 @@ program
36
36
. option ( '-p, --pretty' , 'Use only when printing to console. Logs output in pretty tables.' )
37
37
. parse ( process . argv ) ;
38
38
39
- const inputPath = ( program . input && ! path . isAbsolute ( program . input ) )
40
- ? path . join ( process . cwd ( ) , program . input )
41
- : program . input ;
39
+ function makePathAbsolute ( filePath ) {
40
+ return ( filePath && ! path . isAbsolute ( filePath ) )
41
+ ? path . join ( process . cwd ( ) , filePath )
42
+ : filePath ;
43
+ }
42
44
43
- const outputPath = ( program . output && ! path . isAbsolute ( program . output ) )
44
- ? path . join ( process . cwd ( ) , program . output )
45
- : program . output ;
45
+ const inputPath = makePathAbsolute ( program . input ) ;
46
+ const outputPath = makePathAbsolute ( program . output ) ;
47
+ const fieldsConfigPath = makePathAbsolute ( program . fieldsConfig ) ;
46
48
47
49
// don't fail if piped to e.g. head
48
50
process . stdout . on ( 'error' , ( error ) => {
@@ -51,24 +53,18 @@ process.stdout.on('error', (error) => {
51
53
}
52
54
} ) ;
53
55
54
- function getFields ( fieldList , fields ) {
55
- if ( fieldList ) {
56
- return new Promise ( ( resolve , reject ) => {
57
- fs . readFile ( fieldList , 'utf8' , ( err , data ) => {
58
- if ( err ) {
59
- reject ( err ) ;
60
- return ;
61
- }
62
-
63
- data . replace ( / \r \n | \n \r | \r | \n / g, os . EOL ) ;
64
- resolve ( data . split ( os . EOL ) ) ;
65
- } ) ;
66
- } ) ;
56
+ function getFields ( ) {
57
+ if ( fieldsConfigPath ) {
58
+ try {
59
+ return require ( fieldsConfigPath ) ;
60
+ } catch ( e ) {
61
+ throw new Error ( 'Invalid fields config file. (' + e . message + ')' ) ;
62
+ }
67
63
}
68
64
69
- return Promise . resolve ( fields
70
- ? fields . split ( ',' )
71
- : undefined ) ;
65
+ return program . fields
66
+ ? program . fields . split ( ',' )
67
+ : undefined ;
72
68
}
73
69
74
70
function getInput ( ) {
@@ -148,10 +144,10 @@ function processOutput(csv) {
148
144
} ) ;
149
145
}
150
146
151
- getFields ( program . fieldList , program . fields )
152
- . then ( ( fields ) => {
147
+ Promise . resolve ( )
148
+ . then ( ( ) => {
153
149
const opts = {
154
- fields : fields ,
150
+ fields : getFields ( ) ,
155
151
unwind : program . unwind ? program . unwind . split ( ',' ) : [ ] ,
156
152
flatten : program . flatten ,
157
153
defaultValue : program . defaultValue ,
0 commit comments