-
Notifications
You must be signed in to change notification settings - Fork 679
/
options.ts
204 lines (167 loc) · 6.13 KB
/
options.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
import * as _ from 'lodash';
import * as Util from 'util';
import * as ts from 'typescript';
import { Event } from '../events';
import { Component, AbstractComponent, ChildableComponent } from '../component';
import { Application } from '../../application';
import { OptionDeclaration, DeclarationOption, ParameterScope } from './declaration';
export class OptionsComponent extends AbstractComponent<Options> { }
export enum OptionsReadMode {
Prefetch,
Fetch
}
export interface OptionsReadResult {
hasErrors: boolean;
inputFiles: string[];
}
export class DiscoverEvent extends Event {
data: any;
mode: OptionsReadMode;
inputFiles: string[] = [];
errors: string[] = [];
static DISCOVER = 'optionsDiscover';
/**
* Add an input/source file.
*
* The input files will be used as source files for the compiler. All command line
* arguments without parameter will be interpreted as being input files.
*
* @param fileName The path and filename of the input file.
*/
addInputFile(fileName: string) {
this.inputFiles.push(fileName);
}
addError(message: string, ...args: string[]) {
this.errors.push(Util.format.apply(this, arguments));
}
}
@Component({name: 'options', internal: true, childClass: OptionsComponent})
export class Options extends ChildableComponent<Application, OptionsComponent> {
private declarations: {[name: string]: OptionDeclaration};
private values: {[name: string]: any};
private compilerOptions: ts.CompilerOptions;
initialize() {
this.declarations = {};
this.values = {};
this.compilerOptions = {
target: ts.ScriptTarget.ES3,
module: ts.ModuleKind.None
};
}
read(data: any = {}, mode: OptionsReadMode = OptionsReadMode.Fetch): OptionsReadResult {
const event = new DiscoverEvent(DiscoverEvent.DISCOVER);
event.data = data;
event.mode = mode;
this.trigger(event);
this.setValues(event.data, '', event.addError.bind(event));
if (mode === OptionsReadMode.Fetch) {
const logger = this.application.logger;
for (let error of event.errors) {
logger.error(error);
}
}
return {
hasErrors: event.errors.length > 0,
inputFiles: event.inputFiles
};
}
getValue(name: string): any {
const declaration = this.getDeclaration(name);
if (!declaration) {
throw new Error(Util.format('Unknown option `%s`.', name));
}
if (declaration.scope === ParameterScope.TypeScript) {
throw new Error('TypeScript options cannot be fetched using `getValue`, use `getCompilerOptions` instead.');
}
if (name in this.values) {
return this.values[name];
} else {
return declaration.defaultValue;
}
}
getRawValues(): any {
return _.clone(this.values);
}
getDeclaration(name: string): OptionDeclaration {
name = name.toLowerCase();
if (name in this.declarations) {
return this.declarations[name];
}
}
getDeclarationsByScope(scope: ParameterScope): OptionDeclaration[] {
const result: OptionDeclaration[] = [];
for (let name in this.declarations) {
const declaration = this.declarations[name];
if (declaration.scope === scope) {
result.push(declaration);
}
}
return _.uniq(result);
}
getCompilerOptions(): ts.CompilerOptions {
return this.compilerOptions;
}
setValue(name: string|OptionDeclaration, value: any, errorCallback?: Function) {
const declaration = name instanceof OptionDeclaration ? name : this.getDeclaration(<string> name);
if (!declaration) {
return;
}
const key = declaration.name;
if (declaration.scope === ParameterScope.TypeScript) {
this.compilerOptions[key] = declaration.convert(value, errorCallback);
} else {
this.values[key] = declaration.convert(value, errorCallback);
}
}
setValues(obj: Object, prefix: string = '', errorCallback?: Function) {
for (let key in obj) {
const value = obj[key];
const declaration = this.getDeclaration(key);
const shouldValueBeAnObject = declaration && declaration['map'] === 'object';
if (!Array.isArray(value) && typeof value === 'object' && !shouldValueBeAnObject) {
this.setValues(value, prefix + key + '.', errorCallback);
} else {
this.setValue(prefix + key, value, errorCallback);
}
}
}
addDeclaration(declaration: OptionDeclaration|DeclarationOption) {
let decl: OptionDeclaration;
if (!(declaration instanceof OptionDeclaration)) {
decl = new OptionDeclaration(<DeclarationOption> declaration);
} else {
decl = <OptionDeclaration> declaration;
}
for (let name of decl.getNames()) {
if (name in this.declarations) {
this.application.logger.error('The option "%s" has already been registered by the "%s" component.', name, this.declarations[name].component);
} else {
this.declarations[name] = decl;
}
}
}
addDeclarations(declarations: (OptionDeclaration|DeclarationOption)[]) {
for (let declaration of declarations) {
this.addDeclaration(declaration);
}
}
removeDeclaration(declaration: OptionDeclaration) {
const names = _.keys(this.declarations);
let name: string;
for (name in names) {
if (this.declarations[name] === declaration) {
delete this.declarations[name];
}
}
name = declaration.name;
if (name in this.values) {
delete this.values[name];
}
}
removeDeclarationByName(name: string) {
const declaration = this.getDeclaration(name);
if (declaration) {
this.removeDeclaration(declaration);
}
}
}