Created
April 28, 2017 14:03
-
-
Save blackwright/4369b678ecb180c0ae514cd2b75a7a7d to your computer and use it in GitHub Desktop.
Helpers Index
This file contains hidden or 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
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