From 646a7e51a79430192320b25e5f178349df9d2353 Mon Sep 17 00:00:00 2001 From: Paul Mundt Date: Sat, 5 Jan 2019 14:59:17 +0100 Subject: [PATCH] Support swagger spec files in YAML format --- README.md | 25 ++++++++++++++++++++----- bin/swagger2pdf | 30 +++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a127abb..db52d68 100644 --- a/README.md +++ b/README.md @@ -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) @@ -72,4 +87,4 @@ If you want to use some other version: ``` -Done, just use it. \ No newline at end of file +Done, just use it. diff --git a/bin/swagger2pdf b/bin/swagger2pdf index 1f904d1..6c29b87 100755 --- a/bin/swagger2pdf +++ b/bin/swagger2pdf @@ -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.' @@ -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 @@ -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;