-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assumptions made on helper names #12
Comments
I can make a PR if you wish: const fs = require('fs');
const path = require('path');
const cache = require('./cache');
const traverseDir = require('./traverse-dir');
const resolveRootDir = require('./resolve-root-dir');
/**
* Creates a map of helper names to absolute paths to be required.
* @param {Array} helperDirs - list of absolute paths where helpers are to be found
* @param {String} rootDir - Jest execution root directory: root of the directory containing the project package.json
* @param {Object} helpers - Name/path map of helpers a la Mustache.registerHelper({})
* @returns {Object} - helper module names mapped to their absolute paths
*/
module.exports = function findHelpers(helperDirs, rootDir, helpers = {}) {
const cachedHelpers = cache.get('foundHelpers');
if (cachedHelpers) return cachedHelpers;
let foundHelpers = {};
if (helperDirs) {
helperDirs.forEach(function(helperDir) {
const dir = resolveRootDir(helperDir, rootDir);
try {
traverseDir(dir, function(filepath) {
if (path.extname(filepath) !== '.js') return;
const fullHelperPath = path.resolve(dir, filepath);
const pathFromBaseDir = path.relative(dir, fullHelperPath);
const helperName = getHelperNameWithoutExtension(pathFromBaseDir);
foundHelpers[helperName] = fullHelperPath;
})
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(
'[handlebars-jest] No such directory for helperDirs:',
dir
);
} else {
throw err;
}
}
});
}
Object.keys(helpers).forEach( helperName => {
const fullHelperPath = resolveRootDir( helpers[helperName], rootDir);
if (fs.existsSync(fullHelperPath)){
foundHelpers[helperName] = fullHelperPath;
} else {
console.warn('Could not find helper "%s" at path "%s"',
helperName, fullHelperPath
);
}
});
cache.set('foundHelpers', foundHelpers);
return foundHelpers;
};
function getHelperNameWithoutExtension(filepath) {
const basename = path.basename(filepath, '.js');
const partialName = filepath
.split(path.sep)
.slice(0, -1)
.concat(basename)
.join(path.sep);
return partialName;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find-helpers.js
:Perhaps soluble by adding to the config a list of helpers, in the manner of Moustache's
registerHelper
method?Not yet checked it this applies to partials, too.
The text was updated successfully, but these errors were encountered: