Skip to content

Commit 8cbe76d

Browse files
Chris Patersonchrispaterson
authored andcommitted
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.
1 parent 7fdb257 commit 8cbe76d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ are also very welcome.
1212

1313
Install the normal way: `npm install json-schema-to-markdown`
1414

15+
or if using cli, install globally: `npm install -g json-schema-to-markdwon`
16+
1517
Use it like this:
1618

1719
```js
@@ -20,6 +22,22 @@ var schema = // An object that is a valid JSON Schema
2022
var markdown = parse(schema)
2123
```
2224

25+
### CLI
26+
27+
`json2md /path/to/schema_file.json`
28+
29+
**NOTE: input file must end with .json**
30+
31+
or
32+
33+
`cat /path/to/schema_file.json | json2md`
34+
35+
default output is to stdout but you may specify an output file
36+
37+
`json2md /path/to/schema_file.json /path/to/output.md`
38+
39+
**NOTE: output file must end with .md**
40+
2341
There are plenty of examples in the [test folder](./test), but a very
2442
simple example would be as follows:
2543

cli.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
var parse = require('./index')
4+
var fs = require("fs");
5+
var args = process.argv.slice(2);
6+
var inputFile;
7+
var outputFile;
8+
9+
var out = function(output) {
10+
11+
output = parse(JSON.parse(output));
12+
13+
if(outputFile) {
14+
15+
fs.writeFileSync(outputFile, output);
16+
console.log("created " + outputFile);
17+
18+
} else {
19+
20+
console.log(output);
21+
22+
}
23+
};
24+
25+
for(var i =0; i < args.length; i++) {
26+
if(~args[i].indexOf('.json')) {
27+
inputFile = args[i];
28+
} else if(~args[i].indexOf('.md')) {
29+
outputFile = args[i];
30+
}
31+
}
32+
33+
if(inputFile) {
34+
35+
if (fs.existsSync(inputFile)) {
36+
37+
out(fs.readFileSync(inputFile, 'utf8'));
38+
39+
} else {
40+
41+
throw new Error("File does not exist: " + inputFile);
42+
43+
}
44+
45+
} else if (process.stdin) {
46+
47+
var schema = '';
48+
process.stdin.setEncoding('utf8');
49+
process.stdin.on('readable', () => {
50+
let chunk;
51+
// Use a loop to make sure we read all available data.
52+
while ((chunk = process.stdin.read()) !== null) {
53+
schema += chunk;
54+
}
55+
});
56+
57+
process.stdin.on('end', () => {
58+
out(schema);
59+
});
60+
61+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "1.1.1",
44
"description": "Turn a JSON Schema into a markdown file.",
55
"main": "index.js",
6+
"bin": {
7+
"json2md": "./cli.js"
8+
},
69
"scripts": {
710
"commit": "git-cz",
811
"test": "tape test/*.js"

0 commit comments

Comments
 (0)