1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var fs = require('fs');
|
12 | var nunjucks = require('nunjucks');
|
13 | var lodash = require('lodash');
|
14 | var jsonRefs = require('json-refs');
|
15 |
|
16 | nunjucks.configure({ autoescape: true });
|
17 |
|
18 | module.exports = function (grunt) {
|
19 |
|
20 |
|
21 | grunt.registerMultiTask('static_api_docs', '', function () {
|
22 |
|
23 |
|
24 | var options = this.options({});
|
25 |
|
26 |
|
27 |
|
28 | if(!this.data.hasOwnProperty('src')){
|
29 | grunt.log.warn('No "src" property in target: "' + this.target + '".');
|
30 | return false;
|
31 | }
|
32 | if(!this.data.hasOwnProperty('dest')){
|
33 | grunt.log.warn('No "dest" property in target: "' + this.target + '".');
|
34 | return false;
|
35 | }
|
36 | if (!grunt.file.exists(this.data.src)) {
|
37 | grunt.log.warn('Source file "' + this.data.src + '" not found.');
|
38 | return false;
|
39 | }
|
40 |
|
41 |
|
42 | var swaggerJSON = require(process.cwd() + '/' + this.data.src);
|
43 | var apiModel = jsonRefs.resolveLocalRefs(swaggerJSON).resolved;
|
44 |
|
45 | var fileName = options.filename || "api-doc";
|
46 | var markdownOutputFile = this.data.dest + '/' + fileName + ".md";
|
47 | var htmlOutputFile = this.data.dest + '/' + fileName + ".html";
|
48 |
|
49 |
|
50 | lodash.forIn(apiModel.paths, function (path) {
|
51 |
|
52 | lodash.forIn(path, function (verb) {
|
53 |
|
54 | lodash.forIn(verb.responses, function (response) {
|
55 |
|
56 | response.schemaArr = [];
|
57 |
|
58 | recurvsiveFlatten(undefined, response.schema, response.schemaArr, -1);
|
59 |
|
60 | });
|
61 | });
|
62 | });
|
63 |
|
64 | grunt.file.write(markdownOutputFile, nunjucks.render(__dirname + '/../templates/snippet.md', apiModel));
|
65 | grunt.file.write(htmlOutputFile, nunjucks.render(__dirname + '/../templates/shell.html', apiModel));
|
66 | grunt.log.writeln('Files created.');
|
67 |
|
68 | });
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function recurvsiveFlatten(name, obj, arr, depth){
|
75 |
|
76 | var type;
|
77 |
|
78 |
|
79 | if (obj.hasOwnProperty('items')) {
|
80 |
|
81 | type = obj.items.type;
|
82 |
|
83 |
|
84 | if (obj.type === 'array') {
|
85 | type = lodash.capitalize(type) + '[]';
|
86 | }
|
87 |
|
88 |
|
89 | arr.push({name: name, type: type, depth: depth});
|
90 |
|
91 |
|
92 | lodash.forIn(obj.items.properties, function (val, key) {
|
93 | recurvsiveFlatten(key, val, arr, depth + 1);
|
94 | });
|
95 |
|
96 | } else if (obj.hasOwnProperty('properties')) {
|
97 |
|
98 |
|
99 | arr.push({name: name, type: 'Object', depth: depth});
|
100 |
|
101 |
|
102 | lodash.forIn(obj.properties, function(val, key){
|
103 | recurvsiveFlatten(key, val, arr, depth + 1);
|
104 | });
|
105 | } else {
|
106 |
|
107 |
|
108 | arr.push({name: name, type: obj.type, description: obj.description, depth: depth});
|
109 | }
|
110 |
|
111 | }
|