Skip to content

Commit

Permalink
feat(semver): add tag template string support
Browse files Browse the repository at this point in the history
  • Loading branch information
GethsLeader authored and edbzn committed Aug 8, 2021
1 parent 4bc45e3 commit 91dec48
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/semver/src/executors/version/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('@jscutlery/semver:version', () => {
});

it('should run standard-version with a custom tag', async () => {
const { success } = await version({...options, versionTagPrefix: 'custom-tag-prefix/a-' }, context);
const { success } = await version({...options, versionTagPrefix: 'custom-tag-prefix/${target}-' }, context);

expect(success).toBe(true);
expect(standardVersion).toBeCalledWith(
Expand Down
4 changes: 3 additions & 1 deletion packages/semver/src/executors/version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { VersionBuilderSchema } from './schema';
import { tryPushToGitRemote } from './utils/git';
import { tryBump } from './utils/try-bump';
import { getProjectRoot } from './utils/workspace';
import { resolveTagTemplate } from './utils/tag-template';
import { CommonVersionOptions, versionProject, versionWorkspace } from './version';

export default function version(
Expand All @@ -27,7 +28,8 @@ export default function version(
): Promise<{ success: boolean }> {
const workspaceRoot = context.root;
const preset = 'angular';
const tagPrefix = versionTagPrefix ? versionTagPrefix
const tagPrefix = versionTagPrefix ? resolveTagTemplate(versionTagPrefix,
{ target: context.projectName, projectName: context.projectName })
: (syncVersions ? 'v' : `${context.projectName}-`);

const projectRoot = getProjectRoot(context);
Expand Down
2 changes: 1 addition & 1 deletion packages/semver/src/executors/version/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"type": "string"
},
"versionTagPrefix": {
"description": "Version tag prefix. Defaults are 'v' and '${target}-' for sync and async modes.",
"description": "Version tag prefix. Defaults are 'v' and '${target}-' for sync and independent modes. ${target} will be replaced with context target value for independent mode.",
"type": "string"
}
},
Expand Down
20 changes: 20 additions & 0 deletions packages/semver/src/executors/version/utils/tag-template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolveTagTemplate } from './tag-template';

describe('resolveTagTemplate', () => {
const testContext = { test1: 'xxx', test2: 'yyy' };

it('should do noting to the template string', () => {
expect(resolveTagTemplate('test string that have test1 and ${nothing} in it', testContext))
.toBe('test string that have test1 and ${nothing} in it');
});

it('should replace all ${test1} placeholders', () => {
expect(resolveTagTemplate('test string with ${test1}, when ${test1} repeat itself', testContext))
.toBe('test string with xxx, when xxx repeat itself');
});

it('should replace all ${test1} and ${test2} placeholders', () => {
expect(resolveTagTemplate('test string with ${test1} and ${test2}', testContext))
.toBe('test string with xxx and yyy');
});
});
10 changes: 10 additions & 0 deletions packages/semver/src/executors/version/utils/tag-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface TagTemplateContext {
[key: string]: string;
}

export function resolveTagTemplate(template: string, resolvingContext: TagTemplateContext): string {
return Object.keys(resolvingContext)
.reduce((accumulator, contextParamKey) => accumulator
.replace(new RegExp(`\\$\\{${contextParamKey}}`, 'g'),
resolvingContext[contextParamKey]), template);
}

0 comments on commit 91dec48

Please sign in to comment.