Last active
December 2, 2015 16:41
-
-
Save kfreytag/5a4cca08e38a481c6cb5 to your computer and use it in GitHub Desktop.
Helper for finding handlebars templates in module or in node_modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var path = require('path'); | |
var fs = Promise.promisifyAll(require('fs')); | |
const TEMPLATE_DIRECTORY = 'templates'; | |
// The calling script/handler passes in the directory in which it | |
// lives by calling __dirname | |
var findHandlebarsDirectory = function(handlerDirectory) { | |
return new Promise(function(resolve, reject) { | |
var possiblePaths = [ | |
path.resolve(handlerDirectory, TEMPLATE_DIRECTORY), | |
path.resolve(process.cwd(), 'node_modules', TEMPLATE_DIRECTORY) | |
]; | |
Promise.each(possiblePaths, function(path) { | |
return fs.statAsync(path) | |
.then(function(stats) { | |
if (stats.isDirectory()) { | |
console.log("Directory " + path + " exists"); | |
resolve(path); | |
} | |
}) | |
.catch(function(error) { | |
// IGNORE | |
// console.log(error.path + " does not exist"); | |
}); | |
}); | |
}); | |
}; | |
module.exports = { | |
findHandlebarsDirectory: findHandlebarsDirectory | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment