Skip to content

Commit

Permalink
feat(storefront): bctheme-1063 modify stencil to start locally with c…
Browse files Browse the repository at this point in the history
…omponents ui library
  • Loading branch information
bc-alexsaiannyi committed Mar 24, 2022
1 parent 4ef424e commit 12ce05c
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/template-assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ const upath = require('upath');
const partialRegex = /\{\{>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}}/g;
const dynamicComponentRegex = /\{\{\s*?dynamicComponent\s*(?:'|")([_|\-|a-zA-Z0-9/]+)(?:'|").*?}}/g;

/**
* Takes a templates folder and template name. It returns simple path for custom template if it's available
* or use default folder instead
*
* @param {string} templatesFolder
* @param {string} templateName
*/
function getCustomPath(templatesFolder, templateName) {
const packageMarker = 'external/';
let customTemplatesDir;
let customTemplateName;

if (templateName.startsWith(packageMarker)) {
customTemplatesDir = templatesFolder.replace('templates', 'node_modules');
customTemplateName = templateName.slice(packageMarker.length);
} else {
customTemplatesDir = templatesFolder;
customTemplateName = templateName;
}

return path.join(customTemplatesDir, `${customTemplateName}.html`);
}

/**
* Takes a list of templates and grabs their content. It returns simple key/val pair
* of filename => content
Expand All @@ -20,7 +43,8 @@ function getContent(templatesFolder, templatePaths, callback) {
templatePaths,
{},
(acc, templatePath, reduceCallback) => {
const file = path.join(templatesFolder, `${templatePath}.html`);
const file = getCustomPath(templatesFolder, templatePath);

fs.readFile(file, { encoding: 'utf-8' }, (err, content) => {
if (err) {
reduceCallback(err);
Expand Down Expand Up @@ -81,7 +105,7 @@ function getTemplatePaths(templatesFolder, templates, options, callback) {
});

function resolvePartials(templateName, cb2) {
const templatePath = path.join(templatesFolder, `${templateName}.html`);
const templatePath = getCustomPath(templatesFolder, templateName);

fs.readFile(templatePath, { encoding: 'utf-8' }, (err, content) => {
const componentPaths = [];
Expand Down

0 comments on commit 12ce05c

Please sign in to comment.