diff --git a/src/analyse/adapt-dependencies/adapt-dependencies.ts b/src/analyse/adapt-dependencies/adapt-dependencies.ts index a84d90d..a9c643e 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 836648e..e9ac139 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 @@ -6,20 +6,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 3c01061..a7fdc09 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,19 +10,19 @@ import type { import { logger } from '../../helpers/logger'; 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 clonePackages = clone(env.packages); + const clonePackages = clone(env.packages!); Object.keys(clonePackages).forEach((packageName) => { log(`generating git & npm package.json files for "${white(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 93d8d6b..97508e3 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 @@ -66,7 +66,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 57ace82..75a461b 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 127fe3e..39b1be2 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 691c349..9a08c3d 100644 --- a/src/analyse/prepare-changelogs.ts +++ b/src/analyse/prepare-changelogs.ts @@ -20,17 +20,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 = `${getWeekNumber(new Date()).join('-')}`; 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 ee41823..6e9db28 100644 --- a/src/analyse/spec/prepare-changelogs.spec.ts +++ b/src/analyse/spec/prepare-changelogs.spec.ts @@ -128,8 +128,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 cb5ed33..43d2702 100644 --- a/src/change/spec/write-main-changelog.spec.ts +++ b/src/change/spec/write-main-changelog.spec.ts @@ -69,8 +69,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 9b69788..f67fd00 100644 --- a/src/change/spec/write-package-changelogs.spec.ts +++ b/src/change/spec/write-package-changelogs.spec.ts @@ -12,7 +12,12 @@ import type { // mock logger const mockLog = jest.fn(); const mockError = jest.fn(); -const mockLogger = jest.fn(() => ({ log: mockLog, error: mockError })); +const mockThrowMissingAttrError = jest.fn(); +const mockLogger = jest.fn(() => ({ + log: mockLog, + error: mockError, + throwMissingAttrError: mockThrowMissingAttrError, +})); jest.doMock('../../helpers/logger', () => ({ logger: mockLogger })); jest.mock('fs'); @@ -64,9 +69,8 @@ describe('writePackageChangelogs Module', () => { jest.clearAllMocks(); }); - it('throws an error when "packages" is missing on Env config object', () => { - const expectedErrorMessage = - '"packages" attribute not found on env config object'; + it.only('throws an error when "packages" is missing 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 bffec38..99c7e76 100644 --- a/src/change/write-main-changelog.ts +++ b/src/change/write-main-changelog.ts @@ -1,19 +1,19 @@ -import { writeFileSync, existsSync, mkdirSync } from 'fs'; +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 mainChangeLogPath = env.mainChangelogPath; + const mainChangeLogPath = env.mainChangelogPath!; if (!mainChangeLogDir) { - const errorMessage = - '"changelogPath" attribute not found on env config object'; - error(errorMessage); - throw new Error(errorMessage); + missingEnvAttrError('changelogPath', topic); } if (!existsSync(mainChangeLogDir)) { @@ -23,10 +23,7 @@ export const writeMainChangelog: Module = (env) => { } log('writing main changelog'); + writeFile(mainChangeLogPath, env.changelog!); - writeFileSync( - mainChangeLogPath!, - `${JSON.stringify(env.changelog, null, 2)}\n` - ); return env; }; diff --git a/src/change/write-package-changelogs.ts b/src/change/write-package-changelogs.ts index 9228b82..c465c51 100644 --- a/src/change/write-package-changelogs.ts +++ b/src/change/write-package-changelogs.ts @@ -1,7 +1,5 @@ import { join } from 'path'; -import fs from 'fs'; - import { clone } from 'ramda'; import { white } from 'chalk'; @@ -9,18 +7,18 @@ import type { Module } from '../types'; import { logger } from '../helpers/logger'; import { getReleasablePackages } from '../helpers/get-releasable-packages'; +import { writeFile } from '../helpers/write-file'; import { PackageAfterPrepareChangelogs } from '../types'; -const { error, log } = logger('[change] write package changelogs'); +const section = '[change] write package changelogs'; +const { log, throwMissingAttrError } = 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); + throwMissingAttrError('packages'); } - const releasablePackages = getReleasablePackages(clone(env.packages)); + const releasablePackages = getReleasablePackages(clone(env.packages!)); releasablePackages.forEach((packageName) => { const currentPackage = env.packages![ @@ -31,10 +29,7 @@ export const writePackageChangelogs: Module = (env) => { const changelogJson = currentPackage.changelogs; const changelogFile = join(currentPackage.path, 'changelog.json'); - fs.writeFileSync( - changelogFile, - `${JSON.stringify(changelogJson, null, 2)}\n` - ); + writeFile(changelogFile, changelogJson); }); return env; }; diff --git a/src/change/write-package-jsons-to-npm.ts b/src/change/write-package-jsons-to-npm.ts index d12ab42..a76ccee 100644 --- a/src/change/write-package-jsons-to-npm.ts +++ b/src/change/write-package-jsons-to-npm.ts @@ -1,33 +1,29 @@ -import { writeFileSync } from 'fs'; - -import { white } from 'chalk'; - import type { Module } from '../types'; import { logger } from '../helpers/logger'; +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...'); - Object.entries(env.packages).forEach(([packageName, currentPackage]) => { + Object.entries(env.packages!).forEach(([packageName, currentPackage]) => { if (!('packageJsonNpm' in currentPackage)) { const errorMessage = `missing "packageJsonNpm" on package ${packageName}.`; error(errorMessage); throw new Error(errorMessage); } - log(`Writting "${white(packageName)}"`); - - writeFileSync( + log(`Writting "${packageName}"`); + writeFile( `${currentPackage.path}/package.json`, - `${JSON.stringify(currentPackage.packageJsonNpm, null, 2)}\n` + currentPackage.packageJsonNpm ); }); diff --git a/src/change/write-rlsr-json.ts b/src/change/write-rlsr-json.ts index 7517145..dbeb257 100644 --- a/src/change/write-rlsr-json.ts +++ b/src/change/write-rlsr-json.ts @@ -1,9 +1,7 @@ import { join } from 'path'; - -import { writeFileSync } from 'fs'; - import type { Module } from '../types'; import { logger } from '../helpers/logger'; +import { writeFile } from '../helpers/write-file'; const { error, log } = logger('[change] write main changelog'); @@ -17,7 +15,7 @@ export const writeRlsrJson: Module = (env) => { log('writing status information to rlsr.json'); const statusFile = join(env.appRoot, 'rlsr.json'); + writeFile(statusFile, env.newStatus); - writeFileSync(statusFile, `${JSON.stringify(env.newStatus, null, 2)}\n`); return env; }; diff --git a/src/helpers/logger.ts b/src/helpers/logger.ts index f489d2c..efa9b62 100644 --- a/src/helpers/logger.ts +++ b/src/helpers/logger.ts @@ -20,9 +20,21 @@ const log = console.log(`${levels[level]} ${dim('[' + section + ']')}`, ...rest); }; -export const logger = (section: string) => ({ - debug: debug ? log('debug', section) : () => undefined, - log: log('log', section), - warn: log('warn', section), - error: log('error', section), -}); +const setCurrentTopic = (topic: string) => { + topic = topic; +}; + +export const logger = (section: string) => { + setCurrentTopic(section); + return { + debug: debug ? log('debug', section) : () => undefined, + log: log('log', section), + warn: log('warn', section), + error: log('error', section), + throwMissingAttrError: (attr: string) => { + const errorMessage = `missing "${attr}" on env object.`; + log('error', section); + throw new Error(errorMessage); + }, + }; +}; 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/helpers/write-file.ts b/src/helpers/write-file.ts new file mode 100644 index 0000000..ec9b24b --- /dev/null +++ b/src/helpers/write-file.ts @@ -0,0 +1,4 @@ +import { writeFileSync } from 'fs'; + +export const writeFile = (filePath: string, content: object) => + writeFileSync(filePath, `${JSON.stringify(content, null, 2)}\n`); 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 a03cf50..5e2dd6b 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 46eeb2e..e03ce45 100644 --- a/src/persist/write-package-jsons-for-git.ts +++ b/src/persist/write-package-jsons-for-git.ts @@ -1,25 +1,20 @@ -import { writeFileSync } 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('[persist] package.json files (git)'); +const topic = '[persist] package.json files (git)'; +const { error, log } = logger(topic); -// revert package.jsons -// after the package is publishet, we bring back the package.jsons with the `*` -// dependencies. The data structure should be in the env. -// This is a very similar step to change > writeToPackageJsons 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'); - Object.entries(env.packages).forEach(([packageName, currentPackage]) => { + Object.entries(env.packages!).forEach(([packageName, currentPackage]) => { if (!('packageJsonGit' in currentPackage)) { const errorMessage = `missing "packageJsonNpm" on package ${packageName}.`; error(errorMessage); @@ -29,10 +24,7 @@ export const writePackageJsonsForGit: Module = (env) => { const packageJsonPath = `${currentPackage.path}/package.json`; log(`Reverting "${packageName}" (${packageJsonPath})`); - writeFileSync( - packageJsonPath, - `${JSON.stringify(currentPackage.packageJsonGit, null, 2)}\n` - ); + writeFile(packageJsonPath, currentPackage.packageJsonGit); }); return env;