Skip to content

Commit 35661ee

Browse files
committed
Compiler cache folder is now a local folder of current Meteor app,
Fix bug when diagnostics info is taken from the cache while it's not actual due to new node modules have been installed since Urigo/angular2-meteor#102
1 parent 82696c0 commit 35661ee

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

cache.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ var random = require("random-js")();
1010
var sourceHost = require("./files-source-host").sourceHost;
1111
var Logger = require("./logger").Logger;
1212

13+
function meteorLocalDir() {
14+
var cwdDir = process.cwd();
15+
return cwdDir ? path.join(cwdDir, ".meteor", "local") : __dirname;
16+
}
17+
1318
function ensureCacheDir(cacheDir) {
1419
cacheDir = path.resolve(
1520
cacheDir ||
1621
process.env.TYPESCRIPT_CACHE_DIR ||
17-
path.join(
18-
process.env.HOME || process.env.USERPROFILE || __dirname,
19-
".typescript-cache"
20-
)
22+
path.join(meteorLocalDir(), ".typescript-cache")
2123
);
2224

2325
try {
@@ -144,7 +146,7 @@ function CompileCache(cacheDir) {
144146

145147
var CCp = CompileCache.prototype = new Cache();
146148

147-
CCp.get = function(filePath, options, compileFn, force) {
149+
CCp.get = function(filePath, options, compileFn) {
148150
var source = sourceHost.get(filePath);
149151
var cacheKey = utils.deepHash(pkgVersion, source, options);
150152
var compileResult = this._get(cacheKey);
@@ -153,10 +155,12 @@ CCp.get = function(filePath, options, compileFn, force) {
153155
Logger.debug("file %s result is in cache", filePath);
154156
}
155157

156-
if (! compileResult || force === true) {
157-
compileResult = compileFn();
158-
compileResult.hash = cacheKey;
159-
this._save(cacheKey, compileResult);
158+
var newResult = compileFn(compileResult);
159+
160+
if (newResult) {
161+
newResult.hash = cacheKey;
162+
this._save(cacheKey, newResult);
163+
return newResult;
160164
}
161165

162166
return compileResult;

compile-service.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ CP.getReferences = function(filePath) {
6767
};
6868

6969
CP.getDiagnostics = function(filePath) {
70-
// Parse diagnostics.
71-
var syntactic = tsu.flattenDiagnostics(
72-
this.service.getSyntacticDiagnostics(filePath));
73-
var semantic = tsu.flattenDiagnostics(
74-
this.service.getSemanticDiagnostics(filePath));
75-
return {
76-
syntacticErrors: syntactic,
77-
semanticErrors: semantic
78-
};
70+
return tsu.createDiagnostics(
71+
this.service.getSyntacticDiagnostics(filePath),
72+
this.service.getSemanticDiagnostics(filePath)
73+
)
7974
};

index.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var CompileCache = require("./cache").CompileCache;
1212
var FileCache = require("./cache").FileCache;
1313
var Logger = require("./logger").Logger;
1414
var utils = require("./utils");
15+
var tsu = require("./ts-utils").ts;
1516
var _ = require("underscore");
1617

1718
var compileCache;
@@ -107,6 +108,11 @@ function rebuildWithNewTypings(typings, options) {
107108
return false;
108109
}
109110

111+
var RebuildType = {
112+
FULL: 1,
113+
DIAG: 2
114+
};
115+
110116
function getRebuildMap(filePaths, options) {
111117
assert.ok(options);
112118

@@ -118,7 +124,7 @@ function getRebuildMap(filePaths, options) {
118124

119125
if (serviceHost.isTypingsChanged()) {
120126
_.each(filePaths, function(filePath) {
121-
files[filePath] = true;
127+
files[filePath] = RebuildType.FULL;
122128
});
123129
return files;
124130
}
@@ -136,7 +142,7 @@ function getRebuildMap(filePaths, options) {
136142
var mLen = modules.length;
137143
for (var i = 0; i < mLen; i++) {
138144
if (serviceHost.isFileChanged(modules[i])) {
139-
files[filePath] = true;
145+
files[filePath] = RebuildType.FULL;
140146
break;
141147
}
142148
}
@@ -172,10 +178,32 @@ BP.emit = function(filePath, moduleName) {
172178
return result;
173179
}
174180

175-
return compileCache.get(filePath, fOptions, function() {
176-
Logger.debug("cache miss: %s", filePath);
177-
return compileService.compile(filePath, moduleName);
178-
}, this.rebuildMap[filePath]);
181+
var rebuild = this.rebuildMap[filePath];
182+
return compileCache.get(filePath, fOptions, function(compResult) {
183+
if (! compResult) {
184+
Logger.debug("cache miss: %s", filePath);
185+
return compileService.compile(filePath, moduleName);
186+
}
187+
188+
if (rebuild === RebuildType.FULL) {
189+
Logger.debug("full rebuild: %s", filePath);
190+
return compileService.compile(filePath, moduleName);
191+
}
192+
193+
// If file is not changed but contains errors from previous
194+
// build, then mark it as needs diagnostics re-evaluation.
195+
// This is due to some node modules may become
196+
// available in the mean time.
197+
if (tsu.hasErrors(compResult.diagnostics)) {
198+
Logger.debug("diagnostics re-evaluation: %s", filePath);
199+
compResult.diagnostics =
200+
compileService.getDiagnostics(filePath);
201+
return compResult;
202+
}
203+
204+
// Cached result is up to date, no action required.
205+
return null;
206+
});
179207
};
180208

181209
exports.compile = function compile(fileContent, options) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "meteor-typescript",
33
"author": "@barbatus",
4-
"version": "0.6.0-beta.4",
4+
"version": "0.6.0",
55
"license": "MIT",
66
"description": "TypeScript wrapper package for use with Meteor",
77
"keywords": [

ts-utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ function getReferences(sourceFile) {
5656
};
5757
}
5858

59+
function createDiagnostics(tsSyntactic, tsSemantic) {
60+
// Parse diagnostics to leave only info we need.
61+
var syntactic = flattenDiagnostics(tsSyntactic);
62+
var semantic = flattenDiagnostics(tsSemantic);
63+
return {
64+
syntacticErrors: syntactic,
65+
semanticErrors: semantic
66+
};
67+
}
68+
5969
function flattenDiagnostics(tsDiagnostics) {
6070
var diagnostics = [];
6171

@@ -80,6 +90,13 @@ function flattenDiagnostics(tsDiagnostics) {
8090
return diagnostics;
8191
}
8292

93+
function hasErrors(diagnostics) {
94+
if (! diagnostics) return true;
95+
96+
return !! diagnostics.semanticErrors.length ||
97+
!! diagnostics.syntacticErrors.length;
98+
}
99+
83100
function isSourceMap(fileName) {
84101
return ts.fileExtensionIs(fileName, '.map');
85102
}
@@ -92,6 +109,8 @@ exports.ts = {
92109
normalizePath: normalizePath,
93110
prepareSourceMap: prepareSourceMap,
94111
getReferences: getReferences,
112+
createDiagnostics: createDiagnostics,
113+
hasErrors: hasErrors,
95114
flattenDiagnostics: flattenDiagnostics,
96115
isSourceMap: isSourceMap,
97116
isTypings: isTypings

0 commit comments

Comments
 (0)