Skip to content

Commit 74ef666

Browse files
juanjoDiazknownasilya
authored andcommitted
feat: Add fields config option to CLI (#245)
BREAKING CHANGE: Replaces field-list with field-config
1 parent 1fcde13 commit 74ef666

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ $ npm install json2csv --save
5151
-n, --ndjson Treat the input as NewLine-Delimited JSON.
5252
-s, --no-streamming Process the whole JSON array in memory instead of doing it line by line.
5353
-f, --fields <fields> Specify the fields to convert.
54-
-l, --field-list [list] Specify a file with a list of fields to include. One field per line.
54+
-c, --fields-config [list] Specify a file with a fields configuration as a JSON array.
5555
-u, --unwind <paths> Creates multiple rows from a single JSON document similar to MongoDB unwind.
5656
-F, --flatten Flatten nested objects
5757
-v, --default-value [defaultValue] Specify a default value other than empty string.

bin/json2csv.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ program
2121
.option('-n, --ndjson', 'Treat the input as NewLine-Delimited JSON.')
2222
.option('-s, --no-streamming', 'Process the whole JSON array in memory instead of doing it line by line.')
2323
.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.')
2525
.option('-u, --unwind <paths>', 'Creates multiple rows from a single JSON document similar to MongoDB unwind.')
2626
.option('-F, --flatten', 'Flatten nested objects')
2727
.option('-v, --default-value [defaultValue]', 'Specify a default value other than empty string.')
@@ -36,13 +36,15 @@ program
3636
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
3737
.parse(process.argv);
3838

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+
}
4244

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);
4648

4749
// don't fail if piped to e.g. head
4850
process.stdout.on('error', (error) => {
@@ -51,24 +53,18 @@ process.stdout.on('error', (error) => {
5153
}
5254
});
5355

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+
}
6763
}
6864

69-
return Promise.resolve(fields
70-
? fields.split(',')
71-
: undefined);
65+
return program.fields
66+
? program.fields.split(',')
67+
: undefined;
7268
}
7369

7470
function getInput() {
@@ -148,10 +144,10 @@ function processOutput(csv) {
148144
});
149145
}
150146

151-
getFields(program.fieldList, program.fields)
152-
.then((fields) => {
147+
Promise.resolve()
148+
.then(() => {
153149
const opts = {
154-
fields: fields,
150+
fields: getFields(),
155151
unwind: program.unwind ? program.unwind.split(',') : [],
156152
flatten: program.flatten,
157153
defaultValue: program.defaultValue,

0 commit comments

Comments
 (0)