punch
Advanced tools
Comparing version 0.0.3-beta to 0.0.4-beta
@@ -14,3 +14,3 @@ var fs = require("fs"); | ||
callbacksForPartial: {}, | ||
registerRenderer: function(content_type, handler){ | ||
@@ -247,41 +247,6 @@ var self = this; | ||
// if a directory in output path doesn't exist, | ||
// create it. | ||
var createOutputDirs = function(path, dir_list, callback){ | ||
var current_output_dir = path + dir_list.shift() + "/"; | ||
var proceedToNextOrCallback = function(){ | ||
if(dir_list.length === 0){ | ||
callback(); | ||
} else { | ||
createOutputDirs(current_output_dir, dir_list, callback); | ||
} | ||
} | ||
fs.stat(current_output_dir, function(err, stats){ | ||
if(err || !stats.isDirectory()){ | ||
try { | ||
fs.mkdirSync(current_output_dir); | ||
console.log("Created " + current_output_dir + " directory"); | ||
} | ||
catch(e) { | ||
if(e.code === "EEXIST"){ | ||
proceedToNextOrCallback(); | ||
}; | ||
} | ||
} | ||
proceedToNextOrCallback(); | ||
}); | ||
}; | ||
createOutputDirs("", output_dirs, function(){ | ||
fs.writeFile(output_file_path, output, function(err){ | ||
if(err) throw err; | ||
console.log("Created " + output_file_path ); | ||
}); | ||
fs.writeFile(output_file_path, output, function(err){ | ||
if(err) throw err; | ||
console.log("Created " + output_file_path ); | ||
}); | ||
}; | ||
@@ -329,9 +294,6 @@ | ||
// Start traversing through template directory | ||
var traverseDir = function(path){ | ||
fs.readdir(path, function(err, files){ | ||
// An error can occur due to given path is a file, | ||
// or path is not valid | ||
// in either case pass the path to static file handler | ||
// if not a directory try to read it as a statc file | ||
if(err) { | ||
@@ -342,2 +304,9 @@ self.staticFileHandler(path, config); | ||
//create sub directories in the output path | ||
if(path !== config.template_dir){ | ||
var output_path = path.split("/"); | ||
output_path[0] = config.output_dir; | ||
fs.mkdirSync(output_path.join("/")); | ||
} | ||
files.forEach(function(file){ | ||
@@ -358,2 +327,3 @@ if(file.indexOf(".mustache") > -1){ | ||
// Start traversing through template directory | ||
traverseDir(config.template_dir) | ||
@@ -394,2 +364,5 @@ }, | ||
// Create the output directory | ||
fs.mkdirSync(config.output_dir); | ||
// traverse templates | ||
@@ -396,0 +369,0 @@ self.traverseTemplates(config); |
@@ -5,3 +5,3 @@ { | ||
, "keywords": ["static", "sites", "mustache", "json", "markdown"] | ||
, "version": "0.0.3-beta" | ||
, "version": "0.0.4-beta" | ||
, "homepage": "https://github.com/laktek/punch" | ||
@@ -8,0 +8,0 @@ , "author": "Lakshan Perera <lakshan@web2media.net> (http://laktek.com)" |
@@ -109,3 +109,2 @@ var mustache_renderer = require("../lib/renderers/mustache.js"); | ||
var callback = jasmine.createSpy(); | ||
var output = mustache_instance.render(); | ||
@@ -112,0 +111,0 @@ |
@@ -48,5 +48,6 @@ var util = require("util"); | ||
spyOn(fs, 'mkdirSync'); | ||
spyOn(punch, "fetchAndRender"); | ||
punch.traverseTemplates(config, punch.contentRenderer, function(){ }); | ||
punch.traverseTemplates(config); | ||
@@ -57,2 +58,22 @@ expect(punch.fetchAndRender).toHaveBeenCalledWith("templates/sub_dir/sub.mustache", config); | ||
it("creates sub-directories in the output path", function(){ | ||
var config = {"template_dir": "templates", "output_dir": "public"}; | ||
spyOn(fs, 'mkdirSync'); | ||
spyOn(punch, "fetchAndRender"); | ||
spyOn(fs, 'readdir').andCallFake(function(path, callback){ | ||
if(fs.readdir.mostRecentCall.args[0] === "templates"){ | ||
callback(null, ["index.mustache", "sub_dir"]); | ||
} else { | ||
callback(null, ["sub.mustache"]); | ||
} | ||
}); | ||
punch.traverseTemplates(config); | ||
expect(fs.mkdirSync).toHaveBeenCalledWith("public/sub_dir"); | ||
}); | ||
it("calls to render content when a template is found", function(){ | ||
@@ -69,2 +90,3 @@ var config = {"template_dir": "templates"}; | ||
spyOn(fs, 'mkdirSync'); | ||
spyOn(punch, "fetchAndRender"); | ||
@@ -89,2 +111,3 @@ | ||
spyOn(fs, 'mkdirSync'); | ||
spyOn(punch, "fetchAndRender"); | ||
@@ -99,3 +122,3 @@ | ||
it("calls to handle static files", function() { | ||
it("handle other file types as static files", function() { | ||
var config = {"template_dir": "templates", "output_dir": "public"}; | ||
@@ -107,6 +130,7 @@ | ||
} else { | ||
callback("Not a directory", null); | ||
callback({errno: 27, code: 'ENOTDIR'}, null); | ||
} | ||
}); | ||
spyOn(fs, 'mkdirSync'); | ||
spyOn(punch, "staticFileHandler"); | ||
@@ -120,2 +144,3 @@ | ||
}); | ||
@@ -168,3 +193,3 @@ | ||
it("instantiatesa new renderer", function(){ | ||
it("instantiates a new renderer", function(){ | ||
var config = {}; | ||
@@ -253,33 +278,2 @@ | ||
it("creates the output directory if it doesn't exist", function(){ | ||
var config = {"output_dir": "public", "output_extension": ".html"}; | ||
var fake_renderer = { | ||
afterRender: null | ||
}; | ||
spyOn(punch, "rendererFor").andCallFake(function(){ | ||
return fake_renderer; | ||
}); | ||
spyOn(punch, "fetchTemplate"); | ||
spyOn(punch, "fetchContent"); | ||
spyOn(punch, "fetchPartials"); | ||
spyOn(fs, "stat").andCallFake(function(path, callback){ | ||
var fake_stats = {isDirectory: function(){ return false; }}; | ||
callback("directory doesn't exist", null); | ||
}); | ||
spyOn(fs, "writeFile"); | ||
spyOn(fs, "mkdirSync"); | ||
punch.fetchAndRender("templates/sub/simple.mustache", config); | ||
fake_renderer.afterRender("sample output"); | ||
expect(fs.mkdirSync.callCount).toEqual(2); | ||
}); | ||
}); | ||
@@ -286,0 +280,0 @@ |
38032
899