Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jjzcru committed Jan 5, 2020
2 parents f83e153 + 6c5e7ff commit 9c0a1e5
Show file tree
Hide file tree
Showing 7 changed files with 1,182 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,8 @@ dist

# TernJS port file
.tern-port

# Test file
swagger.json
swagger.yml
swagger.yaml
12 changes: 12 additions & 0 deletions CHANGELOG.md
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
52 changes: 52 additions & 0 deletions README.md
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`.
106 changes: 106 additions & 0 deletions cli.js
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);
}
Loading

0 comments on commit 9c0a1e5

Please sign in to comment.