UNPKG

7.2 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google Inc. All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __extends = (this && this.__extends) || (function () {
10 var extendStatics = Object.setPrototypeOf ||
11 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
12 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
13 return function (d, b) {
14 extendStatics(d, b);
15 function __() { this.constructor = d; }
16 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17 };
18})();
19Object.defineProperty(exports, "__esModule", { value: true });
20var fs_1 = require("fs");
21var path = require("path");
22var ts = require("typescript");
23var vinyl_file_1 = require("./vinyl_file");
24var UserError = (function (_super) {
25 __extends(UserError, _super);
26 function UserError(message) {
27 var _this = _super.call(this, message) || this;
28 // Required for TS 2.1, see
29 // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
30 Object.setPrototypeOf(_this, UserError.prototype);
31 var nativeError = new Error(message);
32 _this._nativeError = nativeError;
33 return _this;
34 }
35 Object.defineProperty(UserError.prototype, "message", {
36 get: function () { return this._nativeError.message; },
37 set: function (message) {
38 if (this._nativeError)
39 this._nativeError.message = message;
40 },
41 enumerable: true,
42 configurable: true
43 });
44 Object.defineProperty(UserError.prototype, "name", {
45 get: function () { return this._nativeError.name; },
46 set: function (name) {
47 if (this._nativeError)
48 this._nativeError.name = name;
49 },
50 enumerable: true,
51 configurable: true
52 });
53 Object.defineProperty(UserError.prototype, "stack", {
54 get: function () { return this._nativeError.stack; },
55 set: function (value) {
56 if (this._nativeError)
57 this._nativeError.stack = value;
58 },
59 enumerable: true,
60 configurable: true
61 });
62 UserError.prototype.toString = function () { return this._nativeError.toString(); };
63 return UserError;
64}(Error));
65exports.UserError = UserError;
66var DEBUG = false;
67function debug(msg) {
68 var o = [];
69 for (var _i = 1; _i < arguments.length; _i++) {
70 o[_i - 1] = arguments[_i];
71 }
72 // tslint:disable-next-line:no-console
73 if (DEBUG)
74 console.log.apply(console, [msg].concat(o));
75}
76function formatDiagnostics(diags) {
77 return diags
78 .map(function (d) {
79 var res = ts.DiagnosticCategory[d.category];
80 if (d.file) {
81 res += ' at ' + d.file.fileName + ':';
82 var _a = d.file.getLineAndCharacterOfPosition(d.start), line = _a.line, character = _a.character;
83 res += (line + 1) + ':' + (character + 1) + ':';
84 }
85 res += ' ' + ts.flattenDiagnosticMessageText(d.messageText, '\n');
86 return res;
87 })
88 .join('\n');
89}
90exports.formatDiagnostics = formatDiagnostics;
91function check(diags) {
92 if (diags && diags.length && diags[0]) {
93 throw new UserError(formatDiagnostics(diags));
94 }
95}
96exports.check = check;
97function validateAngularCompilerOptions(options) {
98 if (options.annotationsAs) {
99 switch (options.annotationsAs) {
100 case 'decorators':
101 case 'static fields':
102 break;
103 default:
104 return [{
105 file: null,
106 start: null,
107 length: null,
108 messageText: 'Angular compiler options "annotationsAs" only supports "static fields" and "decorators"',
109 category: ts.DiagnosticCategory.Error,
110 code: 0
111 }];
112 }
113 }
114}
115exports.validateAngularCompilerOptions = validateAngularCompilerOptions;
116var Tsc = (function () {
117 function Tsc(readFile, readDirectory) {
118 if (readFile === void 0) { readFile = ts.sys.readFile; }
119 if (readDirectory === void 0) { readDirectory = ts.sys.readDirectory; }
120 this.readFile = readFile;
121 this.readDirectory = readDirectory;
122 this.parseConfigHost = {
123 useCaseSensitiveFileNames: true,
124 fileExists: fs_1.existsSync,
125 readDirectory: this.readDirectory,
126 readFile: ts.sys.readFile
127 };
128 }
129 Tsc.prototype.readConfiguration = function (project, basePath, existingOptions) {
130 var _this = this;
131 // Allow a directory containing tsconfig.json as the project value
132 // Note, TS@next returns an empty array, while earlier versions throw
133 try {
134 if (!vinyl_file_1.isVinylFile(project) && this.readDirectory(project).length > 0) {
135 project = path.join(project, 'tsconfig.json');
136 }
137 }
138 catch (e) {
139 // Was not a directory, continue on assuming it's a file
140 }
141 var _a = (function () {
142 // project is vinyl like file object
143 if (vinyl_file_1.isVinylFile(project)) {
144 return { config: JSON.parse(project.contents.toString()), error: null };
145 }
146 else {
147 return ts.readConfigFile(project, _this.readFile);
148 }
149 })(), config = _a.config, error = _a.error;
150 check([error]);
151 var parsed = ts.parseJsonConfigFileContent(config, this.parseConfigHost, basePath, existingOptions);
152 check(parsed.errors);
153 // Default codegen goes to the current directory
154 // Parsed options are already converted to absolute paths
155 var ngOptions = config.angularCompilerOptions || {};
156 ngOptions.genDir = path.join(basePath, ngOptions.genDir || '.');
157 for (var _i = 0, _b = Object.keys(parsed.options); _i < _b.length; _i++) {
158 var key = _b[_i];
159 ngOptions[key] = parsed.options[key];
160 }
161 check(validateAngularCompilerOptions(ngOptions));
162 return { parsed: parsed, ngOptions: ngOptions };
163 };
164 Tsc.prototype.typeCheck = function (compilerHost, program) {
165 debug('Checking global diagnostics...');
166 check(program.getGlobalDiagnostics());
167 var diagnostics = [];
168 debug('Type checking...');
169 for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) {
170 var sf = _a[_i];
171 diagnostics.push.apply(diagnostics, ts.getPreEmitDiagnostics(program, sf));
172 }
173 check(diagnostics);
174 };
175 Tsc.prototype.emit = function (program) {
176 debug('Emitting outputs...');
177 var emitResult = program.emit();
178 var diagnostics = [];
179 diagnostics.push.apply(diagnostics, emitResult.diagnostics);
180 return emitResult.emitSkipped ? 1 : 0;
181 };
182 return Tsc;
183}());
184exports.Tsc = Tsc;
185exports.tsc = new Tsc();
186//# sourceMappingURL=tsc.js.map
\No newline at end of file