Skip to content

Commit

Permalink
Compiler cache folder is now a local folder of current Meteor app,
Browse files Browse the repository at this point in the history
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
  • Loading branch information
barbatus committed Apr 12, 2016
1 parent 82696c0 commit 35661ee
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
22 changes: 13 additions & 9 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ var random = require("random-js")();
var sourceHost = require("./files-source-host").sourceHost;
var Logger = require("./logger").Logger;

function meteorLocalDir() {
var cwdDir = process.cwd();
return cwdDir ? path.join(cwdDir, ".meteor", "local") : __dirname;
}

function ensureCacheDir(cacheDir) {
cacheDir = path.resolve(
cacheDir ||
process.env.TYPESCRIPT_CACHE_DIR ||
path.join(
process.env.HOME || process.env.USERPROFILE || __dirname,
".typescript-cache"
)
path.join(meteorLocalDir(), ".typescript-cache")
);

try {
Expand Down Expand Up @@ -144,7 +146,7 @@ function CompileCache(cacheDir) {

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

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

if (! compileResult || force === true) {
compileResult = compileFn();
compileResult.hash = cacheKey;
this._save(cacheKey, compileResult);
var newResult = compileFn(compileResult);

if (newResult) {
newResult.hash = cacheKey;
this._save(cacheKey, newResult);
return newResult;
}

return compileResult;
Expand Down
13 changes: 4 additions & 9 deletions compile-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ CP.getReferences = function(filePath) {
};

CP.getDiagnostics = function(filePath) {
// Parse diagnostics.
var syntactic = tsu.flattenDiagnostics(
this.service.getSyntacticDiagnostics(filePath));
var semantic = tsu.flattenDiagnostics(
this.service.getSemanticDiagnostics(filePath));
return {
syntacticErrors: syntactic,
semanticErrors: semantic
};
return tsu.createDiagnostics(
this.service.getSyntacticDiagnostics(filePath),
this.service.getSemanticDiagnostics(filePath)
)
};
40 changes: 34 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var CompileCache = require("./cache").CompileCache;
var FileCache = require("./cache").FileCache;
var Logger = require("./logger").Logger;
var utils = require("./utils");
var tsu = require("./ts-utils").ts;
var _ = require("underscore");

var compileCache;
Expand Down Expand Up @@ -107,6 +108,11 @@ function rebuildWithNewTypings(typings, options) {
return false;
}

var RebuildType = {
FULL: 1,
DIAG: 2
};

function getRebuildMap(filePaths, options) {
assert.ok(options);

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

if (serviceHost.isTypingsChanged()) {
_.each(filePaths, function(filePath) {
files[filePath] = true;
files[filePath] = RebuildType.FULL;
});
return files;
}
Expand All @@ -136,7 +142,7 @@ function getRebuildMap(filePaths, options) {
var mLen = modules.length;
for (var i = 0; i < mLen; i++) {
if (serviceHost.isFileChanged(modules[i])) {
files[filePath] = true;
files[filePath] = RebuildType.FULL;
break;
}
}
Expand Down Expand Up @@ -172,10 +178,32 @@ BP.emit = function(filePath, moduleName) {
return result;
}

return compileCache.get(filePath, fOptions, function() {
Logger.debug("cache miss: %s", filePath);
return compileService.compile(filePath, moduleName);
}, this.rebuildMap[filePath]);
var rebuild = this.rebuildMap[filePath];
return compileCache.get(filePath, fOptions, function(compResult) {
if (! compResult) {
Logger.debug("cache miss: %s", filePath);
return compileService.compile(filePath, moduleName);
}

if (rebuild === RebuildType.FULL) {
Logger.debug("full rebuild: %s", filePath);
return compileService.compile(filePath, moduleName);
}

// If file is not changed but contains errors from previous
// build, then mark it as needs diagnostics re-evaluation.
// This is due to some node modules may become
// available in the mean time.
if (tsu.hasErrors(compResult.diagnostics)) {
Logger.debug("diagnostics re-evaluation: %s", filePath);
compResult.diagnostics =
compileService.getDiagnostics(filePath);
return compResult;
}

// Cached result is up to date, no action required.
return null;
});
};

exports.compile = function compile(fileContent, options) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "meteor-typescript",
"author": "@barbatus",
"version": "0.6.0-beta.4",
"version": "0.6.0",
"license": "MIT",
"description": "TypeScript wrapper package for use with Meteor",
"keywords": [
Expand Down
19 changes: 19 additions & 0 deletions ts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ function getReferences(sourceFile) {
};
}

function createDiagnostics(tsSyntactic, tsSemantic) {
// Parse diagnostics to leave only info we need.
var syntactic = flattenDiagnostics(tsSyntactic);
var semantic = flattenDiagnostics(tsSemantic);
return {
syntacticErrors: syntactic,
semanticErrors: semantic
};
}

function flattenDiagnostics(tsDiagnostics) {
var diagnostics = [];

Expand All @@ -80,6 +90,13 @@ function flattenDiagnostics(tsDiagnostics) {
return diagnostics;
}

function hasErrors(diagnostics) {
if (! diagnostics) return true;

return !! diagnostics.semanticErrors.length ||
!! diagnostics.syntacticErrors.length;
}

function isSourceMap(fileName) {
return ts.fileExtensionIs(fileName, '.map');
}
Expand All @@ -92,6 +109,8 @@ exports.ts = {
normalizePath: normalizePath,
prepareSourceMap: prepareSourceMap,
getReferences: getReferences,
createDiagnostics: createDiagnostics,
hasErrors: hasErrors,
flattenDiagnostics: flattenDiagnostics,
isSourceMap: isSourceMap,
isTypings: isTypings
Expand Down

0 comments on commit 35661ee

Please sign in to comment.