Skip to content

Commit

Permalink
refactor: extract missing attr on env object error into helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Iryna Makar committed Aug 2, 2021
1 parent 72fe138 commit 1b4ca80
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 106 deletions.
8 changes: 4 additions & 4 deletions src/analyse/adapt-dependencies/adapt-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
10 changes: 5 additions & 5 deletions src/analyse/add-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 8 additions & 9 deletions src/analyse/add-messages-to-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

const cloneCommitMessages = clone(env.commitMessages);
const cloneCommitMessages = clone(env.commitMessages!);

cloneCommitMessages.forEach((commitMessage) => {
const affectedPackages = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
10 changes: 5 additions & 5 deletions src/analyse/create-dependency-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
17 changes: 8 additions & 9 deletions src/analyse/determine-direct-increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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<string, Message['level']> = {};

log('analyse registered commitMessages');
env.commitMessages.forEach((commitMessage) => {
env.commitMessages!.forEach((commitMessage) => {
commitMessage.affectedPackages?.forEach((affectedPackageName) => {
const affectedPackage = clonePackages[affectedPackageName];

Expand Down
10 changes: 5 additions & 5 deletions src/analyse/determine-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'];

Expand Down
11 changes: 5 additions & 6 deletions src/analyse/prepare-changelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/analyse/spec/add-dependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/analyse/spec/add-messages-to-packages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/analyse/spec/create-dependency-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 5 additions & 10 deletions src/analyse/spec/determine-direct-increment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/analyse/spec/prepare-changelogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
3 changes: 1 addition & 2 deletions src/change/spec/write-main-changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
3 changes: 1 addition & 2 deletions src/change/spec/write-package-changelogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/change/write-main-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit 1b4ca80

Please sign in to comment.