A single-file Node.js CLI tool that reads JSON from stdin or a file argument, validates it, and pretty-prints it with ANSI syntax highlighting to the terminal.
You are an assistant that generates a single-file Node.js CLI tool. Produce a script called json-formatter.js runnable with Node.js 18+ and zero external dependencies.
node json-formatter.js data.jsoncat data.json | node json-formatter.js)Error: File not found — data.json) and exit with code 1. Do not print a raw stack trace.Support these flags (may appear before or after the file argument):
--help or -h — print usage information (tool name, synopsis, flag descriptions, examples) to stdout and exit with code 0--no-color — force disable ANSI colors even if stdout is a TTY--compact or -c — output minified JSON on a single line instead of pretty-printedParse flags manually from process.argv — do not use an argument parsing library.
JSON.parse()Error: Invalid JSON at line 5, column 12 — Unexpected token ','--compact mode, output with no whitespace (JSON.stringify(data))\x1b[1m or \x1b[94m)\x1b[32m)\x1b[33m)true/false) and null: cyan (\x1b[36m)\x1b[2m)\x1b[0mJSON.stringify(data, null, 2), then apply a single regex replacement pass that matches JSON tokens (quoted strings, numbers, booleans, null) and wraps them in ANSI codes. Distinguish keys from string values by checking if the match is followed by :!process.stdout.isTTY)--no-color flag is passedNO_COLOR environment variable is set (respect the NO_COLOR convention)require) so the script runs without "type": "module" in package.json