Skip to content

Commit

Permalink
DXCDT-689:nomalizedYAMLPath util added to handle branding template path
Browse files Browse the repository at this point in the history
  • Loading branch information
kushalshit27 committed Aug 13, 2024
1 parent 7399dde commit 4d91b7b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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,7 +33,7 @@ async function parse(context: YAMLContext): Promise<ParsedBranding> {

const parsedTemplates: BrandingTemplate[] = templates.map(
(templateDefinition: BrandingTemplate): BrandingTemplate => {
const normalizedPathSplit = path.normalize(templateDefinition.body).split(path.sep);
const normalizedPathSplit = nomalizedYAMLPath(templateDefinition.body);
const markupFile = path.join(context.basePath, ...normalizedPathSplit);
return {
template: templateDefinition.template,
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;
}

0 comments on commit 4d91b7b

Please sign in to comment.