From 8e26fed596671d6c5f4b3c031988e211da814f7d Mon Sep 17 00:00:00 2001 From: Eric <33552134+doteric@users.noreply.github.com> Date: Thu, 23 Feb 2023 18:57:03 +0100 Subject: [PATCH] New: Add option to ignore skip of prerelease (#7) --- .github/workflows/test.yml | 1 - README.md | 2 + src/helpers/preparePluginConfig.ts | 13 +++++ src/{steps => helpers}/prepareTags.ts | 4 +- src/helpers/skipForPrerelease.test.ts | 75 +++++++++++++++++++++++++++ src/helpers/skipForPrerelease.ts | 16 ++++++ src/steps/success.test.ts | 6 ++- src/steps/success.ts | 25 +++++---- src/steps/types.ts | 1 - tsconfig.json | 1 - 10 files changed, 127 insertions(+), 17 deletions(-) create mode 100644 src/helpers/preparePluginConfig.ts rename src/{steps => helpers}/prepareTags.ts (86%) create mode 100644 src/helpers/skipForPrerelease.test.ts create mode 100644 src/helpers/skipForPrerelease.ts delete mode 100644 src/steps/types.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2a83386..2a40875 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,6 @@ name: Test on: pull_request: - push: branches: - '**' diff --git a/README.md b/README.md index 3a32ef7..b2d98c3 100644 --- a/README.md +++ b/README.md @@ -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: ``` diff --git a/src/helpers/preparePluginConfig.ts b/src/helpers/preparePluginConfig.ts new file mode 100644 index 0000000..aa070f0 --- /dev/null +++ b/src/helpers/preparePluginConfig.ts @@ -0,0 +1,13 @@ +const pluginConfigDefaults = { + includePrerelease: false, + customTags: ['v${major}'], +}; + +const preparePluginConfig = (pluginConfig: unknown) => ({ + ...pluginConfigDefaults, + ...(pluginConfig ? pluginConfig : {}), +}); + +export type PluginConfigType = ReturnType; + +export default preparePluginConfig; diff --git a/src/steps/prepareTags.ts b/src/helpers/prepareTags.ts similarity index 86% rename from src/steps/prepareTags.ts rename to src/helpers/prepareTags.ts index ed05d60..bbd82a7 100644 --- a/src/steps/prepareTags.ts +++ b/src/helpers/prepareTags.ts @@ -1,4 +1,4 @@ -import type { PluginConfigType } from './types'; +import type { PluginConfigType } from './preparePluginConfig'; const validateCustomTagsSetting = ( customTags: unknown @@ -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('.'); diff --git a/src/helpers/skipForPrerelease.test.ts b/src/helpers/skipForPrerelease.test.ts new file mode 100644 index 0000000..400862e --- /dev/null +++ b/src/helpers/skipForPrerelease.test.ts @@ -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); + }); +}); diff --git a/src/helpers/skipForPrerelease.ts b/src/helpers/skipForPrerelease.ts new file mode 100644 index 0000000..c02bf6a --- /dev/null +++ b/src/helpers/skipForPrerelease.ts @@ -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; diff --git a/src/steps/success.test.ts b/src/steps/success.test.ts index e56158e..49ad690 100644 --- a/src/steps/success.test.ts +++ b/src/steps/success.test.ts @@ -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!' + ); }); }); diff --git a/src/steps/success.ts b/src/steps/success.ts index 093f27e..55e5252 100644 --- a/src/steps/success.ts +++ b/src/steps/success.ts @@ -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; diff --git a/src/steps/types.ts b/src/steps/types.ts deleted file mode 100644 index 6ea2380..0000000 --- a/src/steps/types.ts +++ /dev/null @@ -1 +0,0 @@ -export type PluginConfigType = Record | undefined; diff --git a/tsconfig.json b/tsconfig.json index 30ca3db..c88c6b3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,6 @@ "target": "es2019", "moduleResolution": "node", "outDir": "dist", - "baseUrl": ".", "lib": ["es2021"], "strict": true, "resolveJsonModule": true,