-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtypedoc.js
133 lines (124 loc) · 4.57 KB
/
typedoc.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
const TypeDoc = require('typedoc');
const fs = require('fs');
const path = require('path');
const OUTPUT_DIR = 'docs';
const BASE_PATH = 'src';
/**
* Give the directory as parameter, will return every ts folders in it.
* @param {string} basePath a directory
* @returns
*/
function getAllModuleNamesInPath(basePath) {
const files = fs.readdirSync(basePath, {
withFileTypes: true
});
const paths = files.map((file) => basePath + file.name);
return paths.filter((p) => fs.existsSync(p));
}
function baseOrAbstractClassChecker(moduleName) {
return ['Base', 'Abstract'].some((a) => moduleName.includes(a));
}
function capitalize(s) {
return s[0].toUpperCase() + s.slice(1);
}
function flatten(lists) {
return lists.reduce((a, b) => a.concat(b), []);
}
function getDirectories(srcpath) {
return fs
.readdirSync(srcpath)
.map((file) => path.join(srcpath, file))
.filter((path) => fs.statSync(path).isDirectory());
}
function getDirectoriesRecursive(srcpath) {
const directories = getDirectories(srcpath);
const recursive = directories.map(getDirectoriesRecursive);
return [srcpath, ...flatten(recursive)];
}
function getModuleNamesWithPath(basePath) {
const directories = getDirectoriesRecursive(basePath);
let allPossibleEntryPoints = [];
directories.forEach((directory) => {
const separated = directory.split(path.sep);
if (separated.length > 1) {
// From all paths filter index.ts, MODULE_NAME.ts, MODULE_NAME-events.ts
// + add modules under primitive
const lastPath = separated[separated.length - 1];
const moduleNamed = directory + path.sep + lastPath + '.ts';
const indexNamed = directory + path.sep + 'index.ts';
const eventsNamed = directory + path.sep + lastPath + '-events.ts';
if (fs.existsSync(eventsNamed)) {
allPossibleEntryPoints.push(eventsNamed);
}
if (lastPath === 'primitive') {
const primitiveEntryPoints = getAllModuleNamesInPath(directory + path.sep);
allPossibleEntryPoints = allPossibleEntryPoints.concat(primitiveEntryPoints);
} else if (lastPath === 'shared') {
const sharedEntryPoints = getAllModuleNamesInPath(directory + path.sep);
allPossibleEntryPoints = allPossibleEntryPoints.concat(sharedEntryPoints);
} else if (separated[separated.length - 2] === 'shared') {
if (lastPath === 'ios') {
const iOSEntryPoints = getAllModuleNamesInPath(directory + path.sep);
allPossibleEntryPoints = allPossibleEntryPoints.concat(iOSEntryPoints);
} else if (lastPath === 'android') {
const androidEntryPoints = getAllModuleNamesInPath(directory + path.sep);
allPossibleEntryPoints = allPossibleEntryPoints.concat(androidEntryPoints);
}
}
if (fs.existsSync(moduleNamed)) {
allPossibleEntryPoints.push(moduleNamed);
} else if (fs.existsSync(indexNamed)) {
allPossibleEntryPoints.push(indexNamed);
}
}
});
return allPossibleEntryPoints;
}
async function main() {
const app = new TypeDoc.Application();
app.converter.on(TypeDoc.Converter.EVENT_RESOLVE_BEGIN, (context) => {
// Some extra sanity checks would be good here.
// context is of type Context, which typedoc <0.22 doesn't publicly export
context.project?.children?.forEach((submodule) => {
submodule.name = submodule.name.substr(submodule.name.lastIndexOf('/') + 1);
const oldDefault = submodule?.children?.find((reflection) => reflection.name === 'default' && reflection.kind === 128);
oldDefault?.children?.forEach((child) => {
submodule.children.push(child);
child.parent = submodule;
});
if (oldDefault) {
oldDefault.children = undefined;
context.project.removeReflection(oldDefault);
}
submodule?.children?.forEach((child) => {
if (baseOrAbstractClassChecker(child?.name)) {
child.name = capitalize(submodule.name);
}
});
});
});
app.options.addReader(new TypeDoc.TSConfigReader());
const fileNames = getModuleNamesWithPath(BASE_PATH);
app.bootstrap({
entryPoints: fileNames,
sort: ['source-order', 'visibility'],
excludePrivate: true,
entryPointStrategy: 'Expand',
excludeNotDocumented: false,
excludeInternal: true,
pretty: true,
categorizeByGroup: true,
cleanOutputDir: true,
emit: 'docs',
includeVersion: true
});
const project = app.convert();
// Project may not have converted correctly
if (project) {
// Rendered docs
return await app.generateDocs(project, OUTPUT_DIR);
}
}
main().catch((error) => {
console.error(error);
});