-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,8 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Test file | ||
swagger.json | ||
swagger.yml | ||
swagger.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
## [0.1.0] - 2020-01-05 | ||
|
||
### Added | ||
- First release of the project | ||
- CHANGELOG.md file | ||
- README.md file | ||
|
||
[0.1.0]: https://github.com/jjzcru/swagger-ui-loader/releases/tag/v0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## Installation | ||
|
||
### Npm | ||
```console | ||
npm install swagger-ui-loader --save-dev | ||
``` | ||
|
||
### Yarn | ||
```console | ||
yarn add swagger-ui-loader --dev | ||
``` | ||
|
||
## Usage | ||
Add a run script that executes `swagger-ui-loader`. Your `package.json` should look similar to this. Now just run `npm run swagger`. | ||
|
||
```json | ||
{ | ||
"name": "i-am-your-package🤖", | ||
"scripts": { | ||
"swagger": "swagger-ui-loader" | ||
}, | ||
"devDependencies": { | ||
"swagger-ui-loader": "^0.1.0" | ||
} | ||
} | ||
``` | ||
|
||
Be default it will search for `swagger.json`, `swagger.yml` or `swagger.yaml` on the root directory of the project and it will run a server on port `5466`. | ||
|
||
You can add a property `swagger` to `package.json` with the following properties: | ||
|
||
- `file`: The path on which the file with the swagger data is located. | ||
- `port`: The port on which you want the server to run. | ||
|
||
The `package.json` will look like this with those properties. | ||
```json | ||
{ | ||
"name": "i-am-your-package🤖", | ||
"scripts": { | ||
"swagger": "swagger-ui-loader" | ||
}, | ||
"swagger": { | ||
"file": "./docs/swagger.yml", | ||
"port": 8888, | ||
}, | ||
"devDependencies": { | ||
"swagger-ui-loader": "^0.1.0" | ||
} | ||
} | ||
``` | ||
|
||
If you run `npm run swagger` it will search for the `swagger.yml` inside the `docs` directory and the server will run on port `8888`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#!/usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const open = require('open'); | ||
const express = require('express'); | ||
|
||
const swaggerUi = require('swagger-ui-express'); | ||
const YAML = require('yamljs'); | ||
|
||
const app = express(); | ||
const port = getPort(); | ||
const swaggerFile = getTargetFile(); | ||
|
||
let swaggerDocument; | ||
switch(swaggerFile.ext) { | ||
case '.yaml': | ||
case '.yml': | ||
swaggerDocument = YAML.load(swaggerFile.file); | ||
break; | ||
default: | ||
swaggerDocument = require(swaggerFile.file); | ||
} | ||
|
||
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | ||
|
||
app.listen(port, async () => { | ||
try { | ||
console.log('Swagger UI Server listening on port \x1b[36m%s\x1b[0m', `${port}`); | ||
await open(`http://localhost:${port}`); | ||
} catch(e) { | ||
throwError(e.message); | ||
} | ||
}); | ||
|
||
function getPort() { | ||
if(fs.existsSync('./package.json')) { | ||
const pkg = require('./package.json'); | ||
if(!!pkg.swagger && !!pkg.swagger.port) { | ||
return parseInt(pkg.swagger.port); | ||
} | ||
} | ||
|
||
return 5466; | ||
} | ||
|
||
function getTargetFile() { | ||
let filePath = getFileFromPkg(); | ||
|
||
// The file is set in the configuration file | ||
if(!!filePath && fs.existsSync(filePath)) { | ||
const ext = path.extname(filePath); | ||
switch(ext) { | ||
case '.yml': | ||
case '.yaml': | ||
case '.json': | ||
return { | ||
file: filePath, | ||
ext: ext | ||
} | ||
default: | ||
throwError(`File extension '${ext}' is not valid`); | ||
} | ||
|
||
throwError(`Swagger configuration file '${filePath}' do not exist`); | ||
} | ||
|
||
// I search for 'swagger.yml', 'swagger.yaml' or 'swagger.json' as default value | ||
const defaultPath = getDefaultPath(); | ||
if(!!defaultPath) { | ||
return defaultPath; | ||
} | ||
|
||
// If no file is found it throw an error | ||
throwError(`Swagger configuration file 'swagger.json', 'swagger.yml' or 'swagger.yaml' not found`); | ||
} | ||
|
||
function getFileFromPkg() { | ||
if(fs.existsSync('./package.json')) { | ||
const pkg = require('./package.json'); | ||
if(!!pkg.swagger && !!pkg.swagger.file) { | ||
return pkg.swagger.file; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function getDefaultPath() { | ||
const defaultFiles = ['./swagger.json', './swagger.yml', './swagger.yaml']; | ||
for(const file of defaultFiles) { | ||
if(fs.existsSync(file)) { | ||
const ext = path.extname(file); | ||
return { | ||
file: file, | ||
ext: ext | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function throwError(message) { | ||
console.log('\x1b[31m%s\x1b[0m', message); | ||
process.exit(1); | ||
} |
Oops, something went wrong.