Skip to content

Instantly share code, notes, and snippets.

@blackwright
Created April 28, 2017 14:03
Show Gist options
  • Save blackwright/4369b678ecb180c0ae514cd2b75a7a7d to your computer and use it in GitHub Desktop.
Save blackwright/4369b678ecb180c0ae514cd2b75a7a7d to your computer and use it in GitHub Desktop.
Helpers Index
const fs = require('fs');
const path = require('path');
const express = require('express');
const basename = path.basename(__filename);
const Helpers = {};
// Object to hold registered helpers
Helpers.registered = {};
// Register a single helper or
// a module
Helpers.register = function(key, fn) {
if (typeof key === 'object') {
// Iterate through keys
let helpers = key;
for (let key in helpers) {
// Register helper function
const fn = helpers[key];
this.registered[key] = fn;
}
} else {
// Register a single helper
// function
this.registered[key] = fn;
}
};
// Register all helper files
let files = fs.readdirSync(__dirname);
files.forEach((filename) => {
// If the file is not this file
if (filename !== basename) {
// Require it and register its
// helpers
let helperModule = require(`./${ filename }`);
Helpers.register(helperModule);
}
});
module.exports = Helpers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment