|
1 | 1 | exports.runTypeScriptCompiler = runTypeScriptCompiler; |
| 2 | +exports.getTscProcess = getTscProcess; |
2 | 3 |
|
3 | 4 | var spawn = require('child_process').spawn; |
4 | 5 | var fs = require('fs'); |
5 | 6 | var path = require('path'); |
| 7 | +var tsc = null; |
6 | 8 |
|
7 | 9 | function runTypeScriptCompiler(logger, projectDir, options) { |
8 | 10 | return new Promise(function (resolve, reject) { |
@@ -34,41 +36,46 @@ function runTypeScriptCompiler(logger, projectDir, options) { |
34 | 36 | } |
35 | 37 |
|
36 | 38 | logger.trace(process.execPath, nodeArgs.join(' ')); |
37 | | - var tsc = spawn(process.execPath, nodeArgs); |
| 39 | + tsc = spawn(process.execPath, nodeArgs); |
38 | 40 |
|
39 | 41 | var isResolved = false; |
40 | | - tsc.stdout.on('data', function(data) { |
| 42 | + tsc.stdout.on('data', function (data) { |
41 | 43 | var stringData = data.toString(); |
42 | 44 | logger.info(stringData); |
43 | | - if(options.watch && stringData.toLowerCase().indexOf("compilation complete. watching for file changes.") !== -1 && !isResolved) { |
| 45 | + if (options.watch && stringData.toLowerCase().indexOf("compilation complete. watching for file changes.") !== -1 && !isResolved) { |
44 | 46 | isResolved = true; |
45 | 47 | resolve(); |
46 | 48 | } |
47 | 49 | }); |
48 | 50 |
|
49 | | - tsc.stderr.on('data', function(data) { |
| 51 | + tsc.stderr.on('data', function (data) { |
50 | 52 | logger.info(data.toString()); |
51 | 53 | }); |
52 | 54 |
|
53 | | - tsc.on('error', function(err) { |
| 55 | + tsc.on('error', function (err) { |
54 | 56 | logger.info(err.message); |
55 | | - if(!isResolved) { |
| 57 | + if (!isResolved) { |
56 | 58 | isResolved = true; |
57 | 59 | reject(err); |
58 | 60 | } |
59 | 61 | }); |
60 | 62 |
|
61 | 63 | // TODO: Consider using close event instead of exit |
62 | 64 | tsc.on('exit', function (code, signal) { |
63 | | - if(!isResolved) { |
| 65 | + tsc = null; |
| 66 | + if (!isResolved) { |
64 | 67 | isResolved = true; |
65 | 68 | // EmitReturnStatus enum in https://github.com/Microsoft/TypeScript/blob/8947757d096338532f1844d55788df87fb5a39ed/src/compiler/types.ts#L605 |
66 | 69 | if (code === 0 || code === 2 || code === 3) { |
67 | 70 | resolve(); |
68 | 71 | } else { |
69 | | - reject(Error('TypeScript compiler failed with exit code ' + code)); |
| 72 | + reject(new Error('TypeScript compiler failed with exit code ' + code)); |
70 | 73 | } |
71 | 74 | } |
72 | 75 | }); |
73 | 76 | }); |
74 | 77 | } |
| 78 | + |
| 79 | +function getTscProcess() { |
| 80 | + return tsc; |
| 81 | +} |
0 commit comments