From 74ef6667a15d13c314b1866e2a80cab432487371 Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Fri, 2 Feb 2018 17:56:50 -0800 Subject: [PATCH] feat: Add fields config option to CLI (#245) BREAKING CHANGE: Replaces field-list with field-config --- README.md | 2 +- bin/json2csv.js | 48 ++++++++++++++++++++++-------------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c0ecc3c0..3a2a7856 100644 --- a/README.md +++ b/README.md @@ -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 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 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. diff --git a/bin/json2csv.js b/bin/json2csv.js index bfe5229d..4e15de23 100755 --- a/bin/json2csv.js +++ b/bin/json2csv.js @@ -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 ', '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 ', 'Specify a file with a fields configuration as a JSON array.') .option('-u, --unwind ', '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.') @@ -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) => { @@ -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() { @@ -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,