Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken stdin input
Browse files Browse the repository at this point in the history
juanjoDiaz committed Jan 30, 2018
1 parent 24a7893 commit b4b3157
Showing 4 changed files with 120 additions and 44 deletions.
92 changes: 50 additions & 42 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
@@ -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');
@@ -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);
});
});
}

@@ -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)
@@ -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);
}
67 changes: 67 additions & 0 deletions devtools/test-streamifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Function to convert tests using the async API to use the streamming API.
Please review the conversion to fix any indentation issue or any other
unexpected behaviour.
Example:
const func = ` test('should handle empty object', (t) => {
const opts = {
fields: ['carModel', 'price', 'color']
};
const csv = json2csv(jsonFixtures.default, opts);
t.equal(csv, '"carModel","price","color"');
t.end();
});`
will output
test('should handle empty object', (t) => {
const opts = {
fields: ['carModel', 'price', 'color']
};
const transform = new Json2csvTransform(opts);
const processor = jsonFixturesStreams.default.pipe(transform);
let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, '"carModel","price","color"');
t.end();
})
.on('error', err => t.notOk(err));
});
*/

const func = String.raw`PUT_YOU_FUNCTION_HERE`

const re = /const csv = json2csv\((.*?)(, opts)?\);\n\n((.|\n)*?)}\);/g

const processedFunc = func
.replace(re,
`const transform = new Json2csvTransform($2);
const processor = $1.pipe(transform);
let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
$3
})
.on('error', err => t.notOk(err));
});`)
.replace(/const input = ((.|\n)*?);/g, `const input = new Readable();
input._read = () => {};
input.push(JSON.stringify($1));
input.push(null);`)
.replace(/jsonFixtures\./g, 'jsonFixturesStreams.')
.replace(/ t\./g, ' t.')
.replace(/;\n \n }/g, ';\n }')
.replace(/, opts/g, 'opts');

console.log(processedFunc);
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
@@ -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",

0 comments on commit b4b3157

Please sign in to comment.