Skip to content

Commit

Permalink
fix: allow _ and - for tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp authored Sep 14, 2024
1 parent f8ae8fb commit 6ce146a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
26 changes: 25 additions & 1 deletion packages/core/src/lib/tags/calc-tags-for-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ describe('calc tags for module', () => {
).toEqual(['domain:holidays']);
});

it('should support a placeholder with a dash', () => {
const rootDir = '/project' as FsPath;
const moduleDir = '/project/app1/holidays' as FsPath;

expect(
calcTagsForModule(moduleDir, rootDir, {
'<domain>/<sub-domain>': (tags) => [
`domain:${tags['domain']}:${tags['sub-domain']}`,
],
}),
).toEqual(['domain:app1:holidays']);
});

it('should support a placeholder with a dash underscore', () => {
const rootDir = '/project' as FsPath;
const moduleDir = '/project/app1/holidays' as FsPath;

expect(
calcTagsForModule(moduleDir, rootDir, {
'<app-lib>/<_domain>': 'domain:<app-lib>:<_domain>',
}),
).toEqual(['domain:app1:holidays']);
});

it('should support placeholders in both matcher and tag value', () => {
const rootDir = '/project' as FsPath;
const moduleDir = '/project/holidays' as FsPath;
Expand Down Expand Up @@ -145,7 +169,7 @@ describe('calc tags for module', () => {
'<subdomain>': ['type:<type>', 'subdomain:<subdomain>'],
}),
).toThrowUserError(
new InvalidPlaceholderError('type', '/project/feat-bookings'),
new InvalidPlaceholderError('<type>', '/project/feat-bookings'),
);
});

Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/lib/tags/calc-tags-for-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
TagWithoutValueError,
} from '../error/user-error';

const PLACE_HOLDER_REGEX = /<[a-zA-Z-_]+>/g;

export const calcTagsForModule = (
moduleDir: FsPath,
rootDir: FsPath,
Expand Down Expand Up @@ -155,9 +157,9 @@ function replacePlaceholdersInTag(
);
}

const unavailablePlaceholder = replacedTag.match(/<([a-zA-Z]+)>/);
const unavailablePlaceholder = replacedTag.match(PLACE_HOLDER_REGEX);
if (unavailablePlaceholder) {
throw new InvalidPlaceholderError(unavailablePlaceholder[1], fullDir);
throw new InvalidPlaceholderError(unavailablePlaceholder[0], fullDir);
}

return replacedTag;
Expand All @@ -173,7 +175,7 @@ function handlePlaceholderMatching(
placeholderMatch: string[],
placeholders: Record<string, string>,
) {
const placeholderRegex = pathMatcher.replace(/<[a-zA-Z]+>/g, '(.+)');
const placeholderRegex = pathMatcher.replace(PLACE_HOLDER_REGEX, '(.+)');
const pathMatch = currentPath.match(new RegExp(placeholderRegex));
if (!pathMatch) {
return false;
Expand Down Expand Up @@ -222,7 +224,7 @@ function matchSegment(
matches = false;
}
pathFragment = paths.slice(0, pathFragmentSpan).join('/');
const placeholderMatch = (segmentMatcher.match(/<[a-zA-Z]+>/g) ?? []).map(
const placeholderMatch = (segmentMatcher.match(PLACE_HOLDER_REGEX) ?? []).map(
(str) => str.slice(1, str.length - 1),
);
if (placeholderMatch.length) {
Expand Down

0 comments on commit 6ce146a

Please sign in to comment.