From 8cbe76d994a45e4bb43250d04df3e3e7ec5aa483 Mon Sep 17 00:00:00 2001 From: Chris Paterson Date: Tue, 31 Dec 2019 13:32:50 -0800 Subject: [PATCH 1/2] feat(component): Adds a cli interface to the utility via json2md Adds cli.js aliased when install in the package.json to json2md in the bin field, takes an input json file, stdin json and outputs to stdout or specified 'md' file. --- README.md | 18 ++++++++++++++++ cli.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +++ 3 files changed, 82 insertions(+) create mode 100755 cli.js diff --git a/README.md b/README.md index 30adb56..3f73a29 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ are also very welcome. Install the normal way: `npm install json-schema-to-markdown` +or if using cli, install globally: `npm install -g json-schema-to-markdwon` + Use it like this: ```js @@ -20,6 +22,22 @@ var schema = // An object that is a valid JSON Schema var markdown = parse(schema) ``` +### CLI + +`json2md /path/to/schema_file.json` + +**NOTE: input file must end with .json** + +or + +`cat /path/to/schema_file.json | json2md` + +default output is to stdout but you may specify an output file + +`json2md /path/to/schema_file.json /path/to/output.md` + +**NOTE: output file must end with .md** + There are plenty of examples in the [test folder](./test), but a very simple example would be as follows: diff --git a/cli.js b/cli.js new file mode 100755 index 0000000..6fe3933 --- /dev/null +++ b/cli.js @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +var parse = require('./index') +var fs = require("fs"); +var args = process.argv.slice(2); +var inputFile; +var outputFile; + +var out = function(output) { + + output = parse(JSON.parse(output)); + + if(outputFile) { + + fs.writeFileSync(outputFile, output); + console.log("created " + outputFile); + + } else { + + console.log(output); + + } +}; + +for(var i =0; i < args.length; i++) { + if(~args[i].indexOf('.json')) { + inputFile = args[i]; + } else if(~args[i].indexOf('.md')) { + outputFile = args[i]; + } +} + +if(inputFile) { + + if (fs.existsSync(inputFile)) { + + out(fs.readFileSync(inputFile, 'utf8')); + + } else { + + throw new Error("File does not exist: " + inputFile); + + } + +} else if (process.stdin) { + + var schema = ''; + process.stdin.setEncoding('utf8'); + process.stdin.on('readable', () => { + let chunk; + // Use a loop to make sure we read all available data. + while ((chunk = process.stdin.read()) !== null) { + schema += chunk; + } + }); + + process.stdin.on('end', () => { + out(schema); + }); + +} diff --git a/package.json b/package.json index e53a3b0..0dd0ca4 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "1.1.1", "description": "Turn a JSON Schema into a markdown file.", "main": "index.js", + "bin": { + "json2md": "./cli.js" + }, "scripts": { "commit": "git-cz", "test": "tape test/*.js" From 6a4033dca9f602fe31f7f8c1465e3043850a6e26 Mon Sep 17 00:00:00 2001 From: Chris Paterson <1268493+chrispaterson@users.noreply.github.com> Date: Fri, 24 Jan 2020 17:34:52 -0800 Subject: [PATCH 2/2] fixing typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f73a29..6310364 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ are also very welcome. Install the normal way: `npm install json-schema-to-markdown` -or if using cli, install globally: `npm install -g json-schema-to-markdwon` +or if using cli, install globally: `npm install -g json-schema-to-markdown` Use it like this: