Skip to content

Commit

Permalink
feat: Add fields config option to CLI (#245)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Replaces field-list with field-config
  • Loading branch information
juanjoDiaz authored and knownasilya committed Feb 3, 2018
1 parent 1fcde13 commit 74ef666
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $ npm install json2csv --save
-n, --ndjson Treat the input as NewLine-Delimited JSON.
-s, --no-streamming Process the whole JSON array in memory instead of doing it line by line.
-f, --fields <fields> Specify the fields to convert.
-l, --field-list [list] Specify a file with a list of fields to include. One field per line.
-c, --fields-config [list] Specify a file with a fields configuration as a JSON array.
-u, --unwind <paths> Creates multiple rows from a single JSON document similar to MongoDB unwind.
-F, --flatten Flatten nested objects
-v, --default-value [defaultValue] Specify a default value other than empty string.
Expand Down
48 changes: 22 additions & 26 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ program
.option('-n, --ndjson', 'Treat the input as NewLine-Delimited JSON.')
.option('-s, --no-streamming', 'Process the whole JSON array in memory instead of doing it line by line.')
.option('-f, --fields <fields>', 'Specify the fields to convert.')
.option('-l, --field-list [list]', 'Specify a file with a list of fields to include. One field per line.')
.option('-c, --fields-config <path>', 'Specify a file with a fields configuration as a JSON array.')
.option('-u, --unwind <paths>', 'Creates multiple rows from a single JSON document similar to MongoDB unwind.')
.option('-F, --flatten', 'Flatten nested objects')
.option('-v, --default-value [defaultValue]', 'Specify a default value other than empty string.')
Expand All @@ -36,13 +36,15 @@ program
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
.parse(process.argv);

const inputPath = (program.input && !path.isAbsolute(program.input))
? path.join(process.cwd(), program.input)
: program.input;
function makePathAbsolute(filePath) {
return (filePath && !path.isAbsolute(filePath))
? path.join(process.cwd(), filePath)
: filePath;
}

const outputPath = (program.output && !path.isAbsolute(program.output))
? path.join(process.cwd(), program.output)
: program.output;
const inputPath = makePathAbsolute(program.input);
const outputPath = makePathAbsolute(program.output);
const fieldsConfigPath = makePathAbsolute(program.fieldsConfig);

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

function getFields(fieldList, fields) {
if (fieldList) {
return new Promise((resolve, reject) => {
fs.readFile(fieldList, 'utf8', (err, data) => {
if (err) {
reject(err);
return;
}

data.replace(/\r\n|\n\r|\r|\n/g, os.EOL);
resolve(data.split(os.EOL));
});
});
function getFields() {
if (fieldsConfigPath) {
try {
return require(fieldsConfigPath);
} catch (e) {
throw new Error('Invalid fields config file. (' + e.message + ')');
}
}

return Promise.resolve(fields
? fields.split(',')
: undefined);
return program.fields
? program.fields.split(',')
: undefined;
}

function getInput() {
Expand Down Expand Up @@ -148,10 +144,10 @@ function processOutput(csv) {
});
}

getFields(program.fieldList, program.fields)
.then((fields) => {
Promise.resolve()
.then(() => {
const opts = {
fields: fields,
fields: getFields(),
unwind: program.unwind ? program.unwind.split(',') : [],
flatten: program.flatten,
defaultValue: program.defaultValue,
Expand Down

0 comments on commit 74ef666

Please sign in to comment.