Skip to content

Commit

Permalink
feat: rewrite in TypeScript / ESM
Browse files Browse the repository at this point in the history
* expand test suite
* replace `args` with `cmd-ts`
* make stdin an explicit parameter

BREAKING CHANGE: to pass input as stdin, `-` must be provided as the input file parameter.
BREAKING CHANGE: the `--columns` option long name is now `--column`.
  • Loading branch information
haltcase committed Jan 1, 2022
1 parent 3bd02ca commit 1c45f86
Showing 16 changed files with 588 additions and 175 deletions.
38 changes: 38 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict"

const [, , error] = ["off", "warn", "error"]

module.exports = {
extends: ["./node_modules/ts-standardx/.eslintrc.js"],
ignorePatterns: ["dist"],
rules: {
"no-unused-vars": [
error,
{
argsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
varsIgnorePattern: "^_"
}
],
quotes: [error, "double"],

"prettier/prettier": [
error,
{
semi: false,
singleQuote: false,
trailingComma: "none",
bracketSameLine: true,
arrowParens: "avoid"
}
]
},
overrides: [
{
files: ["**/*.{ts,tsx}"],
rules: {
"@typescript-eslint/quotes": [error, "double"]
}
}
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -9,3 +9,5 @@ package-lock.json
yarn.lock

node_modules

/dist
10 changes: 10 additions & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
extensions: {
ts: "module"
},
nonSemVerExperiments: {
configurableModuleFormat: true,
nextGenConfig: true
},
nodeArguments: ["--loader=ts-node/esm"]
}
51 changes: 0 additions & 51 deletions cli.js

This file was deleted.

58 changes: 0 additions & 58 deletions index.js

This file was deleted.

29 changes: 18 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
"engines": {
"node": ">=14.18"
},
"type": "module",
"exports": "./dist/index.js",
"keywords": [
"cli",
"cli-app",
@@ -20,26 +22,31 @@
"generate"
],
"bin": {
"tablemark": "cli.js"
"tablemark": "./dist/cli.js"
},
"files": [
"index.js",
"cli.js"
"dist"
],
"scripts": {
"lint": "standard | snazzy",
"changelog": "changelog",
"lint": "ts-standardx && cd tests && ts-standardx . --disable-gitignore",
"build": "tsc",
"pretest": "npm run build",
"test": "ava",
"prepublishOnly": "npm run lint && npm test"
},
"dependencies": {
"args": "^5.0.1",
"get-stdin": "^7.0.0",
"is-valid-path": "^0.1.1",
"tablemark": "^2.0.0"
"cmd-ts": "^0.9.0",
"get-stdin": "^9.0.0",
"tablemark": "^3.0.0"
},
"devDependencies": {
"ava": "^2.2.0",
"snazzy": "^8.0.0",
"standard": "^13.0.2"
"@citycide/changelog": "^2.0.0",
"@types/node": "^14.18.2",
"ava": "^3.15.0",
"execa": "^6.0.0",
"ts-node": "^10.4.0",
"ts-standardx": "^0.8.4",
"typescript": "^4.5.4"
}
}
69 changes: 47 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
@@ -2,58 +2,83 @@

> Generate markdown tables from JSON data at the command line.
Parse JSON input data into a markdown table from the command line,
Render JSON input data as a markdown table from the command line,
powered by the [`tablemark`](https://github.com/citycide/tablemark) module.

## features

This utility supports:

- JSON file input from a provided path
- data piped from `stdin`
- NDJSON formatted data ([newline delimited JSON](http://ndjson.org/)).
* JSON file input from a provided path
* data piped from `stdin`
* NDJSON formatted data ([newline delimited JSON](http://ndjson.org/))

## installation

```sh
yarn global add tablemark-cli

# or

npm install --global tablemark-cli
```

## usage

```sh
Usage: tablemark <input-file> > <output-file> [options]

Commands:
tablemark 3.0.0
> Generate markdown tables from JSON data at the command line.

help Display help
ARGUMENTS:
<input-file> - Path to input file containing JSON data (use - for stdin)

Options:
OPTIONS:
--column <str>, -c=<str> - Custom column name, can be used multiple times (default: infer from object keys)
--align <value>, -a=<value> - Custom alignments, can be used multiple times, applied in order to columns (default: left)
--line-ending, -e <str> - End-of-line string (default: \n) [optional]
--wrap-width, -w <number> - Width at which to hard wrap cell content [default: Infinity]

-a, --align <list> List of alignment types, applied in order to columns. (defaults to [])
-c, --columns <list> List of column names, defaults to object keys. (defaults to [])
-h, --help Output usage information
-v, --version Output the version number
FLAGS:
--no-case-headers, -N - Disable automatic sentence casing of inferred column names [default: false]
--wrap-with-gutters, -G - Add '|' characters to wrapped rows [default: false]
--help, -h - show help
--version, -v - print the version
```

To use the `align` and `column` options, you can use the `-a` or
`-c` flags multiple times, like this:
To apply the `align` and `column` options to multiple columns, supply the flag
multiple times, like this:

````console
tablemark input.json > output.md -a left -a center
````
```sh
tablemark input.json > output.md -a left -a center -a right
```

... which will align the first two columns left and center respectively.
... which will align the first three columns left, center, and right respectively.

## stdin

In bash-like shells:

```sh
tablemark < input.json > output.md
# stdin -> stdout
echo '{ "one": 1 }' | tablemark -

# redirect input file content into stdin, then to a file
tablemark - < input.json > output.md
```

In PowerShell:

```powershell
# stdin -> stdout
'{ "one": 1 }' | tablemark -
# redirect input file content into stdin, then to a file
cat input.json | tablemark - > output.md
```

## ndjson

NDJSON is a form of JSON that delimits multiple JSON objects by newlines:
[NDJSON](http://ndjson.org) is a form of JSON that delimits multiple JSON objects by newlines:

```js
{"name":"trilogy","repo":"[citycide/trilogy](https://github.com/citycide/trilogy)","desc":"No-hassle SQLite with type-casting schema models and support for native & pure JS backends."}
@@ -70,7 +95,7 @@ tablemark input.ndjson > output.md

## see also

- [`tablemark`](https://github.com/citycide/tablemark) &ndash; the module used by this utility
* [`tablemark`](https://github.com/citycide/tablemark) &ndash; the module used by this utility

## contributing

Loading

0 comments on commit 1c45f86

Please sign in to comment.