-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (116 loc) · 3.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var color = require("colorize"),
_ = require("underscore")._,
fs = require("fs"),
path = require("path");
/*
restDocDir = appDir + "/public/feather-res-cache/_rest",
*/
module.exports = {
/**
* Generates schema objects for each feather rest interface found.
*
* @param options available options:
* restFolder: the folder to search for rest files in
* interface: the single file to generate a schema for. If omitted, generates for all js files in the folder.
* @param cb
*/
generateSchemas: function(options, cb) {
var restDir = options.restFolder,
ifc = options.interface || "all",
restFiles = getFiles(restDir);
if (ifc !== "all") {
if (! _.include(restFiles, restDir + '/' + ifc + ".js")) {
cb(ifc + " interface not found.");
return;
} else {
restFiles = [restDir + '/' + ifc + ".js"];
}
}
console.log('Generating REST docs for ' + color.ansify("#bold[#cyan[" + ifc + "]]") + " interface(s).");
// Ensure the directory tree is created.
// mkdirpSync(restDocDir);
var schemas = [];
_.each(restFiles, function(restFile) {
var fileData = fs.readFileSync(restFile, 'UTF-8').split('\n');
// Parse the file data
//var fileObj = require(restFile);
var schema = {
name: path.basename(restFile, '.js'),
endpoints: []
},
buf = "",
methodPtn = /^('|")(get|post|delete|put|head)('|"): \{/i,
commentStartPtn = /^\/\*\{$/,
commentEndPtn = /^\}\*\/$/,
endpointPtn = /(('|")(\/.*)('|")): /,
inSchemaComment = false,
schemaComment = {},
currMethod = "",
tmp;
_.each(fileData, function(line) {
var l = line.trim();
if (methodPtn.test(l)) {
currMethod = l.match(methodPtn)[2];
} else if (commentStartPtn.test(l)) {
inSchemaComment = true;
schemaComment = "{";
} else if (commentEndPtn.test(l)) {
inSchemaComment = false;
schemaComment = JSON.parse(schemaComment+"}");
} else if (!inSchemaComment && endpointPtn.test(l)) {
tmp = l.match(endpointPtn)[3];
schemaComment.uri = tmp;
schemaComment.verb = currMethod;
switch(currMethod) {
case 'post':
schemaComment.verbLabel = 'success';
break;
case 'put':
schemaComment.verbLabel = 'warning';
break;
case 'delete':
schemaComment.verbLabel = 'important';
break;
default:
schemaComment.verbLabel = 'info';
break;
}
schema.endpoints.push(schemaComment);
schemaComment = {};
} else if (inSchemaComment) {
schemaComment += l;
}
});
schemas.push(schema);
});
cb(null, schemas);
}
};
function getFiles(dir){
var files = [];
debugger;
try {
fs.statSync(dir);
} catch (ex){
return [];
}
function traverse(dir, stack){
console.log("Traversing " + dir);
stack.push(dir);
fs.readdirSync(stack.join("/")).forEach(function(file){
debugger;
var path = stack.concat([file]).join("/");
var stat = fs.statSync(path);
if (file[0] == ".") {
return;
} else if (stat.isFile() && /\.js$/.test(file)){
files.push(path);
} else if (stat.isDirectory()){
traverse(file, stack);
}
});
stack.pop();
}
traverse(dir, []);
return files;
}