Permalink
Browse files

progress towards reheat functionality

  • Loading branch information...
1 parent b8a23c2 commit 2211dc170c711cae5920a634acd8587959ce6d91 @thlorenz committed Aug 6, 2012
Showing with 92 additions and 49 deletions.
  1. +3 −1 .gitignore
  2. +89 −48 hotplates.js
View
@@ -12,4 +12,6 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+
+spikes
View
@@ -1,8 +1,12 @@
-var fs = require('fs')
- , path = require('path')
- , readdirp = require('readdirp')
- , handlebars = require('handlebars')
- , oven = { }
+var fs = require('fs')
+ , path = require('path')
+ , readdirp = require('readdirp')
+ , handlebars = require('handlebars')
+ , oven = { }
+ , templateFiles = [ ]
+ , partialFiles = [ ]
+ , watchers = { }
+ , isWindows = process.platform === 'win32'
;
function folderParts (folder) {
@@ -24,21 +28,22 @@ function namespace(folder) {
, parent = oven;
for (var i = 0; i < parts.length; i++) {
- if (typeof parent[parts[i]] == 'undefined') {
- parent[parts[i]] = {};
+ if (typeof parent[ parts[i] ] == 'undefined') {
+ parent[ parts[i] ] = { };
}
- parent = parent[parts[i]];
+ parent = parent[ parts[i] ];
}
return parent;
}
-function plateNameFromPath(file) {
- return file.name.substr(0, file.name.length - path.extname(file.name).length);
+function plateNameFrom(filename) {
+ return filename.substr(0, filename.length - path.extname(filename).length);
}
function process(opts, processFile, done) {
if (!opts) done(null);
else {
+
if (!opts.fileFilter) opts.fileFilter = [ '*.hbs', '*.handlebars' ];
readdirp(opts, function (err, entries) {
@@ -58,6 +63,7 @@ function process(opts, processFile, done) {
fs.readFile(file.fullPath, function (err, plate) {
if (err) done(err);
else {
+ templateFiles.push(file);
processFile(file, plate.toString());
if (--tasks === 0) done(null);
}
@@ -67,36 +73,82 @@ function process(opts, processFile, done) {
});
}
}
+function processTemplate(file, plate) {
+ var plateName = plateNameFrom(file.name)
+ , attachTo = namespace(file.parentDir);
-function processTemplates (opts, done) {
- process
- ( opts
- , function processFile(file, plate) {
- var plateName = plateNameFromPath(file)
- , attachTo = namespace(file.parentDir);
+ attachTo[plateName] = handlebars.compile(plate);
+}
- attachTo[plateName] = handlebars.compile(plate);
- }
- , done
- );
+function processPartial(file, partial) {
+ var plateName = plateNameFrom(file.name)
+ , namespaces = folderParts(file.parentDir)
+ , partialName = namespaces.length === 0 ? plateName : namespaces.concat(plateName).join('.')
+ ;
+
+ handlebars.registerPartial(partialName, partial);
}
-function processPartials (opts, done) {
- process
- ( opts
- , function processFile(file, partial) {
- var plateName = plateNameFromPath(file)
- , namespaces = folderParts(file.parentDir)
- , partialName = namespaces.length === 0 ? plateName : namespaces.concat(plateName).join('.')
- ;
-
- handlebars.registerPartial(partialName, partial);
- }
- , done
- );
+function watch(file, update) {
+ var fullPath = file.fullPath;
+
+ if (watchers[fullPath]) return;
+
+ watchers[fullPath] = true;
+
+ if (isWindows) {
+ // slower, but oh well
+ fs.watch(fullPath, function (event) {
+ if (event === 'change') update(fullPath);
+ });
+ } else {
+ fs.watchFile(fullPath, { persistent: true, interval: 100 }, function (event) {
+ if (event === 'change') update(file);
+ });
+ }
+}
+
+function keepWarm(file, process) {
+ watch(file, function (file) {
+ fs.readFile(file.fullPath,function (err, plate) {
+ if (err) console.error(err);
+ else process(file, plate);
+ });
+ });
+}
+
+function keepTemplateWarm (file) {
+ keepWarm(file, processTemplate);
+}
+
+function keepPartialWarm (file) {
+ keepWarm(file, processPartial);
+}
+
+function watch(opts) {
+ if (!opts.reheat) return;
+
+ // Watch all template files for changes
+ templateFiles.forEach(keepWarm);
+ // Watch all template folders for added/removed files
}
function heat(opts, hot) {
+ function processTemplates (opts, done) {
+ process
+ ( opts
+ , function(file, plate) { processTemplate(file, plate); templateFiles.push(file.fullPath); }
+ , done
+ );
+ }
+
+ function processPartials (opts, done) {
+ process
+ ( opts
+ , function(file, plate) { processPartial(file, plate); partialFiles.push(file.fullPath); }
+ , done
+ );
+ }
function continueWithPartials (err) {
if (err) hot(err);
@@ -116,22 +168,11 @@ function burn() {
Object.keys(handlebars.partials).forEach(function (key) {
delete handlebars.partials[key];
});
+ templateFiles = [];
}
module.exports = {
- heat: heat
- , burn : burn
- , oven: oven
+ heat : heat
+ , burn : burn
+ , oven : oven
};
-
-/*var opts = {
- templatesPath: path.join(__dirname, '../readarepo-zip', 'lib', 'templates')
- , directories: '!partials'
- };
-
-store(opts, function (err) {
- if (err) console.log(err);
- else console.log(handledbars);
-
-});
-*/

0 comments on commit 2211dc1

Please sign in to comment.