Skip to content

Commit 9c861ed

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: Allow passing ldjson input files (#220)
1 parent 4668a8b commit 9c861ed

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

bin/json2csv.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,28 @@ function getFields(callback) {
5151
}
5252

5353
function getInput(callback) {
54-
var input, isAbsolute, rows;
54+
var input = '';
5555

5656
if (program.input) {
57-
isAbsolute = isAbsolutePath(program.input);
58-
input = require(isAbsolute ? program.input : path.join(process.cwd(), program.input));
57+
var isAbsolute = isAbsolutePath(program.input);
58+
var inputPath = isAbsolute ? program.input : path.join(process.cwd(), program.input);
5959

60+
if (program.ldjson) {
61+
fs.readFile(inputPath, 'utf8', function (err, data) {
62+
if (err) {
63+
return callback(err);
64+
}
65+
66+
input = parseLdJson(data);
67+
callback(null, input);
68+
});
69+
return;
70+
}
71+
72+
input = require(inputPath);
6073
return callback(null, input);
6174
}
6275

63-
input = '';
6476
process.stdin.resume();
6577
process.stdin.setEncoding('utf8');
6678

@@ -71,11 +83,9 @@ function getInput(callback) {
7183
debug('Could not read from stdin', err);
7284
});
7385
process.stdin.on('end', function () {
74-
if (program.ldjson) {
75-
rows = parseLdJson(input);
76-
} else {
77-
rows = JSON.parse(input);
78-
}
86+
var rows = program.ldjson
87+
? parseLdJson(input)
88+
: JSON.parse(input);
7989

8090
callback(null, rows);
8191
});

0 commit comments

Comments
 (0)