Skip to content

Commit

Permalink
fix: Allow passing ldjson input files (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 20, 2018
1 parent 4668a8b commit 9c861ed
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,28 @@ function getFields(callback) {
}

function getInput(callback) {
var input, isAbsolute, rows;
var input = '';

if (program.input) {
isAbsolute = isAbsolutePath(program.input);
input = require(isAbsolute ? program.input : path.join(process.cwd(), program.input));
var isAbsolute = isAbsolutePath(program.input);
var inputPath = isAbsolute ? program.input : path.join(process.cwd(), program.input);

if (program.ldjson) {
fs.readFile(inputPath, 'utf8', function (err, data) {
if (err) {
return callback(err);
}

input = parseLdJson(data);
callback(null, input);
});
return;
}

input = require(inputPath);
return callback(null, input);
}

input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');

Expand All @@ -71,11 +83,9 @@ function getInput(callback) {
debug('Could not read from stdin', err);
});
process.stdin.on('end', function () {
if (program.ldjson) {
rows = parseLdJson(input);
} else {
rows = JSON.parse(input);
}
var rows = program.ldjson
? parseLdJson(input)
: JSON.parse(input);

callback(null, rows);
});
Expand Down

0 comments on commit 9c861ed

Please sign in to comment.