diff --git a/lib/helpers.js b/lib/helpers.js index ef936fe3..0abdc9fa 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -94,6 +94,21 @@ var walk = function(dir, done) { } +var loadMacros = function(projectPath) { + var macros = {} + var macroDir = path.join(projectPath, "_macros/") + if (!fs.existsSync(macroDir)) { + return macros + } + fs.readdirSync(macroDir).forEach(function(file) { + if(!file.match(/.+\.js/)) { + return + } + macros[file.replace('.js', '')] = require(path.join(macroDir, file)); + }) + return macros +} + /** * @@ -131,14 +146,17 @@ exports.setup = function(projectPath, env){ var configPath = path.join(projectPath, "harp.json") var contents = fs.readFileSync(configPath).toString() var publicPath = path.join(projectPath, "public") + var macros = loadMacros(projectPath) }catch(e){ try{ var configPath = path.join(projectPath, "_harp.json") var contents = fs.readFileSync(configPath).toString() var publicPath = projectPath + var macros = loadMacros(projectPath) }catch(e){ var contents = "{}" var publicPath = projectPath + var macros = loadMacros(projectPath) } } @@ -168,6 +186,8 @@ exports.setup = function(projectPath, env){ // e.g. '$foo' -> process.env.foo cfg = envy(cfg) + cfg.globals.macros = macros + return { projectPath : projectPath, publicPath : publicPath, diff --git a/package.json b/package.json index 32fe34a5..ebf42df2 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ , "Remy Sharp " , "Zeke Sikelianos " , "Marc Knaup " + , "Massimiliano Marcon " ], "keywords": [ "static web server", @@ -33,7 +34,7 @@ "url": "https://github.com/sintaxi/harp.git" }, "dependencies": { - "terraform": "0.9.0", + "terraform": "git://github.com/sintaxi/terraform.git#release-v0.9.1", "commander": "2.0.0", "connect": "2.9.0", "fs-extra": "0.6.4", diff --git a/test/apps/macros/_macros/test.js b/test/apps/macros/_macros/test.js new file mode 100644 index 00000000..82383550 --- /dev/null +++ b/test/apps/macros/_macros/test.js @@ -0,0 +1,3 @@ +module.exports = function(someparam) { + return 'macro:' + someparam; +} \ No newline at end of file diff --git a/test/apps/macros/index.ejs b/test/apps/macros/index.ejs new file mode 100644 index 00000000..05746dba --- /dev/null +++ b/test/apps/macros/index.ejs @@ -0,0 +1 @@ +<%- macros.test('foo') %> \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js index 2a904927..1b09807e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -102,6 +102,19 @@ describe("helpers", function(){ done() }) + it("should load macros when available", function(done){ + var cfg = helpers.setup(path.join(__dirname, "apps", "macros")) + cfg.should.have.property("config") + cfg.should.have.property("projectPath") + cfg.should.have.property("publicPath") + cfg.config.should.have.property("globals") + cfg.config.globals.should.have.property("macros") + cfg.config.globals.macros.should.have.property("test") + cfg.config.globals.macros.test.should.be.type("function") + should(cfg.config.globals.macros.test("foo")).equal("macro:foo") + done() + }) + }) describe("prime(outputPath)", function(){