Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,21 @@ unflatten({

```sh
npx flat foo.json
npx flat --unflatten foo.json
```

Or install the `flat` command globally:

```sh
npm i -g flat && flat foo.json
npm i -g flat && flat --unflatten foo.json
```

Accepts a filename as an argument:

```sh
flat foo.json
flat --unflatten foo.json
```

Also accepts JSON on stdin:
Expand Down
14 changes: 9 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import fs from 'node:fs'
import path from 'node:path'
import readline from 'node:readline'
import { flatten } from './index.js'
import { flatten, unflatten } from './index.js'

const args = process.argv.slice(2)
const shouldUnflatten = args.includes('--unflatten')
const filepath = args.find(arg => !arg.startsWith('--'))

const filepath = process.argv.slice(2)[0]
if (filepath) {
// Read from file
const file = path.resolve(process.cwd(), filepath)
Expand All @@ -25,15 +28,16 @@ if (filepath) {
}

function out (data) {
process.stdout.write(JSON.stringify(flatten(data), null, 2))
const transformed = shouldUnflatten ? unflatten(data) : flatten(data)
process.stdout.write(JSON.stringify(transformed, null, 2))
}

function usage (code) {
console.log(`
Usage:

flat foo.json
cat foo.json | flat
flat [--unflatten] foo.json
cat foo.json | flat [--unflatten]
`)

process.exit(code || 0)
Expand Down