forked from moleculerjs/site
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__api-docs-generator-html.js
134 lines (110 loc) · 3.31 KB
/
__api-docs-generator-html.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
/* eslint-disable no-console */
"use strict";
const documentation = require("documentation");
const fs = require("fs");
const path = require("path");
const pug = require("pug");
const semver = require("semver");
const marked = require("marked");
const hljs = require("highlight.js");
const mkdir = require("mkdirp").sync;
const Promise = require("bluebird");
const template = pug.compileFile(path.join(__dirname, "api-template", "index.pug"), {
pretty: true,
//debug: true
});
function isFunction(section) {
return (
section.kind === "function" ||
(section.kind === "typedef" &&
section.type.type === "NameExpression" &&
section.type.name === "Function")
);
}
function formatParameters(section, short) {
if (section.params) {
let res = section.params.map(param => formatParameter(param, short)).join(", ");
return `(${res})`;
}
return "()";
}
function formatParameter(param, short) {
if (short) {
if (param.type && param.type.type == "OptionalType") {
if (param.default) {
return param.name + " = " + param.default;
}
return param.name + "?";
}
return param.name;
}
if (param.type && param.type.name)
return param.name + ": " + param.type.name.replace(/\n/g, "");
return param.name;
}
function codeHighlight(code) {
return "<pre class='hljs'>" + hljs.highlightAuto(code, "js") + "</pre>";
}
const pkg = require("moleculer/package.json");
const apiVersion = semver.major(pkg.version) + "." + semver.minor(pkg.version);
console.log("API version:", apiVersion);
const sourceFiles = [
{ path: "service-broker.js", name: "ServiceBroker" },
{ path: "service.js", name: "Service" },
{ path: "context.js", name: "Context" }
]
const targetFolder = path.join(".", "source", "api-" + apiVersion);
console.log("Target folder:", targetFolder);
mkdir(targetFolder);
Promise.each(sourceFiles, sourceFile => {
let f = path.join(__dirname, ".", "node_modules", "moleculer", "src", sourceFile.path);
return documentation.build([f], {
//access: ["public"]
shallow: true,
inferPrivate: '^_'
}).then(docs => {
const html = template({
docs,
title: "Context API documentation",
apiVersion,
md: marked,
getTag: (p, tags) => {
let res = tags.find(t => t.lineNumber == p.lineNumber);
return res;
},
getDesc: p => {
if (p.children.length > 0) {
let pp = p.children[0];
if (pp.type == "paragraph" && pp.children.length > 0) {
return pp.children[0].value;
}
}
},
hasValidParam: (params, tags) => {
if (params.length > 0) {
return params.filter(p => tags.find(t => t.lineNumber == p.lineNumber) != null).length > 0;
}
return false;
},
signature: section => {
let returns = "";
let prefix = "";
if (section.kind === "class") {
prefix = "new ";
} else if (!isFunction(section)) {
return section.name;
}
if (section.returns.length && section.returns[0].type) {
returns = ": " + section.returns[0].type.name;
}
return prefix + section.name + formatParameters(section) + returns;
},
highlight: code => hljs.highlight("js", code).value
});
fs.writeFileSync(path.join(targetFolder, sourceFile.path.replace(/\.[^/.]+$/, ".html")), html, "utf8");
console.log(`Done '${sourceFile.path}'!`);
}).catch(err => console.error(err))
}).then(() => {
console.log("All files done!");
process.exit(0);
});