Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component): Adds a cli interface to the utility via json2md #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in package name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 6a4033d


Use it like this:

```js
Expand All @@ -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:

Expand Down
61 changes: 61 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -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);
});

}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down