UNPKG

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