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

Support swagger spec files in YAML format #13

Merged
merged 1 commit into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,46 @@
npm install -g swagger-spec-to-pdf

### Usage:
usage: swagger2pdf [-h] [-v] [-s SRC] [-o OUTPUT]

usage: swagger2pdf [-h] [-v] [-s SRC] [-j] [-y] [-o OUTPUT]

Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-s SRC, --src SRC Swagger spec JSON file.
-j, --json Spec file in JSON format (default).
-y, --yaml Spec file in YAML format.
-o OUTPUT, --output OUTPUT
Output path of the pdf file.

### Exec:
swagger2pdf -s ./swagger-spec-sample.json -o ./dump/
&
Wait several seconds

For JSON files:

swagger2pdf -j -s ./swagger-spec-sample.json -o ./dump/

For YAML files:

swagger2pdf -y -s ./swagger-spec-sample.yaml -o ./dump/

In either case, the execution will take several seconds to complete.

### Sample:
See: ./dump/swagger-spec-sample.pdf

### Appendix:
#### Working Theory

In the case of a JSON input file:

1. Read source swagger json spec
2. Convert it into yaml format
3. Dump yaml into the spec dir of swagger editor
4. Use electron to render swagger editor page (nicely formatted api page)
5. Dump the page into pdf

In the case of a YAML input file, steps 1-2 are bypassed.

#### Swagger Editor
Version of swagger editor embedded is: 2.9.9
You can get it from: [here](https://github.com/swagger-api/swagger-editor)
Expand Down Expand Up @@ -72,4 +87,4 @@ If you want to use some other version:
</script>
```

Done, just use it.
Done, just use it.
30 changes: 25 additions & 5 deletions bin/swagger2pdf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ var cli = new ArgumentParser({
addHelp: true
});
cli.addArgument(['-s', '--src'], {
help: 'Swagger spec JSON file.'
help: 'Swagger spec file.'
});
cli.addArgument(['-j', '--json'], {
help: 'Spec file in JSON format (default).',
nargs: 0,
});
cli.addArgument(['-y', '--yaml'], {
nargs: 0,
help: 'Spec file in YAML format.',
});
cli.addArgument(['-o', '--output'], {
help: 'Output path of the pdf file.'
Expand Down Expand Up @@ -49,11 +57,18 @@ try {
var options = cli.parseArgs();
var src = parsePath(options.src);
var output = parsePath(options.output);
var inputJson = !options.yaml;

// ensure only a single input format is selected, default to JSON otherwise
if (options.yaml && options.json) {
process.stderr.write("Both JSON and YAML selected, continuing as JSON");
inputJson = true;
}

// ensure source file
var srcStatus = libFs.statSync(src);
if (!srcStatus || !srcStatus.isFile()) {
process.stderr.write('Source swagger spec json not found: ' + src);
process.stderr.write('Source swagger spec not found: ' + src);
}

// ensure output dir
Expand All @@ -77,9 +92,14 @@ try {
// });
// });

// read json spec & dump converted yaml into swagger editor spec dir
var specJson = JSON.parse(libFs.readFileSync(src));
libFs.writeFileSync(libPath.join(editorSpecPath, 'default.yaml'), libYaml.dump(specJson));
if (inputJson) {
// read json spec & dump converted yaml into swagger editor spec dir
var specJson = JSON.parse(libFs.readFileSync(src));
libFs.writeFileSync(libPath.join(editorSpecPath, 'default.yaml'), libYaml.dump(specJson));
} else {
// if the source file is in YAML format already, simply copy it to the spec dir
libFs.copyFileSync(src, libPath.join(editorSpecPath, 'default.yaml'));
}

// start http server serving the editor site
var httpServerSpawned = false;
Expand Down