Skip to content

Commit

Permalink
Merge pull request #106 from donatj/cli
Browse files Browse the repository at this point in the history
Adds Basic Cli
  • Loading branch information
donatj authored Dec 1, 2023
2 parents 1f46696 + 94951e8 commit 34cd859
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[*]
end_of_line = lf
insert_final_newline = true

[*.{js,ts,html}]
charset = utf-8
indent_style = tab
Expand Down
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,38 @@ Requires **no external libraries**. Works in Node as well as in the browser.

## Install

```
```bash
npm install csv-to-markdown-table
```

## Usage

### CLI

This package also includes a CLI tool. You can install it globally with:

```bash
npm install -g csv-to-markdown-table
```

Then you can use it like so:

```bash
$ csvtomarkdown --help
Usage: csvtomarkdown [options]
// … help output

$ csvtomarkdown --delimiter ',' --headers < example.csv
| cats | dogs | fish |
|------|------|------|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
$ csvtomarkdown
Reading from stdin... (press Ctrl+D at the start of a line to finish)
CSV Delimiter: \t (tab) Headers: false
[interactive input]
```

### Raw JS via Global

```js
Expand Down Expand Up @@ -49,7 +75,7 @@ console.log(

#### Outputs:

```
```markdown
| header1 | header2 | header3 |
|---------|---------|---------|
| Value1 | Value2 | Value3 |
Expand Down
105 changes: 105 additions & 0 deletions bin/csvtomarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env node

const CsvToMarkdown = require('../lib/CsvToMarkdown.js');
const path = require('path');

// Simple flag parser
const args = process.argv.slice(2);
let delim = '\t'; // Default delimiter
let headers = false;

for (let i = 0; i < args.length; i++) {
const arg = args[i];

if (arg === '--delim') {
if (i + 1 < args.length) { // Check if there is a next argument
delim = args[i + 1];

// Special cases for delimiter
if (delim === ':tab') {
delim = '\t';
} else if (delim === ':comma') {
delim = ',';
} else if (delim === ':semicolon') {
delim = ';';
}

i++; // Skip next argument as it's part of --delim
} else {
console.error("No delimiter specified after --delim.");
return;
}
} else if (arg === '--headers') {
headers = true;
} else if (arg === '--help') {
displayHelp();
return;
} else {
console.error(`Unrecognized argument: ${arg}`);
displayHelp();
process.exit(1);
return;
}
}


if (process.stdin.isTTY) {
console.log("Reading from stdin... (press Ctrl+D at the start of a line to finish)");
console.log("CSV Delimiter:", getDelimiterName(delim), "Headers:", headers);
}

let data = '';
process.stdin.on('readable', () => {
let chunk;
// Keep reading data until there's none left.
while ((chunk = process.stdin.read()) !== null) {
data += chunk;
}
});

process.stdin.on('end', () => {
process.stdout.write(CsvToMarkdown(data, delim, headers));
});

function getDelimiterName(delim) {
const charMap = {
'\n': '\\n (newline)',
'\t': '\\t (tab)',
'\r': '\\r (carriage return)',
'\0': '\\0 (null character)',
' ': '(space)',
'\v': '\\v (vertical tab)',
'\f': '\\f (form feed)',
'\x1C': '\\x1C (file separator)',
'\x1D': '\\x1D (group separator)',
'\x1E': '\\x1E (record separator)',
'\x1F': '\\x1F (unit separator)'
// Add more specific characters as needed
};

let delimName = '';

let stringify = true;
for (const char of delim) {
if (charMap.hasOwnProperty(char)) {
delimName += charMap[char];
stringify = false;
} else if (char.charCodeAt(0) < 32 || char.charCodeAt(0) > 126) {
delimName += `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`;
stringify = false;
} else {
delimName += char;
}
}

return stringify ? JSON.stringify(delimName) : delimName;
}

function displayHelp() {
const scriptName = path.basename(process.argv[1]);
console.log(`Usage: ${scriptName} [options]`);
console.log("Options:");
console.log(" --delim [delimiter] Specify a custom delimiter. Default is tab character. Any valid string. Special cases are provided for :tab, :comma, :semicolon.");
console.log(" --headers Use the first line as a header.");
console.log(" --help Display this help message and exit.");
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "csv-to-markdown-table",
"description": "JavaScript/Node.js Csv to Markdown Table Converter",
"main": "./lib/CsvToMarkdown.js",
"bin": "./bin/csvtomarkdown",
"version": "1.3.1",
"types": "./lib/CsvToMarkdown.d.js",
"scripts": {
Expand Down

0 comments on commit 34cd859

Please sign in to comment.