Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#297 resolve cli table dependency #311

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ $ npm install json2csv --save
-a, --include-empty-rows Includes empty rows in the resulting CSV output.
-b, --with-bom Includes BOM character at the beginning of the csv.
-p, --pretty Use only when printing to console. Logs output in pretty tables.
-P, --pretty-without-color Use only when printing to console. Logs output in pretty tables, but without color.
-h, --help output usage information
```

Expand Down Expand Up @@ -151,7 +152,7 @@ $ json2csv -i test.json -f name,version --no-header >> test.csv

### Available Options

The programatic APIs take a configuration object very equivalent to the CLI options.
The programatic APIs take a configuration object very equivalent to the CLI options.

- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
- `ndjson` - Only effective on the streaming API. Indicates that data coming through the stream is NDJSON.
Expand All @@ -170,7 +171,7 @@ The programatic APIs take a configuration object very equivalent to the CLI opti

### json2csv parser (Synchronous API)

`json2csv` can also be use programatically as a synchronous converter using its `parse` method.
`json2csv` can also be use programatically as a synchronous converter using its `parse` method.
```javascript
const Json2csvParser = require('json2csv').Parser;
const fields = ['field1', 'field2', 'field3'];
Expand Down
8 changes: 5 additions & 3 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ program
.option('-a, --include-empty-rows', 'Includes empty rows in the resulting CSV output.')
.option('-b, --with-bom', 'Includes BOM character at the beginning of the csv.')
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
.option('-P, --pretty-without-color', 'Use only when printing to console. Logs output in pretty tables, but without color.')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary. cli-table3 through colors already supports a --no-color by default, so a dedicated option should not be needed here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in CLI.js call child_process.exec on json2csv.js, so short of adding another command line option here to json2csv (to keep colour by default per #311 (comment) ) , or a fundamental re-write of the tests, this does seem necessary to pass that option through to json2csv as a process argument, because of the way it's called.

.parse(process.argv);

function makePathAbsolute(filePath) {
Expand Down Expand Up @@ -131,9 +132,10 @@ function getInputFromStdin() {
}

function processOutput(csv) {

if (!outputPath) {
// eslint-disable-next-line no-console
program.pretty ? (new TablePrinter(program)).printCSV(csv) : console.log(csv);
(program.pretty || program.prettyWithoutColor) ? (new TablePrinter(program)).printCSV(csv) : console.log(csv);
return;
}

Expand Down Expand Up @@ -176,7 +178,7 @@ Promise.resolve()
const transform = new Json2csvTransform(opts);
const input = getInputStream();
const stream = input.pipe(transform);

if (program.output) {
const outputStream = fs.createWriteStream(outputPath, { encoding: 'utf8' });
const output = stream.pipe(outputStream);
Expand All @@ -188,7 +190,7 @@ Promise.resolve()
});
}

if (!program.pretty) {
if (!(program.pretty || program.prettyWithoutColor)) {
const output = stream.pipe(process.stdout);
return new Promise((resolve, reject) => {
input.on('error', reject);
Expand Down
26 changes: 16 additions & 10 deletions bin/utils/TablePrinter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
'use strict';

const Table = require('cli-table2');
const Table = require('cli-table3');

const MIN_CELL_WIDTH = 15;
const noColorStyle =
{
head: [], // disable colors in header cells
border: [] // disable colors for borders
};

class TablePrinter {
constructor(opts) {
this.opts = opts;
this._hasWritten = false;
this.colWidths;
this.style = opts.prettyWithoutColor ? noColorStyle : {};
}

push(csv) {
Expand All @@ -24,8 +30,8 @@ class TablePrinter {
if (!this._hasWritten) {
this.colWidths = this.getColumnWidths(lines[0]);
if (this.opts.header) {
const head = lines.shift().split(this.opts.delimiter);
const table = new Table({ head, colWidths: this.colWidths, chars });
const head = lines.shift().split(this.opts.delimiter);
const table = new Table({ head, colWidths: this.colWidths, style: this.style, chars });
this.print(table, []);
this._hasWritten = true;
}
Expand All @@ -37,15 +43,15 @@ class TablePrinter {

if (!lines.length) return;

const table = new Table({ colWidths: this.colWidths, chars });
const table = new Table({ colWidths: this.colWidths, style: this.style, chars });
this.print(table, lines);
this._hasWritten = true;
}

end(csv) {
const lines = csv.split(this.opts.eol);
const chars = { 'top-left': '├' , 'top-mid': '┼', 'top-right': '┤' };
const table = new Table({ colWidths: this.colWidths, chars });
const table = new Table({ colWidths: this.colWidths, style: this.style, chars });
this.print(table, lines);
}

Expand All @@ -56,10 +62,10 @@ class TablePrinter {
const head = this.opts.header
? lines.shift().split(this.opts.delimiter)
: undefined;

const table = new Table(head
? { head, colWidths: this.colWidths }
: { colWidths: this.colWidths });
? { head, colWidths: this.colWidths, style: this.style }
: { colWidths: this.colWidths, style: this.style });

this.print(table, lines);
}
Expand All @@ -73,8 +79,8 @@ class TablePrinter {
print(table, lines) {
lines.forEach(line => table.push(line.split(this.opts.delimiter)));
// eslint-disable-next-line no-console
console.log(table.toString());
console.log(table.toString());
}
}

module.exports = TablePrinter;
module.exports = TablePrinter;
Loading