-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompiler.js
85 lines (72 loc) · 2.35 KB
/
compiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
exports.runTypeScriptCompiler = runTypeScriptCompiler;
exports.getTscProcess = getTscProcess;
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var tsc = null;
function runTypeScriptCompiler(logger, projectDir, options) {
return new Promise(function (resolve, reject) {
options = options || {};
var peerTypescriptPath = path.join(__dirname, '../../typescript');
var tscPath = path.join(peerTypescriptPath, 'lib/tsc.js');
if (fs.existsSync(tscPath)) {
try {
logger.info('Found peer TypeScript ' + require(path.join(peerTypescriptPath, 'package.json')).version);
} catch (err) { }
} else {
throw Error('TypeScript installation local to project was not found. Install by executing `npm install typescript`.');
}
var tsconfigPath = path.join(projectDir, 'tsconfig.json');
if (!fs.existsSync(tsconfigPath)) {
throw Error('No tsconfig.json file found in project.');
}
var nodeArgs = ['--max_old_space_size=4096', tscPath, '--project', projectDir];
if (options.watch) {
nodeArgs.push('--watch');
}
if (!options.release) {
// For debugging in Chrome DevTools
nodeArgs.push('--inlineSourceMap', '--inlineSources');
}
if (!options.watchAllFiles) {
nodeArgs.push('--moduleResolution', 'classic');
}
logger.trace(process.execPath, nodeArgs.join(' '));
tsc = spawn(process.execPath, nodeArgs);
var isResolved = false;
tsc.stdout.on('data', function (data) {
var stringData = data.toString();
logger.info(stringData);
if (options.watch && stringData.toLowerCase().indexOf("compilation complete. watching for file changes.") !== -1 && !isResolved) {
isResolved = true;
resolve();
}
});
tsc.stderr.on('data', function (data) {
logger.info(data.toString());
});
tsc.on('error', function (err) {
logger.info(err.message);
if (!isResolved) {
isResolved = true;
reject(err);
}
});
// TODO: Consider using close event instead of exit
tsc.on('exit', function (code, signal) {
tsc = null;
if (!isResolved) {
isResolved = true;
// ExitStatus enum in https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts#L2620
if (code === 0 || code === 2) {
resolve();
} else {
reject(new Error('TypeScript compiler failed with exit code ' + code));
}
}
});
});
}
function getTscProcess() {
return tsc;
}