Skip to content

Commit

Permalink
feat(engine-handlebars): Default location for helpers, like engine-nu…
Browse files Browse the repository at this point in the history
…njucks
  • Loading branch information
engelfrost committed Oct 11, 2018
1 parent f330b5b commit 11c4180
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions packages/engine-handlebars/lib/engine_handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function escapeAtPartialBlock(partialString) {
return partial;
}

function loadHelpers(helpers) {
helpers.forEach(globPattern => {
glob.sync(globPattern).forEach(filePath => {
require(path.join(process.cwd(), filePath))(Handlebars);
});
});
}

const engine_handlebars = {
engine: Handlebars,
engineName: 'handlebars',
Expand Down Expand Up @@ -125,26 +133,32 @@ const engine_handlebars = {
},

/**
* Accept a Pattern Lab config object from the core and put it in
* this module's closure scope so we can configure engine behavior.
* Accept a Pattern Lab config object from the core and use the settings to
* load helpers.
*
* @param {object} config - the global config object from core
*/
usePatternLabConfig: function(config) {
let helpers;

try {
let helpers = config.engines.handlebars.helpers;
// Look for helpers in the config
helpers = config.engines.handlebars.helpers;

if (typeof helpers === 'string') {
helpers = [helpers];
}

helpers.forEach(globPattern => {
glob.sync(globPattern).forEach(filePath => {
require(path.join(process.cwd(), filePath))(Handlebars);
});
});
} catch (error) {
// No helpers to load
// Look for helpers in default location
const configPath = 'patternlab-handlebars-config.js';
if (fs.existsSync(path.join(process.cwd(), configPath))) {
helpers = [configPath];
}
}

// Load helpers if they were found
if (helpers) {
loadHelpers(helpers);
}
},
};
Expand Down

0 comments on commit 11c4180

Please sign in to comment.