From 9f83e7e997c926d093dca024a348cc66be5218a9 Mon Sep 17 00:00:00 2001 From: Noel Schenk Date: Sat, 25 Apr 2026 13:37:59 +0200 Subject: [PATCH] Add unflatten option to cli.js Update README with unflatten argument Added usage instructions for the 'flat' command line tool. --- README.md | 3 +++ cli.js | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 325581c..6dde25a 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/cli.js b/cli.js index a1efc3f..e241086 100755 --- a/cli.js +++ b/cli.js @@ -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) @@ -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)