Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not look if it exists, you should just try to read it and then catch the error there, now your are essential reading the dir twice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to avoid try/catch based programming, but I understand your point about performance. I can change that.

return macros
}
fs.readdirSync(macroDir).forEach(function(file) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using sync functions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context, how would you handle that asynchronously?

if(!file.match(/.+\.js/)) {
return
}
macros[file.replace('.js', '')] = require(path.join(macroDir, file));
})
return macros
}


/**
*
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
, "Remy Sharp <remy@remysharp.com>"
, "Zeke Sikelianos <zeke@sikelianos.com>"
, "Marc Knaup <marc.knaup@koeln.de>"
, "Massimiliano Marcon <me@marcon.me>"
],
"keywords": [
"static web server",
Expand Down
3 changes: 3 additions & 0 deletions test/apps/macros/_macros/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(someparam) {
return 'macro:' + someparam;
}
1 change: 1 addition & 0 deletions test/apps/macros/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- macros.test('foo') %>
13 changes: 13 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down