Skip to content

Instantly share code, notes, and snippets.

@kfreytag
Last active December 2, 2015 16:41
Show Gist options
  • Save kfreytag/5a4cca08e38a481c6cb5 to your computer and use it in GitHub Desktop.
Save kfreytag/5a4cca08e38a481c6cb5 to your computer and use it in GitHub Desktop.
Helper for finding handlebars templates in module or in node_modules
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