Skip to content

Commit

Permalink
Bug fixes and re-factoring: now service host has new methods to tell …
Browse files Browse the repository at this point in the history
…if files or typings have been changed since last interaction

New unit test

Urigo/angular2-meteor#102
  • Loading branch information
barbatus committed Mar 10, 2016
1 parent c4b62ae commit 31090f0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 45 deletions.
11 changes: 4 additions & 7 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ CCp.get = function(filePath, options, compileFn, force) {
return compileResult;
};

Cp.resultChanged = function(filePath, options) {
CCp.resultChanged = function(filePath, options) {
var source = sourceHost.get(filePath);
var cacheKey = utils.deepHash(pkgVersion, source, options);
var compileResult = this._cache.get(cacheKey);
Expand All @@ -181,19 +181,16 @@ exports.FileCache = FileCache;
var FCp = FileCache.prototype = new Cache();

FCp.save = function(filePath, content) {
var content = content === undefined ?
content = content === undefined ?
sourceHost.get(filePath) : content;

var cacheKey = utils.deepHash(filePath);
var contentHash = utils.deepHash(content);
this._save(cacheKey, contentHash);
};

FCp.isChanged = function(filePath, content) {
var content = content === undefined ?
sourceHost.get(filePath) : content;
if (! content) return true;

content = content === undefined ?
sourceHost.get(filePath) : content;
var cacheKey = utils.deepHash(filePath);
var contentHash = utils.deepHash(content);
return this._get(cacheKey) != contentHash;
Expand Down
56 changes: 36 additions & 20 deletions compile-service-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var _ = require("underscore");
var sourceHost = require("./files-source-host").sourceHost;
var tsu = require("./ts-utils").ts;

function CompileServiceHost(compileCache, typingsCache) {
this.compileCache = compileCache;
this.typingsCache = typingsCache;

function CompileServiceHost(fileCache) {
this.files = {};
this.fileCache = fileCache;
this.typingsChanged = false;
this.appId = ts.sys.getCurrentDirectory();
this.webArchExp = new RegExp("^web\.");
}

Expand All @@ -20,40 +20,56 @@ var SH = CompileServiceHost.prototype;

SH.setFiles = function(filePaths, options) {
this.options = options;
var typingsChanged = false;
var typings = {};

_.each(filePaths, function(filePath) {
if (! this.files[filePath]) {
this.files[filePath] = { version: 1 };
return;
}
var isTypings = tsu.isTypings(filePath);
if (isTypings) typings[filePath] = true;

if (tsu.isTypings(filePath)) {
if (this.typingsCache.isChanged(filePath)) {
this.files[filePath].version++;
}
return;
if (! this.files[filePath]) {
this.files[filePath] = { version: 0 };
}

if (this.compileCache.resultChanged(filePath, options)) {
if (this.fileCache.isChanged(filePath)) {
this.files[filePath].version++;
this.files[filePath].changed = true;
typingsChanged = typingsChanged || isTypings;
this.fileCache.save(filePath);
}
}, this);

this.typingsChanged = typingsChanged ||
this.fileCache.isChanged(this.appId, typings);
this.fileCache.save(this.appId, typings);
};

SH.isFileChanged = function(filePath) {
return this.files[filePath].changed;
};

SH.isTypingsChanged = function() {
return this.typingsChanged;
};

SH.getScriptFileNames = function() {
var rootFilePaths = [];
var rootFilePaths = {};
for (var filePath in this.files) {
rootFilePaths.push(filePath);
rootFilePaths[filePath] = true;
}

// Add in options.typings, which is used
// to set up typings that should be read from disk.
var typings = this.options.typings;
if (typings) {
_.each(typings, function(filePath) {
if (! this.files[filePath]) {
rootFilePaths.push(filePath);
if (! rootFilePaths[filePath]) {
rootFilePaths[filePath] = true;
}
}, this);
});
}
return rootFilePaths;

return _.keys(rootFilePaths);
};

SH.getScriptVersion = function(filePath) {
Expand Down
24 changes: 9 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ var utils = require("./utils");
var _ = require("underscore");

var compileCache;
var typingsCache;
var fileCache;
function setCacheDir(cacheDir) {
if (compileCache && compileCache.cacheDir === cacheDir) {
return;
}

compileCache = new CompileCache(cacheDir);
typingsCache = new FileCache(cacheDir);
fileCache = new FileCache(cacheDir);
};

exports.setCacheDir = setCacheDir;
Expand All @@ -44,7 +44,7 @@ function lazyInit() {
}

if (! serviceHost) {
serviceHost = new ServiceHost(compileCache, typingsCache);
serviceHost = new ServiceHost(fileCache);
}

if (! compileService) {
Expand Down Expand Up @@ -72,28 +72,22 @@ function TSBuild(filePaths, getFileContent, options) {
this.rebuildMap = getRebuildMap(filePaths, resOptions);
}

function rebuildWithNewTypings(filePath, typings) {
typings = typings || [];
var rebuild = typingsCache.isChanged(filePath, typings);
typingsCache.save(filePath, typings);
function rebuildWithNewTypings(typings) {
if (! typings) return false;

var tLen = typings.length;
for (var i = 0; i < tLen; i++) {
var path = typings[i];
if (typingsCache.isChanged(path)) {
typingsCache.save(path);
rebuild = true;
}
if (serviceHost.isFileChanged(path)) return true;
}

return rebuild;
return false;
}

function getRebuildMap(filePaths, options) {
var files = {};

var appPath = ts.sys.getCurrentDirectory();
if (rebuildWithNewTypings(appPath, options.typings)) {
if (serviceHost.isTypingsChanged()) {
_.each(filePaths, function(filePath) {
files[filePath] = true;
});
Expand All @@ -105,7 +99,7 @@ function getRebuildMap(filePaths, options) {
var result = compileCache.get(filePath, options);
var refs = result.references;
if (refs) {
files[filePath] = rebuildWithNewTypings(filePath, refs.typings);
files[filePath] = rebuildWithNewTypings(refs.typings);
if (files[filePath]) return;

var modules = refs.modules;
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.2",
"version": "0.6.0-beta.3",
"license": "MIT",
"description": "TypeScript wrapper package for use with Meteor",
"keywords": [
Expand Down
23 changes: 22 additions & 1 deletion tests/ts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe("meteor-typescript -> ", function() {

it("should update diagnostics when file's references has changed", function() {
var foo9 = "module foo9 { export var foo = 'foo' }";
var foo10 = "/// <reference path='foo9.ts'> \n" +
var foo10 = "/// <reference path='foo9.ts' /> \n " +
"module foo10 { export var foo = foo9.foo }";

var build1 = new TSBuild(["foo9.ts", "foo10.ts"], function(filePath) {
Expand All @@ -270,6 +270,7 @@ describe("meteor-typescript -> ", function() {
var result2 = build2.emit("foo10.ts");

expect(result2.diagnostics.semanticErrors.length).toEqual(1);
expect(result2.diagnostics.semanticErrors[0].message).toContain("'foo' does not exist");
});

it("should handle ambient typings properly", function() {
Expand All @@ -285,5 +286,25 @@ describe("meteor-typescript -> ", function() {

expect(result1.diagnostics.semanticErrors.length).toEqual(0);
});

it("should take result from cache for once compiled code", function() {
var build1 = new TSBuild(["foo13.ts"], function(filePath) {
if (filePath === "foo13.ts") return testCodeLine;
});
var result1 = build1.emit("foo13.ts");

var changedCode = "export const foo1 = 'foo'";
var build2 = new TSBuild(["foo13.ts"], function(filePath) {
if (filePath === "foo13.ts") return changedCode;
});
var result2 = build2.emit("foo13.ts");

var build3 = new TSBuild(["foo13.ts"], function(filePath) {
if (filePath === "foo13.ts") return testCodeLine;
});
var result3 = build3.emit("foo13.ts");

expect(result3).toEqual(result1);
});
});
});
2 changes: 1 addition & 1 deletion ts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function isSourceMap(fileName) {
}

function isTypings(fileName) {
return /\.d\.ts$/.test(fileName);
return ts.fileExtensionIs(fileName, '.d.ts');
}

exports.ts = {
Expand Down

0 comments on commit 31090f0

Please sign in to comment.