Skip to content

Commit

Permalink
New: Add option to ignore skip of prerelease (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
doteric authored Feb 23, 2023
1 parent 745f84c commit 8e26fed
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 17 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Test

on:
pull_request:
push:
branches:
- '**'

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ yarn add -D semantic-release-major-tag
`customTags` (optional, defaults to `[v${major}]`) - An array of the tags format to be created.
Use `${major}`, `${minor}` or `${patch}` as strings to specify where you would like the specific version number to be present.

`includePrerelease` (optional, defaults to `false`) - Set to `true` if you would like to include prereleases.

Example:

```
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/preparePluginConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const pluginConfigDefaults = {
includePrerelease: false,
customTags: ['v${major}'],
};

const preparePluginConfig = (pluginConfig: unknown) => ({
...pluginConfigDefaults,
...(pluginConfig ? pluginConfig : {}),
});

export type PluginConfigType = ReturnType<typeof preparePluginConfig>;

export default preparePluginConfig;
4 changes: 2 additions & 2 deletions src/steps/prepareTags.ts → src/helpers/prepareTags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PluginConfigType } from './types';
import type { PluginConfigType } from './preparePluginConfig';

const validateCustomTagsSetting = (
customTags: unknown
Expand All @@ -24,7 +24,7 @@ const validateCustomTagsSetting = (
};

const prepareTags = (version: string, pluginConfig: PluginConfigType) => {
const customTags = validateCustomTagsSetting(pluginConfig?.customTags);
const customTags = validateCustomTagsSetting(pluginConfig.customTags);
const tagsFormat = customTags || ['v${major}'];

const [major, minor, patch] = version.split('.');
Expand Down
75 changes: 75 additions & 0 deletions src/helpers/skipForPrerelease.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import preparePluginConfig from './preparePluginConfig';
import skipForPrerelease from './skipForPrerelease';

describe('skipForPrerelease', () => {
it('returns correctly when ignore prerelease skip config set', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
prerelease: 'beta',
},
preparePluginConfig({ includePrerelease: true })
)
).toBe(false);
});

it('returns correctly when branch is not a prerelease', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
},
preparePluginConfig({})
)
).toBe(false);
});

it('returns correctly when prerelease set to empty string', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
prerelease: '',
},
preparePluginConfig({})
)
).toBe(false);
});

it('returns correctly when prerelease set to string', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
prerelease: 'beta',
},
preparePluginConfig({})
)
).toBe(true);
});

it('returns correctly when prerelease set to true boolean', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
prerelease: true,
},
preparePluginConfig({})
)
).toBe(true);
});

it('returns correctly when prerelease set to false boolean', () => {
expect(
skipForPrerelease(
{
name: 'example-branch',
prerelease: false,
},
preparePluginConfig({})
)
).toBe(false);
});
});
16 changes: 16 additions & 0 deletions src/helpers/skipForPrerelease.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { BranchObject } from 'semantic-release';

import type { PluginConfigType } from './preparePluginConfig';

const isPrerelease = (branchObject: BranchObject) => !!branchObject?.prerelease;

const skipForPrerelease = (
branch: BranchObject,
pluginConfig: PluginConfigType
): boolean => {
if (pluginConfig.includePrerelease === true) return false;

return isPrerelease(branch);
};

export default skipForPrerelease;
6 changes: 5 additions & 1 deletion src/steps/success.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ describe('success', () => {
},
logger: { info: jest.fn(), error: jest.fn() },
} as unknown as Context;
success(
const result = success(
{ customTags: ['v${major}-test', 'v${major}.${minor}'] },
contextMock
);
expect(result).toBeUndefined();
expect(execSync).not.toBeCalled();
expect(contextMock.logger.info).toBeCalledWith(
'Not publishing any tags on a prerelease!'
);
});
});
25 changes: 14 additions & 11 deletions src/steps/success.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import type { BranchObject, Context } from 'semantic-release';
import type { Context } from 'semantic-release';
import { execSync } from 'node:child_process';

import prepareTags from './prepareTags';
import type { PluginConfigType } from './types';
import prepareTags from '../helpers/prepareTags';
import skipForPrerelease from '../helpers/skipForPrerelease';
import preparePluginConfig from '../helpers/preparePluginConfig';

const isPrerelease = (branchObject: BranchObject) =>
branchObject && !!branchObject.prerelease;

const success = (pluginConfig: PluginConfigType, context: Context) => {
const success = (pluginConfigBare: unknown, context: Context) => {
const { options, nextRelease, logger, branch } = context;
if (isPrerelease(branch)) {
logger.info(`Not publishing any tags on a prerelease!`);

// Prepare config with defaults
const pluginConfig = preparePluginConfig(pluginConfigBare);

if (skipForPrerelease(branch, pluginConfig)) {
logger.info('Not publishing any tags on a prerelease!');
return;
}

if (!options) {
logger.error(`Missing options from context!`);
logger.error('Missing options from context!');
return;
}
if (!nextRelease) {
logger.error(`Missing nextRelease from context!`);
logger.error('Missing nextRelease from context!');
return;
}
const { repositoryUrl } = options;
Expand Down
1 change: 0 additions & 1 deletion src/steps/types.ts

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"target": "es2019",
"moduleResolution": "node",
"outDir": "dist",
"baseUrl": ".",
"lib": ["es2021"],
"strict": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 8e26fed

Please sign in to comment.