diff --git a/.jshintrc b/.jshintrc index 0f986d60..f6a71c78 100644 --- a/.jshintrc +++ b/.jshintrc @@ -58,6 +58,6 @@ "prototypejs": false, "maxparams": 4, "maxdepth": 4, - "maxstatements": 19, + "maxstatements": 20, "maxcomplexity": 8 } diff --git a/example/app.js b/example/app.js index 29510ce5..c2546637 100644 --- a/example/app.js +++ b/example/app.js @@ -5,6 +5,7 @@ var express = require('express'); var bodyParser = require('body-parser'); var routes = require('./routes'); +var routes2 = require('./routes2'); var swaggerJSDoc = require('../'); @@ -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'], }; @@ -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; diff --git a/example/routes2.js b/example/routes2.js new file mode 100644 index 00000000..04f6073c --- /dev/null +++ b/example/routes2.js @@ -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)!'); + }); +}; diff --git a/lib/index.js b/lib/index.js index 5d8dd38a..3cb8c0cc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'); @@ -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); @@ -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 @@ -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); @@ -166,6 +182,5 @@ module.exports = function(options) { swaggerObject = api; } }); - return swaggerObject; }; diff --git a/package.json b/package.json index 3ac29283..ac0a8cc8 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" }, diff --git a/test/swagger-spec.json b/test/swagger-spec.json index 21d7e82b..10a21f7f 100644 --- a/test/swagger-spec.json +++ b/test/swagger-spec.json @@ -75,6 +75,16 @@ } } } + }, + "/hello": { + "get": { + "description": "Returns the homepage", + "responses": { + "200": { + "description": "hello world" + } + } + } } }, "definitions": {