1
+ /** @module index */
1
2
'use strict' ;
2
3
3
- var doctrine = require ( 'doctrine' ) ;
4
+
5
+ // Dependencies
4
6
var fs = require ( 'fs' ) ;
5
- var jsYaml = require ( 'js-yaml' ) ;
6
7
var path = require ( 'path' ) ;
8
+ var doctrine = require ( 'doctrine' ) ;
9
+ var jsYaml = require ( 'js-yaml' ) ;
7
10
var swaggerTools = require ( 'swagger-tools' ) ;
8
11
9
- // This is the Swagger object that conforms to the Swagger 2.0 specification.
10
- module . exports . swaggerObject = [ ] ;
11
-
12
-
13
12
14
13
/**
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
18
19
*/
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
+
20
27
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 ) ;
31
36
}
32
37
}
33
38
34
- return jsDocs ;
39
+ return jsDocComments ;
35
40
}
36
41
37
42
38
-
39
-
40
43
/**
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
45
49
*/
46
- function filterSwaggerTags ( jsDocs ) {
47
- var swaggerJsDocs = [ ] ;
50
+ function filterJsDocComments ( jsDocComments ) {
51
+ var swaggerJsDocComments = [ ] ;
48
52
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 ] ;
53
57
if ( tag . title === 'swagger' ) {
54
- swaggerJsDocs . push ( jsYaml . safeLoad ( tag . description ) ) ;
58
+ swaggerJsDocComments . push ( jsYaml . safeLoad ( tag . description ) ) ;
55
59
}
56
60
}
57
61
}
58
62
59
- return swaggerJsDocs ;
63
+ return swaggerJsDocComments ;
60
64
}
61
65
66
+
62
67
/**
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'
65
72
*/
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 ) {
74
74
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 ) ;
77
77
78
78
for ( var j = 0 ; j < propertyNames . length ; j = j + 1 ) {
79
79
var propertyName = propertyNames [ j ] ;
80
- module . exports . swaggerObject . paths [ propertyName ] = paths [ propertyName ] ;
80
+ swaggerObject . paths [ propertyName ] = pathObject [ propertyName ] ;
81
81
}
82
82
}
83
83
}
84
84
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
-
111
85
86
+ // This is the Swagger object that conforms to the Swagger 2.0 specification.
87
+ module . exports . swaggerObject = [ ] ;
112
88
113
89
114
90
/**
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
118
96
*/
119
97
module . exports . init = function ( app , options ) {
120
98
if ( ! options ) {
@@ -132,7 +110,9 @@ module.exports.init = function(app, options) {
132
110
133
111
// Parse the documentation in the APIs array.
134
112
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 ) ;
136
116
}
137
117
138
118
var swaggerToolsUIOptions = {
0 commit comments