-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrenderer.builder.ts
88 lines (85 loc) · 3.43 KB
/
renderer.builder.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
import MediaWiki from './../MediaWiki.js'
import { Renderer } from './abstract.renderer.js'
import { VisualEditorRenderer } from './visual-editor.renderer.js'
import { WikimediaDesktopRenderer } from './wikimedia-desktop.renderer.js'
import { WikimediaMobileRenderer } from './wikimedia-mobile.renderer.js'
import { MediawikiRESTApiRenderer } from './mediawiki-rest-api.renderer.js'
import { RendererBuilderOptions } from './abstract.renderer.js'
import * as logger from './../Logger.js'
export class RendererBuilder {
public async createRenderer(options: RendererBuilderOptions): Promise<Renderer> {
const { renderType, renderName } = options
const [hasVisualEditorApi, hasWikimediaDesktopApi, hasWikimediaMobileApi, hasMediawikiRESTApi] = await Promise.all([
MediaWiki.hasVisualEditorApi(),
MediaWiki.hasWikimediaDesktopApi(),
MediaWiki.hasWikimediaMobileApi(),
MediaWiki.hasMediawikiRESTApi(),
])
switch (renderType) {
case 'desktop':
if (hasWikimediaDesktopApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
return new VisualEditorRenderer()
} else if (hasMediawikiRESTApi) {
return new MediawikiRESTApiRenderer()
} else {
logger.error('No available desktop renderer.')
process.exit(1)
}
case 'mobile':
if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
}
logger.error('No available mobile renderer.')
process.exit(1)
case 'auto':
if (hasWikimediaDesktopApi) {
// Choose WikimediaDesktopRenderer if it's present, regardless of hasVisualEditorApi value
return new WikimediaDesktopRenderer()
} else if (hasVisualEditorApi) {
return new VisualEditorRenderer()
} else if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
} else if (hasMediawikiRESTApi) {
return new MediawikiRESTApiRenderer()
} else {
logger.error('No render available at all.')
process.exit(1)
}
case 'specific':
// renderName argument is required for 'specific' mode
switch (renderName) {
case 'WikimediaDesktop':
if (hasWikimediaDesktopApi) {
return new WikimediaDesktopRenderer()
}
logger.error('Cannot create an instance of WikimediaDesktop renderer.')
process.exit(1)
case 'VisualEditor':
if (hasVisualEditorApi) {
return new VisualEditorRenderer()
}
logger.error('Cannot create an instance of VisualEditor renderer.')
process.exit(1)
case 'MediawikiRESTApi':
if (hasMediawikiRESTApi) {
return new MediawikiRESTApiRenderer()
}
logger.error('Cannot create an instance of MediawikiRESTApi renderer.')
process.exit(1)
case 'WikimediaMobile':
if (hasWikimediaMobileApi) {
return new WikimediaMobileRenderer()
}
logger.error('No available mobile renderer.')
process.exit(1)
default:
throw new Error(`Unknown renderName for specific mode: ${renderName}`)
}
default:
throw new Error(`Unknown render: ${renderType}`)
}
}
}