From 1b4ca800b2a9dff3ce7d3a9409d7c41c60cd33ae Mon Sep 17 00:00:00 2001 From: Iryna Makar Date: Mon, 2 Aug 2021 13:32:07 +0200 Subject: [PATCH] refactor: extract missing attr on env object error into helper method --- .../adapt-dependencies/adapt-dependencies.ts | 8 ++++---- src/analyse/add-dependencies.ts | 10 +++++----- src/analyse/add-messages-to-packages.ts | 17 ++++++++--------- .../add-package-names-to-messages.ts | 10 +++++----- .../spec/add-package-names-to-messages.spec.ts | 2 +- src/analyse/create-dependency-tree.ts | 10 +++++----- .../create-package-json-content.ts | 13 ++++++------- .../spec/create-package-json-content.spec.ts | 2 +- src/analyse/determine-direct-increment.ts | 17 ++++++++--------- src/analyse/determine-version.ts | 10 +++++----- src/analyse/prepare-changelogs.ts | 11 +++++------ src/analyse/spec/add-dependencies.spec.ts | 3 +-- .../spec/add-messages-to-packages.spec.ts | 4 ++-- src/analyse/spec/create-dependency-tree.spec.ts | 2 +- .../spec/determine-direct-increment.spec.ts | 15 +++++---------- src/analyse/spec/prepare-changelogs.spec.ts | 3 +-- src/change/spec/write-main-changelog.spec.ts | 3 +-- .../spec/write-package-changelogs.spec.ts | 3 +-- src/change/write-main-changelog.ts | 9 ++++----- src/change/write-package-changelogs.ts | 8 ++++---- src/change/write-package-jsons-to-npm.ts | 10 +++++----- src/helpers/validation-errors.ts | 9 +++++++++ src/persist/commit-and-tag-packages.ts | 8 ++++---- src/persist/publish.ts | 9 ++++----- src/persist/write-package-jsons-for-git.ts | 10 +++++----- 25 files changed, 100 insertions(+), 106 deletions(-) create mode 100644 src/helpers/validation-errors.ts diff --git a/src/analyse/adapt-dependencies/adapt-dependencies.ts b/src/analyse/adapt-dependencies/adapt-dependencies.ts index b3e0f35..752750e 100644 --- a/src/analyse/adapt-dependencies/adapt-dependencies.ts +++ b/src/analyse/adapt-dependencies/adapt-dependencies.ts @@ -9,18 +9,18 @@ import type { Module, PackageAfterDetermineVersion } from '../../types'; import { logger } from '../../helpers/logger'; import { getReleasablePackages } from '../../helpers/get-releasable-packages'; +import { missingEnvAttrError } from '../../helpers/validation-errors'; +const topic = '[analyse] adapt dependencies'; const { error, log } = logger('[analyse] adapt dependencies'); export const adaptDependencies: Module = (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } log('analyse affected packages'); - const clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); const releasablePackages = getReleasablePackages(clone(clonePackages)); log(`${Object.keys(releasablePackages).length} packages will be released`); diff --git a/src/analyse/add-dependencies.ts b/src/analyse/add-dependencies.ts index 58c5e7c..b5bd7a3 100644 --- a/src/analyse/add-dependencies.ts +++ b/src/analyse/add-dependencies.ts @@ -8,19 +8,19 @@ import { } from '../types'; import { logger } from '../helpers/logger'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[analyse] add dependencies'); +const topic = '[analyse] add dependencies'; +const { log } = logger(topic); export const addDependencies: Module = (env) => { log("Populating packages' dependencies"); if (!env.packages) { - const errorMessage = '"packages" attribute not found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } - const envPackages = clone(env.packages); + const envPackages = clone(env.packages!); const packagesNames = Object.keys(envPackages); diff --git a/src/analyse/add-messages-to-packages.ts b/src/analyse/add-messages-to-packages.ts index 4694d5d..f855e24 100644 --- a/src/analyse/add-messages-to-packages.ts +++ b/src/analyse/add-messages-to-packages.ts @@ -3,25 +3,24 @@ import { clone } from 'ramda'; import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[analyse] add messages to packages'); +const topic = '[analyse] add messages to packages'; +const { error, log } = logger(topic); export const addMessagesToPackages: Module = (env) => { if (!env.commitMessages) { - const errorMessage = 'missing "commitMessages" on env object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('commitMessages', topic); } + if (!env.packages) { - const errorMessage = 'missing "packages" on env object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } log('Register messages to packages'); - const clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); - env.commitMessages.forEach((commitMessage) => { + env.commitMessages!.forEach((commitMessage) => { commitMessage.affectedPackages?.forEach((affectedPackage) => { if (!clonePackages[affectedPackage]) { const errorMessage = `"${affectedPackage}" is not a valid (registered) package`; diff --git a/src/analyse/add-package-names-to-messages/add-package-names-to-messages.ts b/src/analyse/add-package-names-to-messages/add-package-names-to-messages.ts index 7d617e0..7c196d3 100644 --- a/src/analyse/add-package-names-to-messages/add-package-names-to-messages.ts +++ b/src/analyse/add-package-names-to-messages/add-package-names-to-messages.ts @@ -5,20 +5,20 @@ import type { Module } from '../../types'; import { logger } from '../../helpers/logger'; import { findPackageName } from './find-package-name'; +import { missingEnvAttrError } from '../../helpers/validation-errors'; -const { error, log } = logger('[analyse] add package names to messages'); +const topic = '[analyse] add package names to messages'; +const { log } = logger(topic); export const addPackageNamesToMessages: Module = (env) => { if (!env.commitMessages) { - const errorMessage = 'missing "commitMessages" on env object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('commitMessages', topic); } log('Analyse affected packages per commit'); const userAffectedPackages = new Set(); - const cloneCommitMessages = clone(env.commitMessages); + const cloneCommitMessages = clone(env.commitMessages!); cloneCommitMessages.forEach((commitMessage) => { const affectedPackages = new Set(); diff --git a/src/analyse/add-package-names-to-messages/spec/add-package-names-to-messages.spec.ts b/src/analyse/add-package-names-to-messages/spec/add-package-names-to-messages.spec.ts index 381a377..3d4933a 100644 --- a/src/analyse/add-package-names-to-messages/spec/add-package-names-to-messages.spec.ts +++ b/src/analyse/add-package-names-to-messages/spec/add-package-names-to-messages.spec.ts @@ -42,7 +42,7 @@ describe('addPackageNamesToMessages Module', () => { }); it('throws an exception if env config object has no "commitMessages"', () => { - const expectedErrorMessage = 'missing "commitMessages" on env object'; + const expectedErrorMessage = 'missing "commitMessages" on env object.'; expect(() => addPackageNamesToMessages(envWithConfig)).toThrow( expectedErrorMessage ); diff --git a/src/analyse/create-dependency-tree.ts b/src/analyse/create-dependency-tree.ts index 68fc75a..003a4f7 100644 --- a/src/analyse/create-dependency-tree.ts +++ b/src/analyse/create-dependency-tree.ts @@ -4,17 +4,17 @@ import { yellow } from 'chalk'; import { Module } from '../types'; import { logger } from '../helpers/logger'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[analyse] create dependency tree'); +const topic = '[analyse] create dependency tree'; +const { log } = logger('[analyse] create dependency tree'); export const createDependencyTree: Module = (env) => { if (!env.packages) { - const errorMessage = 'no packages found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } - const packagesClone = clone(env.packages); + const packagesClone = clone(env.packages!); const counter = { default: 0, devDependencies: 0, diff --git a/src/analyse/create-package-json-content/create-package-json-content.ts b/src/analyse/create-package-json-content/create-package-json-content.ts index 3df13af..b78ff6b 100644 --- a/src/analyse/create-package-json-content/create-package-json-content.ts +++ b/src/analyse/create-package-json-content/create-package-json-content.ts @@ -10,21 +10,20 @@ import { logger } from '../../helpers/logger'; import { getReleasablePackages } from '../../helpers/get-releasable-packages'; import { getPackageJson } from './get-package-json'; +import { missingEnvAttrError } from '../../helpers/validation-errors'; -const { error, log } = logger('[analyse] create package json content'); +const topic = '[analyse] create package json content'; +const { log } = logger(topic); export const createPackageJsonContent: Module = (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } log('analyse packageJson Content'); - const releasablePackages = getReleasablePackages(env.packages); - - const clonePackages = clone(env.packages); + const releasablePackages = getReleasablePackages(env.packages!); + const clonePackages = clone(env.packages!); releasablePackages.forEach((packageName) => { log(`generating git & npm package.json files for ${packageName}`); diff --git a/src/analyse/create-package-json-content/spec/create-package-json-content.spec.ts b/src/analyse/create-package-json-content/spec/create-package-json-content.spec.ts index a257f21..f11802e 100644 --- a/src/analyse/create-package-json-content/spec/create-package-json-content.spec.ts +++ b/src/analyse/create-package-json-content/spec/create-package-json-content.spec.ts @@ -67,7 +67,7 @@ describe('createPackageJsonContent Module', () => { }); it('throws an error when "packages" is missing on Env config object', () => { - const expectedErrorMessage = 'missing "packages" on env object'; + const expectedErrorMessage = 'missing "packages" on env object.'; expect(() => createPackageJsonContent(envWithConfig)).toThrow( expectedErrorMessage ); diff --git a/src/analyse/determine-direct-increment.ts b/src/analyse/determine-direct-increment.ts index c5d7e54..6289101 100644 --- a/src/analyse/determine-direct-increment.ts +++ b/src/analyse/determine-direct-increment.ts @@ -4,8 +4,10 @@ import { green, red, yellow, white } from 'chalk'; import type { Package, Message, Module } from '../types'; import { logger } from '../helpers/logger'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[analyse] determine direct increment'); +const topic = '[analyse] determine direct increment'; +const { log } = logger(topic); const mapLevelToIncrementLevel: Record< Message['level'], @@ -28,21 +30,18 @@ const mapLevelToColour: Record< export const determineDirectIncrement: Module = (env) => { if (!env.commitMessages) { - const errorMessage = '"commitMessage" not present on env config object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('commitMessages', topic); } + if (!env.packages) { - const errorMessage = '"packages" not present on env config object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } - const clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); const packagesToRelease: Record = {}; log('analyse registered commitMessages'); - env.commitMessages.forEach((commitMessage) => { + env.commitMessages!.forEach((commitMessage) => { commitMessage.affectedPackages?.forEach((affectedPackageName) => { const affectedPackage = clonePackages[affectedPackageName]; diff --git a/src/analyse/determine-version.ts b/src/analyse/determine-version.ts index eb5df21..befab27 100644 --- a/src/analyse/determine-version.ts +++ b/src/analyse/determine-version.ts @@ -5,6 +5,7 @@ import { green, red, yellow, white } from 'chalk'; import { logger } from '../helpers/logger'; import type { Module, PackageAfterDetermineVersion } from '../types'; +import { missingEnvAttrError } from '../helpers/validation-errors'; const mapLevelToColour: Record< PackageAfterDetermineVersion['determinedIncrementLevel'], @@ -19,18 +20,17 @@ const mapLevelToColour: Record< 2: ['major', red], }; -const { error, log } = logger('[analyse] determine Version'); +const topic = '[analyse] determine Version'; +const { error, log } = logger(topic); export const determineVersion: Module = (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } log('analyse packages'); - const clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); const incrementLevels: semver.ReleaseType[] = ['patch', 'minor', 'major']; diff --git a/src/analyse/prepare-changelogs.ts b/src/analyse/prepare-changelogs.ts index c889d53..e35506c 100644 --- a/src/analyse/prepare-changelogs.ts +++ b/src/analyse/prepare-changelogs.ts @@ -16,17 +16,16 @@ import type { import { logger } from '../helpers/logger'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[analyse] prepare changelogs'); +const topic = '[analyse] prepare changelogs'; +const { error, log } = logger(topic); export const prepareChangelogs: Module = (env) => { if (!env.packages) { - const errorMessage = '"packages" attribute not found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } - - const clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); const releasablePackages = getReleasablePackages(clonePackages); const changelogDate = new Date().toISOString(); diff --git a/src/analyse/spec/add-dependencies.spec.ts b/src/analyse/spec/add-dependencies.spec.ts index a1ce500..1d17ebd 100644 --- a/src/analyse/spec/add-dependencies.spec.ts +++ b/src/analyse/spec/add-dependencies.spec.ts @@ -55,8 +55,7 @@ describe('addDependencies Module', () => { }); it("Throws an exception if 'env.packages' is empty", () => { - const expectedErrorMessage = - '"packages" attribute not found on env config object'; + const expectedErrorMessage = 'missing "packages" on env object.'; expect(() => addDependencies(envWithConfig)).toThrow(expectedErrorMessage); diff --git a/src/analyse/spec/add-messages-to-packages.spec.ts b/src/analyse/spec/add-messages-to-packages.spec.ts index a66ef9d..d8d5b01 100644 --- a/src/analyse/spec/add-messages-to-packages.spec.ts +++ b/src/analyse/spec/add-messages-to-packages.spec.ts @@ -70,7 +70,7 @@ describe('addMessagesToPackages module', () => { }); it('throws an error when "commitMessages" is missing on Env config object', () => { - const expectedErrorMessage = 'missing "commitMessages" on env object'; + const expectedErrorMessage = 'missing "commitMessages" on env object.'; expect(() => addMessagesToPackages(envWithoutCommitMessages)).toThrow( expectedErrorMessage ); @@ -79,7 +79,7 @@ describe('addMessagesToPackages module', () => { }); it('throws an error when "packages" is missing on Env config object', () => { - const expectedErrorMessage = 'missing "packages" on env object'; + const expectedErrorMessage = 'missing "packages" on env object.'; expect(() => addMessagesToPackages(envWithoutPackages)).toThrow( expectedErrorMessage ); diff --git a/src/analyse/spec/create-dependency-tree.spec.ts b/src/analyse/spec/create-dependency-tree.spec.ts index e8921f6..a8e6971 100644 --- a/src/analyse/spec/create-dependency-tree.spec.ts +++ b/src/analyse/spec/create-dependency-tree.spec.ts @@ -46,7 +46,7 @@ describe('[analyse] createDependencyTree module', () => { }); it('throws an exception when "env.packages" is not defined', () => { - const expectedError = 'no packages found on env config object'; + const expectedError = 'missing "packages" on env object.'; expect(() => createDependencyTree(envWithConfig)).toThrow(expectedError); expect(mockError).toHaveBeenCalledTimes(1); expect(mockError).toHaveBeenCalledWith(expectedError); diff --git a/src/analyse/spec/determine-direct-increment.spec.ts b/src/analyse/spec/determine-direct-increment.spec.ts index 4556cf5..2f4b0c5 100644 --- a/src/analyse/spec/determine-direct-increment.spec.ts +++ b/src/analyse/spec/determine-direct-increment.spec.ts @@ -18,15 +18,10 @@ jest.doMock('chalk', () => ({ })); // mock logger -const mockError = jest.fn(); const mockLog = jest.fn(); -const mockLogger = jest.fn(() => ({ - error: mockError, - log: mockLog, -})); -jest.doMock('../../helpers/logger', () => ({ - logger: mockLogger, -})); +const mockError = jest.fn(); +const mockLogger = jest.fn(() => ({ log: mockLog, error: mockError })); +jest.doMock('../../helpers/logger', () => ({ logger: mockLogger })); // mock Packages const mockPackageBuilder = (id: number): Package => ({ @@ -79,7 +74,7 @@ describe('determineDirectIncrement Module', () => { }); it('throws an error if "commitMessage" is not present on env Config Object', () => { - const expectedError = '"commitMessage" not present on env config object.'; + const expectedError = 'missing "commitMessages" on env object.'; const mockEnv: Env = { ...envWithConfig }; expect(() => determineDirectIncrement(mockEnv)).toThrow(expectedError); @@ -89,7 +84,7 @@ describe('determineDirectIncrement Module', () => { }); it('throws an error if "packages" is not present on env Config Object', () => { - const expectedError = '"packages" not present on env config object.'; + const expectedError = 'missing "packages" on env object.'; const mockEnv: Env = { ...envWithConfig, commitMessages: [] }; expect(() => determineDirectIncrement(mockEnv)).toThrow(expectedError); diff --git a/src/analyse/spec/prepare-changelogs.spec.ts b/src/analyse/spec/prepare-changelogs.spec.ts index 805bec1..e999da2 100644 --- a/src/analyse/spec/prepare-changelogs.spec.ts +++ b/src/analyse/spec/prepare-changelogs.spec.ts @@ -122,8 +122,7 @@ describe('prepareChangelogs Module', () => { }); it('throws an error when "packages" is missing on Env config object', () => { - const expectedErrorMessage = - '"packages" attribute not found on env config object'; + const expectedErrorMessage = 'missing "packages" on env object.'; expect(() => prepareChangelogs(envWithConfig)).toThrow( expectedErrorMessage ); diff --git a/src/change/spec/write-main-changelog.spec.ts b/src/change/spec/write-main-changelog.spec.ts index 7cb2293..9f5b67a 100644 --- a/src/change/spec/write-main-changelog.spec.ts +++ b/src/change/spec/write-main-changelog.spec.ts @@ -61,8 +61,7 @@ describe('writeMainChangelog Module', () => { }); it('throws an error when "changelogPath" is missing on Env config object', () => { - const expectedErrorMessage = - '"changelogPath" attribute not found on env config object'; + const expectedErrorMessage = 'missing "changelogPath" on env object.'; const invalidEnvConfig = clone(envWithConfig); invalidEnvConfig.config!.changelogPath = ''; diff --git a/src/change/spec/write-package-changelogs.spec.ts b/src/change/spec/write-package-changelogs.spec.ts index 73ff9f4..9ce6690 100644 --- a/src/change/spec/write-package-changelogs.spec.ts +++ b/src/change/spec/write-package-changelogs.spec.ts @@ -62,8 +62,7 @@ describe('writePackageChangelogs Module', () => { }); it('throws an error when "packages" is missing on Env config object', () => { - const expectedErrorMessage = - '"packages" attribute not found on env config object'; + const expectedErrorMessage = 'missing "packages" on env object.'; expect(() => writePackageChangelogs(envWithConfig)).toThrow( expectedErrorMessage diff --git a/src/change/write-main-changelog.ts b/src/change/write-main-changelog.ts index 2182244..29e779b 100644 --- a/src/change/write-main-changelog.ts +++ b/src/change/write-main-changelog.ts @@ -5,18 +5,17 @@ import { existsSync, mkdirSync } from 'fs'; import type { Module } from '../types'; import { logger } from '../helpers/logger'; import { writeFile } from '../helpers/write-file'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[change] write main changelog'); +const topic = '[change] write main changelog'; +const { log } = logger(topic); export const writeMainChangelog: Module = (env) => { const mainChangeLogDir = env.config!.changelogPath; const mainChangeLogFile = join(env.config!.changelogPath, 'changelog.json'); if (!mainChangeLogDir) { - const errorMessage = - '"changelogPath" attribute not found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('changelogPath', topic); } if (!existsSync(mainChangeLogDir)) { diff --git a/src/change/write-package-changelogs.ts b/src/change/write-package-changelogs.ts index 15c19c1..575704e 100644 --- a/src/change/write-package-changelogs.ts +++ b/src/change/write-package-changelogs.ts @@ -9,14 +9,14 @@ import { getReleasablePackages } from '../helpers/get-releasable-packages'; import { writeFile } from '../helpers/write-file'; import { PackageAfterPrepareChangelogs } from '../types'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { log, error } = logger('[change] write package changelogs'); +const section = '[change] write package changelogs'; +const { log } = logger(section); export const writePackageChangelogs: Module = (env) => { if (!env.packages) { - const errorMessage = '"packages" attribute not found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', section); } const releasablePackages = getReleasablePackages(clone(env.packages!)); diff --git a/src/change/write-package-jsons-to-npm.ts b/src/change/write-package-jsons-to-npm.ts index 91eb541..441a794 100644 --- a/src/change/write-package-jsons-to-npm.ts +++ b/src/change/write-package-jsons-to-npm.ts @@ -3,19 +3,19 @@ import type { Module } from '../types'; import { logger } from '../helpers/logger'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; import { writeFile } from '../helpers/write-file'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { log, error } = logger('[change] write packageJsons (NPM)'); +const section = '[change] write packageJsons (NPM)'; +const { log, error } = logger(section); export const writePackageJsonsToNpm: Module = (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', section); } log('Analysing releasable packages...'); - const releasablePackages = getReleasablePackages(env.packages); + const releasablePackages = getReleasablePackages(env.packages!); releasablePackages.forEach((packageName) => { const currentPackage = env.packages![packageName]; diff --git a/src/helpers/validation-errors.ts b/src/helpers/validation-errors.ts new file mode 100644 index 0000000..843ee98 --- /dev/null +++ b/src/helpers/validation-errors.ts @@ -0,0 +1,9 @@ +import { logger } from './logger'; + +export const missingEnvAttrError = (attributeName: string, topic: string) => { + const { error } = logger(topic); + const errorMessage = `missing "${attributeName}" on env object.`; + + error(errorMessage); + throw new Error(errorMessage); +}; diff --git a/src/persist/commit-and-tag-packages.ts b/src/persist/commit-and-tag-packages.ts index a6c4cc6..92ec92b 100644 --- a/src/persist/commit-and-tag-packages.ts +++ b/src/persist/commit-and-tag-packages.ts @@ -4,15 +4,15 @@ import { logger } from '../helpers/logger'; import simpleGit from 'simple-git'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { log, error } = logger('[persist] commit and tag packages'); +const topic = '[persist] commit and tag packages'; +const { log, error } = logger(topic); const git = simpleGit(); const commitAndTagPackages: Module = async (env: Env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } const prepareCommit = (releasablePackages: string[]): string[] => { diff --git a/src/persist/publish.ts b/src/persist/publish.ts index 9e057af..9f7ab06 100644 --- a/src/persist/publish.ts +++ b/src/persist/publish.ts @@ -5,9 +5,10 @@ import type { Module, PackageAfterCreatePackageJsonContent } from '../types'; import { logger } from '../helpers/logger'; import { command } from '../helpers/command'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; +import { missingEnvAttrError } from '../helpers/validation-errors'; const topic = 'publish to NPM'; -const { error, log } = logger(topic); +const { log } = logger(topic); // now that we have all relevant files in place locally // we can publish each of the packages to npm. @@ -17,12 +18,10 @@ const { error, log } = logger(topic); // But it's worth printing a success message for each released package. export const publish: Module = async (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } - const releasablePackages = getReleasablePackages(env.packages); + const releasablePackages = getReleasablePackages(env.packages!); log(white(`Releasing ${releasablePackages.length} packages`)); diff --git a/src/persist/write-package-jsons-for-git.ts b/src/persist/write-package-jsons-for-git.ts index cef606c..8db658b 100644 --- a/src/persist/write-package-jsons-for-git.ts +++ b/src/persist/write-package-jsons-for-git.ts @@ -3,19 +3,19 @@ import type { Module } from '../types'; import { logger } from '../helpers/logger'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; import { writeFile } from '../helpers/write-file'; +import { missingEnvAttrError } from '../helpers/validation-errors'; -const { error, log } = logger('[persist] package.json files (git)'); +const topic = '[persist] package.json files (git)'; +const { error, log } = logger(topic); export const writePackageJsonsForGit: Module = (env) => { if (!env.packages) { - const errorMessage = 'missing "packages" on env object.'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('packages', topic); } log('Preparing packages to commit to Git'); - const releasablePackages = getReleasablePackages(env.packages); + const releasablePackages = getReleasablePackages(env.packages!); releasablePackages.forEach((packageName) => { const currentPackage = env.packages![packageName];