Skip to content

Commit 5b15669

Browse files
committed
Preserve watch output
TypeScript clears console output from version 2.7.1. `tns debug` command prints debug URL on console after that typescript clears the output. So the debug command cannot be used.
1 parent dc8b6a5 commit 5b15669

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/compiler.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,35 @@ exports.getTscProcess = getTscProcess;
44
var spawn = require('child_process').spawn;
55
var fs = require('fs');
66
var path = require('path');
7+
var semver = require('semver');
78
var tsc = null;
89

10+
function getTypeScriptVersion(typeScriptPath) {
11+
try {
12+
return require(path.join(typeScriptPath, 'package.json')).version;
13+
} catch (err) { }
14+
15+
return null;
16+
}
17+
18+
function shouldPreserveWatchOutput(typeScriptVersion) {
19+
try {
20+
return semver.gte(typeScriptVersion, "2.8.1");
21+
} catch (err) { }
22+
23+
return false;
24+
}
25+
926
function runTypeScriptCompiler(logger, projectDir, options) {
1027
return new Promise(function (resolve, reject) {
1128
options = options || {};
1229

1330
var peerTypescriptPath = path.join(__dirname, '../../typescript');
1431
var tscPath = path.join(peerTypescriptPath, 'lib/tsc.js');
32+
var typeScriptVersion = getTypeScriptVersion(peerTypescriptPath);
33+
1534
if (fs.existsSync(tscPath)) {
16-
try {
17-
logger.info('Found peer TypeScript ' + require(path.join(peerTypescriptPath, 'package.json')).version);
18-
} catch (err) { }
35+
logger.info(`Found peer TypeScript ${typeScriptVersion}`);
1936
} else {
2037
throw Error('TypeScript installation local to project was not found. Install by executing `npm install typescript`.');
2138
}
@@ -35,13 +52,21 @@ function runTypeScriptCompiler(logger, projectDir, options) {
3552
nodeArgs.push('--inlineSourceMap', '--inlineSources');
3653
}
3754

55+
if (this.shouldPreserveWatchOutput(typeScriptVersion)) {
56+
nodeArgs.push('--preserveWatchOutput');
57+
}
58+
3859
logger.trace(process.execPath, nodeArgs.join(' '));
3960
tsc = spawn(process.execPath, nodeArgs);
4061

4162
var isResolved = false;
4263
tsc.stdout.on('data', function (data) {
4364
var stringData = data.toString();
44-
logger.info(stringData);
65+
// Prevent console clear. Fixed the behaviour for typescript 2.7.1 and 2.7.2. Should be deleted after dropping support for 2.7.x version.
66+
// https://github.com/Microsoft/TypeScript/blob/master/src/compiler/sys.ts#L623
67+
if (stringData !== "\x1Bc") {
68+
logger.info(stringData);
69+
}
4570
if (options.watch && stringData.toLowerCase().indexOf("compilation complete. watching for file changes.") !== -1 && !isResolved) {
4671
isResolved = true;
4772
resolve();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"url": "https://github.com/NativeScript/nativescript-dev-typescript.git"
4141
},
4242
"dependencies": {
43-
"nativescript-hook": "^0.2.0"
43+
"nativescript-hook": "^0.2.0",
44+
"semver": "5.5.0"
4445
}
4546
}

0 commit comments

Comments
 (0)