UNPKG

7.57 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var Renderer_1;
9Object.defineProperty(exports, "__esModule", { value: true });
10const Path = require("path");
11const FS = require("fs-extra");
12const ProgressBar = require('progress');
13const events_1 = require("./events");
14const fs_1 = require("../utils/fs");
15const DefaultTheme_1 = require("./themes/DefaultTheme");
16const components_1 = require("./components");
17const component_1 = require("../utils/component");
18const declaration_1 = require("../utils/options/declaration");
19let Renderer = Renderer_1 = class Renderer extends component_1.ChildableComponent {
20 initialize() {
21 }
22 render(project, outputDirectory) {
23 if (!this.prepareTheme() || !this.prepareOutputDirectory(outputDirectory)) {
24 return;
25 }
26 const output = new events_1.RendererEvent(events_1.RendererEvent.BEGIN, outputDirectory, project);
27 output.settings = this.application.options.getRawValues();
28 output.urls = this.theme.getUrls(project);
29 const bar = new ProgressBar('Rendering [:bar] :percent', {
30 total: output.urls.length,
31 width: 40
32 });
33 this.trigger(output);
34 if (!output.isDefaultPrevented) {
35 output.urls.forEach((mapping) => {
36 this.renderDocument(output.createPageEvent(mapping));
37 bar.tick();
38 });
39 this.trigger(events_1.RendererEvent.END, output);
40 }
41 }
42 renderDocument(page) {
43 this.trigger(events_1.PageEvent.BEGIN, page);
44 if (page.isDefaultPrevented) {
45 return false;
46 }
47 page.template = page.template || this.theme.resources.templates.getResource(page.templateName).getTemplate();
48 page.contents = page.template(page);
49 this.trigger(events_1.PageEvent.END, page);
50 if (page.isDefaultPrevented) {
51 return false;
52 }
53 try {
54 fs_1.writeFile(page.filename, page.contents, false);
55 }
56 catch (error) {
57 this.application.logger.error('Could not write %s', page.filename);
58 return false;
59 }
60 return true;
61 }
62 prepareTheme() {
63 if (!this.theme) {
64 const themeName = this.themeName;
65 let path = Path.resolve(themeName);
66 if (!FS.existsSync(path)) {
67 path = Path.join(Renderer_1.getThemeDirectory(), themeName);
68 if (!FS.existsSync(path)) {
69 this.application.logger.error('The theme %s could not be found.', themeName);
70 return false;
71 }
72 }
73 const filename = Path.join(path, 'theme.js');
74 if (!FS.existsSync(filename)) {
75 this.theme = this.addComponent('theme', new DefaultTheme_1.DefaultTheme(this, path));
76 }
77 else {
78 try {
79 const themeClass = typeof require(filename) === 'function' ? require(filename) : require(filename).default;
80 this.theme = this.addComponent('theme', new (themeClass)(this, path));
81 }
82 catch (err) {
83 throw new Error(`Exception while loading "${filename}". You must export a \`new Theme(renderer, basePath)\` compatible class.\n` +
84 err);
85 }
86 }
87 }
88 this.theme.resources.activate();
89 return true;
90 }
91 prepareOutputDirectory(directory) {
92 if (FS.existsSync(directory)) {
93 if (!FS.statSync(directory).isDirectory()) {
94 this.application.logger.error('The output target "%s" exists but it is not a directory.', directory);
95 return false;
96 }
97 if (this.disableOutputCheck) {
98 return true;
99 }
100 if (FS.readdirSync(directory).length === 0) {
101 return true;
102 }
103 if (!this.theme.isOutputDirectory(directory)) {
104 this.application.logger.error('The output directory "%s" exists but does not seem to be a documentation generated by TypeDoc.\n' +
105 'Make sure this is the right target directory, delete the folder and rerun TypeDoc.', directory);
106 return false;
107 }
108 try {
109 FS.removeSync(directory);
110 }
111 catch (error) {
112 this.application.logger.warn('Could not empty the output directory.');
113 }
114 }
115 if (!FS.existsSync(directory)) {
116 try {
117 FS.mkdirpSync(directory);
118 }
119 catch (error) {
120 this.application.logger.error('Could not create output directory %s', directory);
121 return false;
122 }
123 }
124 return true;
125 }
126 static getThemeDirectory() {
127 return Path.dirname(require.resolve('typedoc-default-themes'));
128 }
129 static getDefaultTheme() {
130 return Path.join(Renderer_1.getThemeDirectory(), 'default');
131 }
132};
133__decorate([
134 component_1.Option({
135 name: 'theme',
136 help: 'Specify the path to the theme that should be used or \'default\' or \'minimal\' to use built-in themes.',
137 type: declaration_1.ParameterType.String,
138 defaultValue: 'default'
139 })
140], Renderer.prototype, "themeName", void 0);
141__decorate([
142 component_1.Option({
143 name: 'disableOutputCheck',
144 help: 'Should TypeDoc disable the testing and cleaning of the output directory?',
145 type: declaration_1.ParameterType.Boolean
146 })
147], Renderer.prototype, "disableOutputCheck", void 0);
148__decorate([
149 component_1.Option({
150 name: 'gaID',
151 help: 'Set the Google Analytics tracking ID and activate tracking code.'
152 })
153], Renderer.prototype, "gaID", void 0);
154__decorate([
155 component_1.Option({
156 name: 'gaSite',
157 help: 'Set the site name for Google Analytics. Defaults to `auto`.',
158 defaultValue: 'auto'
159 })
160], Renderer.prototype, "gaSite", void 0);
161__decorate([
162 component_1.Option({
163 name: 'hideGenerator',
164 help: 'Do not print the TypeDoc link at the end of the page.',
165 type: declaration_1.ParameterType.Boolean
166 })
167], Renderer.prototype, "hideGenerator", void 0);
168__decorate([
169 component_1.Option({
170 name: 'entryPoint',
171 help: 'Specifies the fully qualified name of the root symbol. Defaults to global namespace.',
172 type: declaration_1.ParameterType.String
173 })
174], Renderer.prototype, "entryPoint", void 0);
175__decorate([
176 component_1.Option({
177 name: 'toc',
178 help: 'Specifies the top level table of contents.',
179 type: declaration_1.ParameterType.Array
180 })
181], Renderer.prototype, "toc", void 0);
182Renderer = Renderer_1 = __decorate([
183 component_1.Component({ name: 'renderer', internal: true, childClass: components_1.RendererComponent })
184], Renderer);
185exports.Renderer = Renderer;
186require("./plugins");
187//# sourceMappingURL=renderer.js.map
\No newline at end of file