Skip to content

Commit f3449bb

Browse files
Refactoring
1 parent 617a73f commit f3449bb

File tree

1 file changed

+58
-78
lines changed

1 file changed

+58
-78
lines changed

lib/index.js

+58-78
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,98 @@
1+
/** @module index */
12
'use strict';
23

3-
var doctrine = require('doctrine');
4+
5+
// Dependencies
46
var fs = require('fs');
5-
var jsYaml = require('js-yaml');
67
var path = require('path');
8+
var doctrine = require('doctrine');
9+
var jsYaml = require('js-yaml');
710
var swaggerTools = require('swagger-tools');
811

9-
// This is the Swagger object that conforms to the Swagger 2.0 specification.
10-
module.exports.swaggerObject = [];
11-
12-
1312

1413
/**
15-
* Parses the jsDoc comments from JavaScript source code.
16-
* @param {string} sourceCode - Source code to parse
17-
* @returns {array}
14+
* Parses the provided API file for JSDoc comments.
15+
* @function
16+
* @param {string} file - File to be parsed
17+
* @returns {array} JSDoc comments
18+
* @requires doctrine
1819
*/
19-
function parseJsDoc(jsSourceCode) {
20+
function parseApiFile(file) {
21+
var fileExtension = path.extname(file);
22+
23+
if (fileExtension !== '.js') {
24+
throw new Error('Unsupported extension \'' + fileExtension + '\'.');
25+
}
26+
2027
var jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm;
21-
var fragments = jsSourceCode.match(jsDocRegex);
22-
var jsDocs = [];
23-
24-
if (fragments) {
25-
for (var i = 0; i < fragments.length; i = i + 1) {
26-
var fragment = fragments[i];
27-
var jsDoc = doctrine.parse(fragment, {
28-
unwrap: true,
29-
});
30-
jsDocs.push(jsDoc);
28+
var fileContent = fs.readFileSync(file, { encoding: 'utf8' });
29+
var regexResults = fileContent.match(jsDocRegex);
30+
31+
var jsDocComments = [];
32+
if (regexResults) {
33+
for (var i = 0; i < regexResults.length; i = i + 1) {
34+
var jsDocComment = doctrine.parse(regexResults[i], { unwrap: true });
35+
jsDocComments.push(jsDocComment);
3136
}
3237
}
3338

34-
return jsDocs;
39+
return jsDocComments;
3540
}
3641

3742

38-
39-
4043
/**
41-
* Filter out jsDoc comments that do not have '@swagger' tag
42-
* and parses the YAML description of those that do.
43-
* @param {array} jsDocs - jsDoc comments
44-
* @returns {array}
44+
* Filters JSDoc comments for those tagged with '@swagger'
45+
* @function
46+
* @param {array} jsDocComments - JSDoc comments
47+
* @returns {array} JSDoc comments tagged with '@swagger'
48+
* @requires js-yaml
4549
*/
46-
function filterSwaggerTags(jsDocs) {
47-
var swaggerJsDocs = [];
50+
function filterJsDocComments(jsDocComments) {
51+
var swaggerJsDocComments = [];
4852

49-
for (var i = 0; i < jsDocs.length; i = i + 1) {
50-
var jsDoc = jsDocs[i];
51-
for (var j = 0; j < jsDoc.tags.length; j = j + 1) {
52-
var tag = jsDoc.tags[j];
53+
for (var i = 0; i < jsDocComments.length; i = i + 1) {
54+
var jsDocComment = jsDocComments[i];
55+
for (var j = 0; j < jsDocComment.tags.length; j = j + 1) {
56+
var tag = jsDocComment.tags[j];
5357
if (tag.title === 'swagger') {
54-
swaggerJsDocs.push(jsYaml.safeLoad(tag.description));
58+
swaggerJsDocComments.push(jsYaml.safeLoad(tag.description));
5559
}
5660
}
5761
}
5862

59-
return swaggerJsDocs;
63+
return swaggerJsDocComments;
6064
}
6165

66+
6267
/**
63-
* Parse the JSDoc comments from a JavaScript file.
64-
* @param {string} file - File to parse
68+
* Adds the data in the Swagger JSDoc comments to the swagger object.
69+
* @function
70+
* @param {object} swaggerObject - Swagger object which will be written to
71+
* @param {array} swaggerJsDocComments - JSDoc comments tagged with '@swagger'
6572
*/
66-
function parseJsFile(file) {
67-
var sourceCode = fs.readFileSync(file, {
68-
encoding: 'utf8',
69-
});
70-
var jsDocComments = parseJsDoc(sourceCode);
71-
var swaggerJsDocComments = filterSwaggerTags(jsDocComments);
72-
73-
// Attach the findings to the Swagger object.
73+
function addDataToSwaggerObject(swaggerObject, swaggerJsDocComments) {
7474
for (var i = 0; i < swaggerJsDocComments.length; i = i + 1) {
75-
var paths = swaggerJsDocComments[i];
76-
var propertyNames = Object.getOwnPropertyNames(paths);
75+
var pathObject = swaggerJsDocComments[i];
76+
var propertyNames = Object.getOwnPropertyNames(pathObject);
7777

7878
for (var j = 0; j < propertyNames.length; j = j + 1) {
7979
var propertyName = propertyNames[j];
80-
module.exports.swaggerObject.paths[propertyName] = paths[propertyName];
80+
swaggerObject.paths[propertyName] = pathObject[propertyName];
8181
}
8282
}
8383
}
8484

85-
/**
86-
* Parses the provided API file and attaches the fields to the Swagger object.
87-
* @param {string} file - File to be parsed
88-
*/
89-
function parseApiFile(file) {
90-
var fileExtension = path.extname(file);
91-
92-
if (fileExtension === '.js') {
93-
parseJsFile(file);
94-
} else {
95-
throw new Error('Unsupported extension \'' + fileExtension + '\'.');
96-
}
97-
}
98-
99-
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
11185

86+
// This is the Swagger object that conforms to the Swagger 2.0 specification.
87+
module.exports.swaggerObject = [];
11288

11389

11490
/**
115-
* Initializes the module. This is intended to be called only once.
116-
* @param {object} app - Express application
117-
* @param {object} options - Configuration options
91+
* Initializes the module. This is intended to be called only once.
92+
* @function
93+
* @param {object} app - Express application
94+
* @param {object} options - Configuration options
95+
* @requires swagger-tools
11896
*/
11997
module.exports.init = function(app, options) {
12098
if (!options) {
@@ -132,7 +110,9 @@ module.exports.init = function(app, options) {
132110

133111
// Parse the documentation in the APIs array.
134112
for (var i = 0; i < options.apis.length; i = i + 1) {
135-
parseApiFile(options.apis[i]);
113+
var jsDocComments = parseApiFile(options.apis[i]);
114+
var swaggerJsDocComments = filterJsDocComments(jsDocComments);
115+
addDataToSwaggerObject(module.exports.swaggerObject, swaggerJsDocComments);
136116
}
137117

138118
var swaggerToolsUIOptions = {

0 commit comments

Comments
 (0)