Skip to content
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

Fix branding template body path parsing issue #943

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/context/directory/handlers/branding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs-extra';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
import { dumpJSON, existsMustBeDir, getFiles, isFile, loadJSON } from '../../../utils';
import { dumpJSON, existsMustBeDir, getFiles, isFile, loadJSON, nomalizedYAMLPath } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
Expand Down Expand Up @@ -38,8 +38,10 @@ function parse(context: DirectoryContext): ParsedBranding {
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
});

const normalizedPathArray = nomalizedYAMLPath(definition.body);
definition.body = loadFileAndReplaceKeywords(
path.join(brandingTemplatesFolder, definition.body),
path.join(brandingTemplatesFolder, ...normalizedPathArray),
{
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
Expand Down Expand Up @@ -94,7 +96,7 @@ const dumpBrandingTemplates = ({ filePath, assets }: DirectoryContext): void =>
}

// save the location as relative file.
templateDefinition.body = `.${path.sep}${templateDefinition.template}.html`;
templateDefinition.body = `./${templateDefinition.template}.html`;
dumpJSON(
path.join(brandingTemplatesFolder, `${templateDefinition.template}.json`),
templateDefinition
Expand Down
9 changes: 3 additions & 6 deletions src/context/yaml/handlers/branding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { YAMLHandler } from '.';
import YAMLContext from '..';
import { Asset, ParsedAsset } from '../../../types';
import log from '../../../logger';
import { nomalizedYAMLPath } from '../../../utils';

type BrandingTemplate = {
template: string;
Expand All @@ -32,12 +33,8 @@ async function parse(context: YAMLContext): Promise<ParsedBranding> {

const parsedTemplates: BrandingTemplate[] = templates.map(
(templateDefinition: BrandingTemplate): BrandingTemplate => {
const brandingTemplatesFolder = path.join(
context.basePath,
constants.BRANDING_TEMPLATES_YAML_DIRECTORY
);
const file = `${templateDefinition.template}.html`;
const markupFile = path.join(brandingTemplatesFolder, file);
const normalizedPathArray = nomalizedYAMLPath(templateDefinition.body);
const markupFile = path.join(context.basePath, ...normalizedPathArray);
return {
template: templateDefinition.template,
body: loadFileAndReplaceKeywords(markupFile, {
Expand Down
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,29 @@ export function mapClientID2NameSorted(enabledClients: string[], knownClients: A
...(enabledClients || []).map((clientId) => convertClientIdToName(clientId, knownClients)),
].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
}

export function nomalizedYAMLPath(filePath: string): string[] {
// Trim any leading or trailing whitespace
filePath = filePath.trim();

// Handle empty path cases
if (filePath === '') {
return [];
}

// Normalize the path by replacing backslashes with forward slashes
const normalizedPath = filePath.replace(/\\/g, '/');

// Split the path using the forward slash as the separator
let pathSplit = normalizedPath.split('/');

// Remove empty components resulting from leading or redundant slashes
pathSplit = pathSplit.filter(component => component !== '');

// Remove the first '.' if it's the first component
if (pathSplit.length > 0 && pathSplit[0] === '.') {
pathSplit.shift();
}

return pathSplit;
}
1 change: 0 additions & 1 deletion test/context/yaml/branding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('#YAML context branding templates', () => {

const config = { AUTH0_INPUT_FILE: yamlFile, AUTH0_KEYWORD_REPLACE_MAPPINGS: { foo: 'bar' } };
const context = new Context(config, mockMgmtClient());
context.basePath = baseDir;
await context.loadAssetsFromLocal();
expect(context.assets.branding).to.deep.equal({
colors: {
Expand Down
Loading