/
cli.ts
77 lines (67 loc) · 2.47 KB
/
cli.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
import * as typescript from 'typescript';
import { Application } from './application';
import { BindOption } from './utils/options';
import { getOptionsHelp } from './utils/options/help';
import { ArgumentsReader, TypeDocReader } from './utils/options/readers';
import { TSConfigReader } from './utils/options/readers/tsconfig';
import { TypeDocAndTSOptions } from './utils/options/declaration';
export const enum ExitCode {
OptionError = 1,
NoInputFiles = 2,
NoOutput = 3,
CompileError = 4,
OutputError = 5
}
export class CliApplication extends Application {
@BindOption('out')
out!: string;
@BindOption('json')
json!: string;
@BindOption('version')
version!: boolean;
@BindOption('help')
help!: boolean;
/**
* Run TypeDoc from the command line.
*/
bootstrap(options?: Partial<TypeDocAndTSOptions>) {
this.options.addReader(new ArgumentsReader(0));
this.options.addReader(new TypeDocReader());
this.options.addReader(new TSConfigReader());
this.options.addReader(new ArgumentsReader(300));
const result = super.bootstrap(options);
if (result.hasErrors) {
return process.exit(ExitCode.OptionError);
}
if (this.version) {
typescript.sys.write(this.toString());
} else if (this.help) {
typescript.sys.write(getOptionsHelp(this.options));
} else if (result.inputFiles.length === 0) {
typescript.sys.write(getOptionsHelp(this.options));
process.exit(ExitCode.NoInputFiles);
} else {
const src = this.expandInputFiles(result.inputFiles);
const project = this.convert(src);
if (project) {
if (this.out) {
this.generateDocs(project, this.out);
}
if (this.json) {
this.generateJson(project, this.json);
}
if (!this.out && !this.json) {
this.logger.log("No 'out' or 'json' option has been set");
this.logger.log("The './docs' directory has been set as the output location by default");
this.generateDocs(project, './docs');
}
if (this.logger.hasErrors()) {
process.exit(ExitCode.OutputError);
}
} else {
process.exit(ExitCode.CompileError);
}
}
return result;
}
}