Skip to content

Commit

Permalink
fix: broken stdin input (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 30, 2018
1 parent 24a7893 commit 6cb407c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
92 changes: 50 additions & 42 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const os = require('os');
const path = require('path');
const Table = require('cli-table');
const program = require('commander');
const debug = require('debug')('json2csv:cli');
const json2csv = require('../lib/json2csv');
const parseNdJson = require('../lib/parse-ndjson');
const pkg = require('../package');
Expand Down Expand Up @@ -72,36 +71,46 @@ function getFields(fieldList, fields) {
: undefined);
}

function getInput(input, ndjson) {
if (inputPath) {
if (ndjson) {
return new Promise((resolve, reject) => {
fs.readFile(inputPath, 'utf8', (err, data) => {
if (err) {
reject(err);
return;
}

resolve(parseNdJson(data));
});
});
}
function getInput() {
if (!inputPath) {
return getInputFromStdin();
}

return Promise.resolve(require(inputPath));
if (program.ndjson) {
return getInputFromNDJSON();
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
return Promise.resolve(require(inputPath));
}

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

let inputData = '';
process.stdin.on('data', chunk => (inputData += chunk));
process.stdin.on('error', err => debug('Could not read from stdin', err));
process.stdin.on('end', () => {
const rows = ndjson
? parseNdJson(inputData)
: JSON.parse(inputData);
resolve(parseNdJson(data));
});
});
}

return Promise.resolve(rows);
function getInputFromStdin() {
return new Promise((resolve, reject) => {
process.stdin.resume();
process.stdin.setEncoding('utf8');

let inputData = '';
process.stdin.on('data', chunk => (inputData += chunk));
process.stdin.on('error', err => reject(new Error('Could not read from stdin', err)));
process.stdin.on('end', () => {
const rows = program.ndjson
? parseNdJson(inputData)
: JSON.parse(inputData);

resolve(rows);
});
});
}

Expand All @@ -119,22 +128,21 @@ function logPretty(csv) {
}

function processOutput(csv) {
if (outputPath) {
return new Promise((resolve, reject) => {
fs.writeFile(outputPath, csv, (err) => {
if (err) {
reject(new Error('Cannot save to ' + program.output + ': ' + err));
return;
}

debug(program.input + ' successfully converted to ' + program.output);
resolve();
});
});
if (!outputPath) {
// eslint-disable-next-line no-console
console.log(program.pretty ? logPretty(csv) : csv);
}

// eslint-disable-next-line no-console
console.log(program.pretty ? logPretty(csv) : csv);
return new Promise((resolve, reject) => {
fs.writeFile(outputPath, csv, (err) => {
if (err) {
reject(new Error('Cannot save to ' + program.output + ': ' + err));
return;
}

resolve();
});
});
}

getFields(program.fieldList, program.fields)
Expand All @@ -154,8 +162,8 @@ getFields(program.fieldList, program.fields)
withBOM: program.withBom
};

if (program.streamming === false) {
return getInput(program.input, program.ndjson)
if (!inputPath || program.streamming === false) {
return getInput()
.then(input => new JSON2CSVParser(opts).parse(input))
.then(processOutput);
}
Expand Down
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"dependencies": {
"cli-table": "^0.3.1",
"commander": "^2.8.1",
"debug": "^3.1.0",
"flat": "^4.0.0",
"jsonparse": "^1.3.1",
"lodash.clonedeep": "^4.5.0",
Expand Down

0 comments on commit 6cb407c

Please sign in to comment.