helper.js
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var Store = require('../store');
HelperStore.super_.call(this);
var pathHelper = new (require('../helper/path'))();
// Load default framework helpers
var defaultHelperPath = path.normalize(__dirname + '/../helper');
pathHelper.list(defaultHelperPath).forEach(function(helperPath) {
if (!helperPath.match(/\.js$/i)) {
self.loadHelper(self.pathToName(helperPath, defaultHelperPath), helperPath);
util.inherits(HelperStore, Store);
* Load all helpers of the application.
HelperStore.prototype.loadApplicationHelpers = function()
// Load all modules from the application
var defaultModulePath = process.cwd() + '/modules/';
// Skip loading of application helpers on environments
// which does not provides a modules directory
if (!fs.existsSync(defaultModulePath)) {
fs.readdirSync(defaultModulePath).forEach(function(module) {
if (!fs.statSync(defaultModulePath + module).isDirectory()) {
* Convert the path to the valid helper-keys format.
* @param {String} path - Absolute path to the helper file
* @param {String} helperPath - Cutting prefix on absolute paths
* @param {String} [module] - Module name to prefix
HelperStore.prototype.pathToName = function(path, helperPath, module)
throw new Error('Module was not given');
throw new Error('Helper path was not given');
var name = (module) ? (module + '.') : '';
return name += path.replace(helperPath, '')
* Load all helpers for a given module.
* @param {String} module - Name of the module
HelperStore.prototype.loadModule = function(module)
throw new Error('Module was not given');
var modulePath = process.cwd() + '/modules/' + module + '/helpers/';
if (!fs.existsSync(modulePath)) {
this.get('path').list(modulePath).filter(function(helperPath) {
return helperPath.match(/\.js$/i);
var key = self.pathToName(helperPath, modulePath, module);
self.loadHelper(item.name, item.path);
* Load an helper as given key and load the file.
* @param {String} key - Helper name (namespace/key)
* @param {String} path - Absolute path to the helper file
HelperStore.prototype.loadHelper = function(key, path)
if ('function' === typeof helper) {
return this.set(key, new helper());
console.log('Could not load the helper: ' + path, helper);
throw new Error('Error occured while loading an helper.');
if ('object' === typeof helper) {
console.log('Could not load the helper: ' + path, helper);
throw new Error('Helper is not of supported type.');
* Wrapper for the original store method.
* @param {String} [namespace] - Namespace or the default-namespace
* @param {String} key - Key to get
HelperStore.prototype.get = function()
var helper = HelperStore.super_.prototype.get.apply(this, arguments);
'Helper "' + arguments[0] + '" is not registered.' +