forked from typedoc2md/typedoc-plugin-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.helpers.ts
147 lines (129 loc) · 3.42 KB
/
jest.helpers.ts
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
require('ts-node/register');
import * as fs from 'fs';
import * as Handlebars from 'handlebars';
import * as path from 'path';
import {
Application,
PageEvent,
ProjectReflection,
RendererEvent,
TSConfigReader,
TypeDocReader,
UrlMapping,
} from 'typedoc';
import { load } from 'typedoc-plugin-markdown';
import { formatContents } from 'typedoc-plugin-markdown/src/utils';
const STUBS_SRC_PATH = path.join(__dirname, 'stub-project', 'src');
const STUBS_TSCONFIG_PATH = path.join(
__dirname,
'stub-project',
'tsconfig.json',
);
global.bootstrap = async (entryPoints: string[] = [], options: any = {}) => {
const app = new Application();
load(app);
app.options.addReader(new TypeDocReader());
app.options.addReader(new TSConfigReader());
app.bootstrap({
logLevel: 'None',
entryPoints:
entryPoints.length > 0
? entryPoints.map((inputFile: string) =>
path.join(STUBS_SRC_PATH, inputFile),
)
: [STUBS_SRC_PATH],
tsconfig: STUBS_TSCONFIG_PATH,
...options,
});
const project = app.convert() as ProjectReflection;
app.renderer.render = render;
await app.generateDocs(project, 'docs');
return project;
};
global.stubPartials = (partials: string[]) => {
partials.forEach((partial) => {
Handlebars.registerPartial(partial, `[partial: ${partial}]`);
});
};
global.stubHelpers = (helpers: string[]) => {
helpers.forEach((helper) => {
Handlebars.registerHelper(helper, () => `[helper: ${helper}]`);
});
};
global.getTemplate = (name: string) => {
const templateDir = path.resolve(
__dirname,
'packages',
'typedoc-plugin-markdown',
'dist',
'resources',
'templates',
);
const hbs = fs.readFileSync(templateDir + '/' + name + '.hbs');
return Handlebars.compile(hbs.toString());
};
global.getPartial = (name: string) => {
const partialDir = path.resolve(
__dirname,
'packages',
'typedoc-plugin-markdown',
'dist',
'resources',
'partials',
);
const hbs = fs.readFileSync(partialDir + '/' + name + '.hbs');
return Handlebars.compile(hbs.toString());
};
global.compileTemplate = (
template: Handlebars.TemplateDelegate,
context: any,
) => {
return formatContents(
template(context, {
allowProtoMethodsByDefault: true,
allowProtoPropertiesByDefault: true,
}),
);
};
global.compileHelper = (
helper: Handlebars.HelperDelegate,
context: any,
args?: any,
) => {
return formatContents(helper.call(context, args));
};
global.findModule = (project: ProjectReflection, name: string) => {
return project.children?.find(
(child) => child.name.replace(/\"/g, '') === name,
);
};
global.handlebarsOptionsStub = {
fn: () => true,
inverse: () => false,
hash: {},
};
export async function render(
project: ProjectReflection,
outputDirectory: string,
): Promise<void> {
if (!this.prepareTheme()) {
return;
}
const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
project,
);
output.urls = this.theme!.getUrls(project);
this.trigger(output);
await Promise.all(this.preRenderAsyncJobs.map((job) => job(output)));
this.preRenderAsyncJobs = [];
if (!output.isDefaultPrevented) {
output.urls?.forEach((mapping: UrlMapping) => {
output.createPageEvent(mapping);
});
await Promise.all(this.postRenderAsyncJobs.map((job) => job(output)));
this.postRenderAsyncJobs = [];
this.trigger(RendererEvent.END, output);
}
}