Simplified checkUpToDate() and added getLastModified()

This commit is contained in:
Patrick Steele-Idem 2014-09-12 17:02:01 -06:00
parent 2843f29c81
commit e49d0f31cb
3 changed files with 49 additions and 14 deletions

View File

@ -30,6 +30,7 @@ var ElementNode = require('./ElementNode');
var TextNode = require('./TextNode');
var TagHandlerNode = require('../taglibs/core/TagHandlerNode');
var raptorModulesResolver = require('raptor-modules/resolver');
var fs = require('fs');
function TemplateCompiler(path, options) {
this.dirname = nodePath.dirname(path);
@ -54,7 +55,7 @@ TemplateCompiler.prototype = {
function transformTreeHelper(node) {
try {
_this.taglibs.forEachNodeTransformer(node, function (transformer) {
if (!node.isTransformerApplied(transformer)) {
//Check to make sure a transformer of a certain type is only applied once to a node
node.setTransformerApplied(transformer);
@ -122,7 +123,7 @@ TemplateCompiler.prototype = {
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + (e.stack || e)), e);
return returnError(err);
}
try {
/*
* The tree has been transformed and we can now generate
@ -132,7 +133,7 @@ TemplateCompiler.prototype = {
var err = createError(new Error('An error occurred while trying to compile template at path "' + filePath + '". Exception: ' + e), e);
return returnError(err);
}
if (this.hasErrors()) {
var message = 'An error occurred while trying to compile template at path "' + filePath + '". Error(s) in template:\n';
var errors = _this.getErrors();
@ -226,7 +227,7 @@ TemplateCompiler.prototype = {
var tagName = Ctor;
Ctor = this.getNodeClass(tagName);
}
ok(Ctor != null, 'Ctor is required');
ok(typeof Ctor === 'function', 'Ctor should be a function');
@ -239,14 +240,38 @@ TemplateCompiler.prototype = {
return new TextNode(text, escapeXml);
},
checkUpToDate: function(sourceFile, targetFile) {
getLastModified: function() {
var sourceFile = this.path;
var statSource = fs.statSync(sourceFile);
var lastModifiedTime = statSource.mtime.getTime();
var taglibFiles = this.taglibs.getInputFiles();
var len = taglibFiles.length;
for (var i=0; i<len; i++) {
var taglibFileStat;
var taglibFile = taglibFiles[i];
try {
taglibFileStat = fs.statSync(taglibFile);
} catch(e) {
continue;
}
lastModifiedTime = Math.max(lastModifiedTime, taglibFileStat.mtime.getTime());
}
return lastModifiedTime;
},
checkUpToDate: function(targetFile) {
if (this.options.checkUpToDate === false) {
return false;
}
var fs = require('fs');
var sourceFile = this.path;
var statTarget;
try {
@ -262,7 +287,7 @@ TemplateCompiler.prototype = {
}
// Now check if any of the taglib files have been modified after the target file was generated
var taglibFiles = this.taglibs.getInputFiles();
var len = taglibFiles.length;
for (var i=0; i<len; i++) {
@ -287,4 +312,4 @@ TemplateCompiler.prototype = {
return raptorModulesResolver.deresolve(targetModuleFile, this.dirname);
}
};
module.exports = TemplateCompiler;
module.exports = TemplateCompiler;

View File

@ -84,6 +84,16 @@ extend(exports, {
});
},
getLastModified: function(path, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var compiler = this.createCompiler(path, options);
callback(null, compiler.getLastModified());
},
Node: require('./Node'),
ElementNode: require('./ElementNode'),
TextNode: require('./TextNode'),

View File

@ -21,10 +21,10 @@ function loadSource(templatePath, compiledSrc) {
module.exports = function load(templatePath) {
templatePath = nodePath.resolve(cwd, templatePath);
var targetFile = templatePath + '.js';
var compiler = raptorTemplatesCompiler.createCompiler(templatePath);
var isUpToDate = compiler.checkUpToDate(templatePath, targetFile);
var isUpToDate = compiler.checkUpToDate(targetFile);
if (isUpToDate) {
return require(targetFile);
}
@ -33,10 +33,10 @@ module.exports = function load(templatePath) {
var compiledSrc = compiler.compile(templateSrc);
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
fs.writeFileSync(targetFile, compiledSrc, {encoding: 'utf8'});
return require(targetFile);
};
module.exports.loadSource = loadSource;
module.exports.loadSource = loadSource;