File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ are also very welcome.
1212
1313Install 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+
1517Use it like this:
1618
1719``` js
@@ -20,6 +22,22 @@ var schema = // An object that is a valid JSON Schema
2022var 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+
2341There are plenty of examples in the [ test folder] ( ./test ) , but a very
2442simple example would be as follows:
2543
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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"
You can’t perform that action at this time.
0 commit comments