-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit-docs-generate-readme.js
147 lines (125 loc) · 4.58 KB
/
bit-docs-generate-readme.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
138
139
140
141
142
143
144
145
146
147
var Q = require("q");
var fs = require("fs");
var path = require("path");
var readFile = Q.denodeify(fs.readFile),
writeFile = Q.denodeify(fs.writeFile);
var _ = require("lodash");
var Handlebars = require("handlebars").create();
var makeHelpers = require("./helpers");
var mkdir = Q.denodeify(fs.mkdir);
module.exports = function(docMapPromise, siteConfig){
siteConfig = _.cloneDeep(siteConfig);
var apisPromise;
if( Array.isArray(siteConfig.readme.apis) ) {
apisPromise = Q(siteConfig.readme.apis);
} else {
var apisPath = path.join( siteConfig.cwd, siteConfig.readme.apis);
apisPromise = readFile( apisPath ).then(function(apisSource){
return JSON.parse(""+apisSource);
});
}
return Q.all([
docMapPromise,
readFile( path.join( __dirname, 'templates', 'readme.mustache') ),
readFile( path.join( __dirname, 'templates', 'signature.mustache') ),
readFile( path.join( __dirname, 'templates', 'types.mustache') ),
apisPromise
]).then(function(result){
var template = Handlebars.compile(result[1].toString()),
docMap = result[0],
apis = result[4];
siteConfig.readme.apis = apis;
Handlebars.registerPartial("signature.mustache", result[2].toString());
Handlebars.registerPartial("types.mustache", result[3].toString());
var helpers = makeHelpers(docMap, siteConfig);
_.forEach(helpers, function(helper, helperName){
Handlebars.registerHelper(helperName, helper);
});
var outlineEntities = makeDocEntities(apis,0,docMap);
var entities = makeDocEntities(apis,0,docMap, true);
var out = template({
outlineEntities: outlineEntities,
entities: entities
});
//writeFile(path.join(siteConfig.dest,"docMap.json"), JSON.stringify(result[0]));
return mkdir(siteConfig.dest).then(function(){
return writeFile(path.join(siteConfig.dest,"GREADME.md"), out)
});
});
};
function getDepth (name, docMap) {
var matches = {};
var depth = 0;
var docObject = docMap[name];
matches[name] = true;
while(docObject) {
depth++;
name = docObject.parent;
if(!matches[name]) {
docObject = docMap[name];
matches[name] = true;
} else {
docObject = null;
}
}
return depth;
}
function makeDocEntity(name, depth, docMap, ignoreGroups) {
var entities = [];
var docObject = docMap[name];
if(!docObject) {
docObject = {type: "group", name: name};
} else {
docObject = _.cloneDeep(docObject);
}
docObject.depth = depth;
if(docObject.signatures) {
if(docObject.type === "module") {
entities.push(docObject);
// only increase the depth for the signatures we will add under this module
if(docObject.signatures.length) {
depth++;
}
}
docObject.signatures.forEach(function(signature){
entities.push(signature);
signature.depth = depth;
//signature.docObject = docObject;
});
} else if( docObject.types ) {
docObject.isTyped = true;
entities.push(docObject);
docObject.types.forEach(function(type){
type.depth = depth+1;
});
} else if(docObject.type === "group") {
if(!ignoreGroups) {
entities.push(docObject);
}
} else {
console.log("unable to process",docObject);
}
return {entities: entities, depth: depth};
}
function makeDocEntities(apis, depth, docMap, ignoreGroups) {
var entities = [];
apis.forEach(function(name){
if(typeof name !== "string") {
_.forEach(name, function(apis, name){
var isGroup = !docMap[name] || (docMap[name] && docMap[name].type === "group");
if(isGroup && ignoreGroups) {
entities.push.apply(entities, makeDocEntities(apis, depth, docMap, ignoreGroups) );
} else {
var result = makeDocEntity(name, depth, docMap, ignoreGroups);
entities.push.apply(entities, result.entities );
entities.push.apply(entities, makeDocEntities(apis, result.depth+1, docMap, ignoreGroups) );
}
});
} else {
// make one docObject's entities
var result = makeDocEntity(name, depth, docMap, ignoreGroups);
entities.push.apply(entities, result.entities);
}
});
return entities;
}