Skip to content

Commit

Permalink
Merge pull request #20 from jonboiser/master
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
chdanielmueller committed Apr 7, 2016
2 parents e42fb0c + 0af0c5d commit 7d9aa00
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@
"prototypejs": false,
"maxparams": 4,
"maxdepth": 4,
"maxstatements": 19,
"maxstatements": 20,
"maxcomplexity": 8
}
5 changes: 3 additions & 2 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var express = require('express');
var bodyParser = require('body-parser');
var routes = require('./routes');
var routes2 = require('./routes2');
var swaggerJSDoc = require('../');


Expand Down Expand Up @@ -35,7 +36,7 @@ var options = {
// Import swaggerDefinitions
swaggerDefinition: swaggerDefinition,
// Path to the API docs
apis: ['./example/routes.js', './example/parameters.yaml'],
apis: ['./example/routes*.js', './example/parameters.yaml'],
};


Expand All @@ -51,7 +52,7 @@ app.get('/api-docs.json', function(req, res) {

// Set up the routes
routes.setup(app);

routes2.setup(app);

// Expose app
exports = module.exports = app;
Expand Down
17 changes: 17 additions & 0 deletions example/routes2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';


module.exports.setup = function(app) {
/**
* @swagger
* /hello:
* get:
* description: Returns the homepage
* responses:
* 200:
* description: hello world
*/
app.get('/hello', function(req, res) {
res.send('Hello World (Version 2)!');
});
};
23 changes: 19 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// Dependencies
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var doctrine = require('doctrine');
var jsYaml = require('js-yaml');
Expand All @@ -17,7 +18,6 @@ var parser = require('swagger-parser');
* @requires doctrine
*/
function parseApiFile(file) {

var jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm;
var fileContent = fs.readFileSync(file, { encoding: 'utf8' });
var ext = path.extname(file);
Expand Down Expand Up @@ -126,6 +126,20 @@ function addDataToSwaggerObject(swaggerObject, data) {
}
}

/**
* Converts an array of globs to full paths
* @function
* @param {array} globs - Array of globs and/or normal paths
* @return {array} Array of fully-qualified paths
* @requires glob
*/
function convertGlobPaths(globs) {
return globs.reduce(function(acc, globString) {
var globFiles = glob.sync(globString);
return acc.concat(globFiles);
}, []);
}

/**
* Generates the swagger spec
* @function
Expand Down Expand Up @@ -153,9 +167,11 @@ module.exports = function(options) {
swaggerObject.parameters = {};
swaggerObject.securityDefinitions = {};

var apiPaths = convertGlobPaths(options.apis);

// Parse the documentation in the APIs array.
for (var i = 0; i < options.apis.length; i = i + 1) {
var files = parseApiFile(options.apis[i]);
for (var i = 0; i < apiPaths.length; i = i + 1) {
var files = parseApiFile(apiPaths[i]);
var swaggerJsDocComments = filterJsDocComments(files.jsdoc);
addDataToSwaggerObject(swaggerObject, files.yaml);
addDataToSwaggerObject(swaggerObject, swaggerJsDocComments);
Expand All @@ -166,6 +182,5 @@ module.exports = function(options) {
swaggerObject = api;
}
});

return swaggerObject;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-jsdoc",
"version": "1.2.1",
"version": "1.3.0",
"description": "Generates swagger doc based on JSDoc",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,7 @@
"homepage": "https://github.com/Surnet/swagger-jsdoc",
"dependencies": {
"doctrine": "^1.2.0",
"glob": "^7.0.3",
"js-yaml": "^3.5.3",
"swagger-parser": "^3.4.0"
},
Expand Down
10 changes: 10 additions & 0 deletions test/swagger-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
}
}
}
},
"/hello": {
"get": {
"description": "Returns the homepage",
"responses": {
"200": {
"description": "hello world"
}
}
}
}
},
"definitions": {
Expand Down

0 comments on commit 7d9aa00

Please sign in to comment.