1 | "use strict";
|
2 | var __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 | };
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const _ = require("lodash");
|
10 | const Util = require("util");
|
11 | const ts = require("typescript");
|
12 | const events_1 = require("../events");
|
13 | const component_1 = require("../component");
|
14 | const declaration_1 = require("./declaration");
|
15 | class OptionsComponent extends component_1.AbstractComponent {
|
16 | }
|
17 | exports.OptionsComponent = OptionsComponent;
|
18 | var OptionsReadMode;
|
19 | (function (OptionsReadMode) {
|
20 | OptionsReadMode[OptionsReadMode["Prefetch"] = 0] = "Prefetch";
|
21 | OptionsReadMode[OptionsReadMode["Fetch"] = 1] = "Fetch";
|
22 | })(OptionsReadMode = exports.OptionsReadMode || (exports.OptionsReadMode = {}));
|
23 | class DiscoverEvent extends events_1.Event {
|
24 | constructor(name, mode) {
|
25 | super(name);
|
26 | this.inputFiles = [];
|
27 | this.errors = [];
|
28 | this.mode = mode;
|
29 | }
|
30 | addInputFile(fileName) {
|
31 | this.inputFiles.push(fileName);
|
32 | }
|
33 | addError(message, ...args) {
|
34 | this.errors.push(Util.format.apply(this, arguments));
|
35 | }
|
36 | }
|
37 | DiscoverEvent.DISCOVER = 'optionsDiscover';
|
38 | exports.DiscoverEvent = DiscoverEvent;
|
39 | let Options = class Options extends component_1.ChildableComponent {
|
40 | initialize() {
|
41 | this.declarations = {};
|
42 | this.values = {};
|
43 | this.compilerOptions = {
|
44 | target: ts.ScriptTarget.ES3,
|
45 | module: ts.ModuleKind.None
|
46 | };
|
47 | }
|
48 | read(data = {}, mode = OptionsReadMode.Fetch) {
|
49 | const event = new DiscoverEvent(DiscoverEvent.DISCOVER, mode);
|
50 | event.data = data;
|
51 | this.trigger(event);
|
52 | this.setValues(event.data, '', event.addError.bind(event));
|
53 | if (mode === OptionsReadMode.Fetch) {
|
54 | const logger = this.application.logger;
|
55 | for (let error of event.errors) {
|
56 | logger.error(error);
|
57 | }
|
58 | }
|
59 | return {
|
60 | hasErrors: event.errors.length > 0,
|
61 | inputFiles: event.inputFiles
|
62 | };
|
63 | }
|
64 | getValue(name) {
|
65 | const declaration = this.getDeclaration(name);
|
66 | if (!declaration) {
|
67 | throw new Error(Util.format('Unknown option `%s`.', name));
|
68 | }
|
69 | if (declaration.scope === declaration_1.ParameterScope.TypeScript) {
|
70 | throw new Error('TypeScript options cannot be fetched using `getValue`, use `getCompilerOptions` instead.');
|
71 | }
|
72 | if (name in this.values) {
|
73 | return this.values[name];
|
74 | }
|
75 | else {
|
76 | return declaration.defaultValue;
|
77 | }
|
78 | }
|
79 | getRawValues() {
|
80 | return _.clone(this.values);
|
81 | }
|
82 | getDeclaration(name) {
|
83 | return this.declarations[name.toLowerCase()];
|
84 | }
|
85 | getDeclarationsByScope(scope) {
|
86 | const result = _.values(this.declarations)
|
87 | .filter(declaration => declaration.scope === scope);
|
88 | return _.uniq(result);
|
89 | }
|
90 | getCompilerOptions() {
|
91 | return this.compilerOptions;
|
92 | }
|
93 | setValue(name, value, errorCallback) {
|
94 | const declaration = name instanceof declaration_1.OptionDeclaration ? name : this.getDeclaration(name);
|
95 | if (!declaration) {
|
96 | return;
|
97 | }
|
98 | const key = declaration.name;
|
99 | if (declaration.scope === declaration_1.ParameterScope.TypeScript) {
|
100 | this.compilerOptions[key] = declaration.convert(value, errorCallback);
|
101 | }
|
102 | else {
|
103 | this.values[key] = declaration.convert(value, errorCallback);
|
104 | }
|
105 | }
|
106 | setValues(obj, prefix = '', errorCallback) {
|
107 | for (let key in obj) {
|
108 | const value = obj[key];
|
109 | const declaration = this.getDeclaration(key);
|
110 | const shouldValueBeAnObject = declaration && declaration['map'] === 'object';
|
111 | if (!Array.isArray(value) && typeof value === 'object' && !shouldValueBeAnObject) {
|
112 | this.setValues(value, prefix + key + '.', errorCallback);
|
113 | }
|
114 | else {
|
115 | this.setValue(prefix + key, value, errorCallback);
|
116 | }
|
117 | }
|
118 | }
|
119 | addDeclaration(declaration) {
|
120 | const decl = declaration instanceof declaration_1.OptionDeclaration
|
121 | ? declaration
|
122 | : new declaration_1.OptionDeclaration(declaration);
|
123 | for (let name of decl.getNames()) {
|
124 | if (name in this.declarations) {
|
125 | this.application.logger.error('The option "%s" has already been registered by the "%s" component.', name, this.declarations[name].component || '__unknown');
|
126 | }
|
127 | else {
|
128 | this.declarations[name] = decl;
|
129 | }
|
130 | }
|
131 | }
|
132 | addDeclarations(declarations) {
|
133 | for (let declaration of declarations) {
|
134 | this.addDeclaration(declaration);
|
135 | }
|
136 | }
|
137 | removeDeclaration(declaration) {
|
138 | const names = _.keys(this.declarations);
|
139 | for (const name of names) {
|
140 | if (this.declarations[name] === declaration) {
|
141 | delete this.declarations[name];
|
142 | }
|
143 | }
|
144 | if (declaration.name in this.values) {
|
145 | delete this.values[declaration.name];
|
146 | }
|
147 | }
|
148 | removeDeclarationByName(name) {
|
149 | const declaration = this.getDeclaration(name);
|
150 | if (declaration) {
|
151 | this.removeDeclaration(declaration);
|
152 | }
|
153 | }
|
154 | };
|
155 | Options = __decorate([
|
156 | component_1.Component({ name: 'options', internal: true, childClass: OptionsComponent })
|
157 | ], Options);
|
158 | exports.Options = Options;
|
159 |
|
\ | No newline at end of file |