-
Notifications
You must be signed in to change notification settings - Fork 712
/
application.ts
295 lines (242 loc) · 9.12 KB
/
application.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* The TypeDoc main module and namespace.
*
* The [[Application]] class holds the core logic of the cli application. All code related
* to resolving reflections is stored in [[TypeDoc.Factories]], the actual data models can be found
* in [[TypeDoc.Models]] and the final rendering is defined in [[TypeDoc.Output]].
*/
import * as Path from "path";
import * as FS from "fs";
import * as Util from "util";
import * as typescript from "typescript";
import {Minimatch, IMinimatch} from "minimatch";
import {Converter} from "./converter/index";
import {Renderer} from "./output/renderer";
import {ProjectReflection} from "./models/index";
import {Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile} from "./utils/index";
import {AbstractComponent, ChildableComponent, Component, Option} from "./utils/component";
import {Options, OptionsReadMode, IOptionsReadResult} from "./utils/options/index"
import {ParameterType} from "./utils/options/declaration";
/**
* The default TypeDoc main application class.
*
* This class holds the two main components of TypeDoc, the [[Dispatcher]] and
* the [[Renderer]]. When running TypeDoc, first the [[Dispatcher]] is invoked which
* generates a [[ProjectReflection]] from the passed in source files. The
* [[ProjectReflection]] is a hierarchical model representation of the TypeScript
* project. Afterwards the model is passed to the [[Renderer]] which uses an instance
* of [[BaseTheme]] to generate the final documentation.
*
* Both the [[Dispatcher]] and the [[Renderer]] are subclasses of the [[EventDispatcher]]
* and emit a series of events while processing the project. Subscribe to these Events
* to control the application flow or alter the output.
*/
@Component({name:"application", internal:true})
export class Application extends ChildableComponent<Application, AbstractComponent<Application>>
{
options:Options;
/**
* The converter used to create the declaration reflections.
*/
converter:Converter;
/**
* The renderer used to generate the documentation output.
*/
renderer:Renderer;
/**
* The logger that should be used to output messages.
*/
logger:Logger;
plugins:PluginHost;
@Option({
name: 'logger',
help: 'Specify the logger that should be used, \'none\' or \'console\'',
defaultValue: 'console',
type: ParameterType.Mixed,
})
loggerType:string|Function;
@Option({
name: 'ignoreCompilerErrors',
help: 'Should TypeDoc generate documentation pages even after the compiler has returned errors?',
type: ParameterType.Boolean
})
ignoreCompilerErrors:boolean;
@Option({
name: 'exclude',
help: 'Define a pattern for excluded files when specifying paths.',
type: ParameterType.String
})
exclude:string;
/**
* The version number of TypeDoc.
*/
static VERSION:string = '{{ VERSION }}';
/**
* Create a new TypeDoc application instance.
*
* @param options An object containing the options that should be used.
*/
constructor(options?:Object) {
super(null);
this.logger = new ConsoleLogger();
this.converter = this.addComponent('converter', Converter);
this.renderer = this.addComponent('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);
this.bootstrap(options);
}
/**
* Initialize TypeDoc with the given options object.
*
* @param options The desired options to set.
*/
protected bootstrap(options?:Object):IOptionsReadResult {
this.options.read(options, OptionsReadMode.Prefetch);
var logger = this.loggerType;
if (typeof logger == 'function') {
this.logger = new CallbackLogger(<any>logger);
} else if (logger == 'none') {
this.logger = new Logger();
}
this.plugins.load();
return this.options.read(options, OptionsReadMode.Fetch);
}
/**
* Return the application / root component instance.
*/
get application():Application {
return this
}
get isCLI():boolean {
return false;
}
/**
* Return the path to the TypeScript compiler.
*/
public getTypeScriptPath():string {
return Path.dirname(require.resolve('typescript'));
}
public getTypeScriptVersion():string {
var tsPath = this.getTypeScriptPath();
var json = JSON.parse(FS.readFileSync(Path.join(tsPath, '..', 'package.json'), 'utf8'));
return json.version;
}
/**
* Run the converter for the given set of files and return the generated reflections.
*
* @param src A list of source that should be compiled and converted.
* @returns An instance of ProjectReflection on success, NULL otherwise.
*/
public convert(src:string[]):ProjectReflection {
this.logger.writeln('Using TypeScript %s from %s', this.getTypeScriptVersion(), this.getTypeScriptPath());
var result = this.converter.convert(src);
if (result.errors && result.errors.length) {
this.logger.diagnostics(result.errors);
if (this.ignoreCompilerErrors) {
this.logger.resetErrors();
return result.project;
} else {
return null;
}
} else {
return result.project;
}
}
/**
* @param src A list of source files whose documentation should be generated.
*/
public generateDocs(src:string[], out:string):boolean;
/**
* @param project The project the documentation should be generated for.
*/
public generateDocs(project:ProjectReflection, out:string):boolean;
/**
* Run the documentation generator for the given set of files.
*
* @param out The path the documentation should be written to.
* @returns TRUE if the documentation could be generated successfully, otherwise FALSE.
*/
public generateDocs(input:any, out:string):boolean {
var project = input instanceof ProjectReflection ? input : this.convert(input);
if (!project) return false;
out = Path.resolve(out);
this.renderer.render(project, out);
if (this.logger.hasErrors()) {
this.logger.error('Documentation could not be generated due to the errors above.');
} else {
this.logger.success('Documentation generated at %s', out);
}
return true;
}
/**
* @param src A list of source that should be compiled and converted.
*/
public generateJson(src:string[], out:string):boolean;
/**
* @param project The project that should be converted.
*/
public generateJson(project:ProjectReflection, out:string):boolean;
/**
* Run the converter for the given set of files and write the reflections to a json file.
*
* @param out The path and file name of the target file.
* @returns TRUE if the json file could be written successfully, otherwise FALSE.
*/
public generateJson(input:any, out:string):boolean {
var project = input instanceof ProjectReflection ? input : this.convert(input);
if (!project) return false;
out = Path.resolve(out);
writeFile(out, JSON.stringify(project.toObject(), null, '\t'), false);
this.logger.success('JSON written to %s', out);
return true;
}
/**
* Expand a list of input files.
*
* Searches for directories in the input files list and replaces them with a
* listing of all TypeScript files within them. One may use the ```--exclude``` option
* to filter out files with a pattern.
*
* @param inputFiles The list of files that should be expanded.
* @returns The list of input files with expanded directories.
*/
public expandInputFiles(inputFiles?:string[]):string[] {
var exclude:IMinimatch, files:string[] = [];
if (this.exclude) {
exclude = new Minimatch(this.exclude);
}
function add(dirname:string) {
FS.readdirSync(dirname).forEach((file) => {
var realpath = Path.join(dirname, file);
if (FS.statSync(realpath).isDirectory()) {
add(realpath);
} else if (/\.tsx?$/.test(realpath)) {
if (exclude && exclude.match(realpath.replace(/\\/g, '/'))) {
return;
}
files.push(realpath);
}
});
}
inputFiles.forEach((file) => {
file = Path.resolve(file);
if (FS.statSync(file).isDirectory()) {
add(file);
} else {
files.push(file);
}
});
return files;
}
/**
* Print the version number.
*/
public toString() {
return [
'',
'TypeDoc ' + Application.VERSION,
'Using TypeScript ' + this.getTypeScriptVersion() + ' from ' + this.getTypeScriptPath(),
''
].join(typescript.sys.newLine);
}
}