1 | var __extends = this.__extends || function (d, b) {
|
2 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
3 | function __() { this.constructor = d; }
|
4 | __.prototype = b.prototype;
|
5 | d.prototype = new __();
|
6 | };
|
7 | var ts = require('typescript-api');
|
8 |
|
9 | var glob = require('simple-glob');
|
10 | var events = require('events');
|
11 |
|
12 | function compile(files, options, callback) {
|
13 | return new BatchCompiler().compile(files, options, callback);
|
14 | }
|
15 | exports.compile = compile;
|
16 |
|
17 | var BatchCompiler = (function (_super) {
|
18 | __extends(BatchCompiler, _super);
|
19 | function BatchCompiler() {
|
20 | _super.call(this);
|
21 | this._skipWrite = false;
|
22 | this.redirectErrors();
|
23 | this._compiler = new ts.BatchCompiler(ts.IO);
|
24 | process.mainModule.filename = require.resolve('typescript');
|
25 | }
|
26 | BatchCompiler.prototype.redirectErrors = function () {
|
27 | var _this = this;
|
28 | ts.IO.stderr.Write = function (s) {
|
29 | _this.emit('error', s);
|
30 | };
|
31 | ts.IO.stderr.WriteLine = function (s) {
|
32 | ts.IO.stderr.Write(s + '\n');
|
33 | };
|
34 | ts.IO.stdout.Write = function (s) {
|
35 | _this.emit('info', s);
|
36 | };
|
37 | ts.IO.stdout.WriteLine = function (s) {
|
38 | ts.IO.stdout.Write(s + '\n');
|
39 | };
|
40 | ts.BatchCompiler.prototype.addDiagnostic = function (diagnostic) {
|
41 | var diagnosticInfo = diagnostic.info();
|
42 | if (diagnosticInfo.category === 1) {
|
43 | this.hasErrors = true;
|
44 | }
|
45 | var errorLocation = '';
|
46 | if (diagnostic.fileName()) {
|
47 | errorLocation = diagnostic.fileName() + "(" + (diagnostic.line() + 1) + "," + (diagnostic.character() + 1) + "): ";
|
48 | }
|
49 | this.ioHost.stderr.WriteLine(errorLocation + diagnostic.message());
|
50 | };
|
51 | };
|
52 |
|
53 | BatchCompiler.prototype.compile = function (globs, options, callback) {
|
54 | var _this = this;
|
55 | handleOverloads.call(this);
|
56 | handleSkipWrite.call(this);
|
57 | setupArguments(function (args) {
|
58 | ts.IO.arguments = args;
|
59 | _this._batchCompile(callback);
|
60 | });
|
61 |
|
62 | function handleOverloads() {
|
63 | if (typeof options === 'function') {
|
64 | callback = options;
|
65 | options = {};
|
66 | } else if (typeof callback !== 'function') {
|
67 | callback = function () {
|
68 | };
|
69 | }
|
70 | }
|
71 |
|
72 | function handleSkipWrite() {
|
73 | options = options || {};
|
74 | this._skipWrite = options.skipWrite;
|
75 | delete options.skipWrite;
|
76 | }
|
77 |
|
78 | function setupArguments(cb) {
|
79 | var args = argify(options);
|
80 | args.push.apply(args, glob(globs));
|
81 | cb(args);
|
82 | }
|
83 | };
|
84 |
|
85 | BatchCompiler.prototype._batchCompile = function (callback) {
|
86 | var compiler = this._compiler;
|
87 |
|
88 | ts.CompilerDiagnostics.diagnosticWriter = { Alert: function (s) {
|
89 | compiler.ioHost.printLine(s);
|
90 | } };
|
91 |
|
92 | if (compiler.parseOptions()) {
|
93 | compiler.logger = compiler.compilationSettings.gatherDiagnostics() ? new DiagnosticsLogger(this.ioHost) : new ts.NullLogger();
|
94 |
|
95 | if (compiler.compilationSettings.watch()) {
|
96 | compiler.watchFiles();
|
97 | callback(null);
|
98 | return;
|
99 | }
|
100 |
|
101 | compiler.resolve();
|
102 | this._compile(callback);
|
103 | } else {
|
104 | callback(new Error('Error parsing compiler options'));
|
105 | }
|
106 |
|
107 | if (compiler.hasErrors) {
|
108 | callback(new Error('Unspecified error'));
|
109 | }
|
110 | };
|
111 |
|
112 | BatchCompiler.prototype._compile = function (callback) {
|
113 | var compiler = this._compiler;
|
114 | var tsCompiler = new ts.TypeScriptCompiler(compiler.logger, compiler.compilationSettings);
|
115 |
|
116 | compiler.resolvedFiles.forEach(function (resolvedFile) {
|
117 | var sourceFile = compiler.getSourceFile(resolvedFile.path);
|
118 | tsCompiler.addFile(resolvedFile.path, sourceFile.scriptSnapshot, sourceFile.byteOrderMark, 0, false, resolvedFile.referencedFiles);
|
119 | });
|
120 |
|
121 | var results = [];
|
122 | for (var it = tsCompiler.compile(function (path) {
|
123 | return compiler.resolvePath(path);
|
124 | }); it.moveNext();) {
|
125 | var result = it.current();
|
126 |
|
127 | result.diagnostics.forEach(function (d) {
|
128 | return compiler.addDiagnostic(d);
|
129 | });
|
130 | if (!this._skipWrite && !compiler.tryWriteOutputFiles(result.outputFiles)) {
|
131 | callback(new Error('Error writing to output file'));
|
132 | }
|
133 | Array.prototype.push.apply(results, result.outputFiles);
|
134 | }
|
135 | callback(null, results);
|
136 | };
|
137 | return BatchCompiler;
|
138 | })(events.EventEmitter);
|
139 | exports.BatchCompiler = BatchCompiler;
|
140 |
|
141 | var DiagnosticsLogger = (function () {
|
142 | function DiagnosticsLogger(ioHost) {
|
143 | this.ioHost = ioHost;
|
144 | }
|
145 | DiagnosticsLogger.prototype.information = function () {
|
146 | return false;
|
147 | };
|
148 | DiagnosticsLogger.prototype.debug = function () {
|
149 | return false;
|
150 | };
|
151 | DiagnosticsLogger.prototype.warning = function () {
|
152 | return false;
|
153 | };
|
154 | DiagnosticsLogger.prototype.error = function () {
|
155 | return false;
|
156 | };
|
157 | DiagnosticsLogger.prototype.fatal = function () {
|
158 | return false;
|
159 | };
|
160 | DiagnosticsLogger.prototype.log = function (s) {
|
161 | this.ioHost.stdout.WriteLine(s);
|
162 | };
|
163 | return DiagnosticsLogger;
|
164 | })();
|
165 |
|
166 | function argify(options) {
|
167 | var args = [];
|
168 | Object.keys(options).forEach(function (key) {
|
169 | var value = options[key];
|
170 | if (!value) {
|
171 | return;
|
172 | }
|
173 | if (key === 'optionsFile') {
|
174 | args.push('@' + value);
|
175 | return;
|
176 | }
|
177 | var flag = '-';
|
178 | if (key.length !== 1) {
|
179 | flag += '-';
|
180 | }
|
181 | args.push(flag + key);
|
182 | if (typeof value !== 'boolean') {
|
183 | args.push(value);
|
184 | }
|
185 | });
|
186 | return args;
|
187 | }
|
188 |
|
189 | var OutputFile = (function (_super) {
|
190 | __extends(OutputFile, _super);
|
191 | function OutputFile() {
|
192 | _super.apply(this, arguments);
|
193 | }
|
194 | return OutputFile;
|
195 | })(ts.OutputFile);
|
196 | exports.OutputFile = OutputFile;
|