Skip to content

Commit

Permalink
refactor(grease): integrate @flex-development/log@2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Sep 16, 2021
1 parent 1bc20c4 commit 787ac1d
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 186 deletions.
10 changes: 10 additions & 0 deletions packages/grease/__mocks__/@flex-development/log/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @file Node Module Mock - @flex-development/log
* @module grease/tests/mocks/flex-development/log
* @see https://jestjs.io/docs/next/manual-mocks#mocking-node-modules
* @see https://github.com/flex-development/log
*/

export default jest.fn((...args) => {
return jest.requireActual('@flex-development/log').default(...args)
})
8 changes: 0 additions & 8 deletions packages/grease/__mocks__/standard-version/lib/checkpoint.ts

This file was deleted.

8 changes: 4 additions & 4 deletions packages/grease/src/__tests__/main.functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import notes from '@grease/lifecycles/notes.lifecycle'
import GreaseOptions from '@grease/models/grease-options.model'
import cacheOptions from '@grease/utils/cache-options.util'
import getPrerelease from '@grease/utils/get-prerelease.util'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import readPackageFiles from '@grease/utils/read-package-files.util'
import anymatch from 'anymatch'
import { currentBranch } from 'isomorphic-git'
Expand All @@ -29,7 +29,7 @@ jest.mock('@grease/lifecycles/greaser.lifecycle')
jest.mock('@grease/lifecycles/notes.lifecycle')
jest.mock('@grease/utils/cache-options.util')
jest.mock('@grease/utils/get-prerelease.util')
jest.mock('@grease/utils/log.util')
jest.mock('@grease/utils/logger.util')
jest.mock('@grease/utils/read-package-files.util')

const mockAnymatch = anymatch as jest.MockedFunction<typeof anymatch>
Expand All @@ -47,7 +47,7 @@ const mockGetPrerelease = getPrerelease as jest.MockedFunction<
typeof getPrerelease
>
const mockGreaser = greaser as jest.MockedFunction<typeof greaser>
const mockLog = log as jest.MockedFunction<typeof log>
const mockLogger = logger as jest.MockedFunction<typeof logger>
const mockNotes = notes as jest.MockedFunction<typeof notes>
const mockReadPackageFiles = readPackageFiles as jest.MockedFunction<
typeof readPackageFiles
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('functional:main', () => {
}

// Expect
expect(mockLog).toBeCalledTimes(1)
expect(mockLogger).toBeCalledTimes(1)
expect(exception).toMatchObject({
code: ExceptionStatusCode.CONFLICT,
data: { releaseBranchWhitelist: $OPTS.releaseBranchWhitelist },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExceptionStatusCode } from '@flex-development/exceptions/enums'
import Exception from '@flex-development/exceptions/exceptions/base.exception'
import { DependencyCommand } from '@grease/enums/dependency-command.enum'
import type { IGreaseOptions } from '@grease/interfaces'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import indexOf from 'lodash/indexOf'
import sh from 'shelljs'
import runLifecycleScript from 'standard-version/lib/run-lifecycle-script'
Expand All @@ -13,10 +13,10 @@ import TestSubject from '../depchecker.lifecycle'
* @module grease/lifecycles/tests/functional/depchecker
*/

jest.mock('@grease/utils/log.util')
jest.mock('@grease/utils/logger.util')

const mockSH = sh as jest.Mocked<typeof sh>
const mockLog = log as jest.MockedFunction<typeof log>
const mockLogger = logger as jest.MockedFunction<typeof logger>
const mockRunLifecycleScript = runLifecycleScript as jest.MockedFunction<
typeof runLifecycleScript
>
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('functional:lifecycles/depchecker', () => {
await TestSubject(options)

// Expect
expect(mockLog.mock.calls[i + 1][1]).toBe(command)
expect(mockLogger.mock.calls[i + 1][1]).toBe(command)
})
})
})
Expand All @@ -109,7 +109,7 @@ describe('functional:lifecycles/depchecker', () => {

// Expect
expect(mockRunLifecycleScript).not.toBeCalled()
expect(mockLog).not.toBeCalled()
expect(mockLogger).not.toBeCalled()
expect(mockSH.which).not.toBeCalled()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GH_RELEASE_CREATE } from '@grease/config/constants.config'
import CreateReleaseDTO from '@grease/dtos/create-release.dto'
import type { ICreateReleaseDTO, IGreaseOptions } from '@grease/interfaces'
import { VERSION } from '@grease/tests/fixtures/git-tags.fixture'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import validate from '@grease/utils/validate.util'
import sh from 'shelljs'
import runLifecycleScript from 'standard-version/lib/run-lifecycle-script'
Expand All @@ -15,12 +15,12 @@ import TestSubject from '../greaser.lifecycle'
*/

jest.mock('@grease/config/cache.config')
jest.mock('@grease/utils/log.util')
jest.mock('@grease/utils/logger.util')
jest.mock('@grease/utils/validate.util')

const mockSH = sh as jest.Mocked<typeof sh>
const mockCache = cache as jest.Mocked<typeof cache>
const mockLog = log as jest.MockedFunction<typeof log>
const mockLogger = logger as jest.MockedFunction<typeof logger>
const mockRunLifecycleScript = runLifecycleScript as jest.MockedFunction<
typeof runLifecycleScript
>
Expand All @@ -45,7 +45,7 @@ describe('functional:lifecycles/greaser', () => {
})

it('should log checkpoints', () => {
expect(mockLog).toBeCalledTimes(1)
expect(mockLogger).toBeCalledTimes(1)
})

it('should validate release data', () => {
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('functional:lifecycles/greaser', () => {

// Expect
expect(mockRunLifecycleScript).not.toBeCalled()
expect(mockLog).not.toBeCalled()
expect(mockLogger).not.toBeCalled()
expect(mockSH.which).not.toBeCalled()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IGreaseOptions } from '@grease/interfaces'
import TAGS from '@grease/tests/fixtures/git-tags.fixture'
import INFILE from '@grease/tests/fixtures/infile.fixture'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import type { RestoreConsole } from 'jest-mock-console'
import mockConsole from 'jest-mock-console'
import sh from 'shelljs'
Expand All @@ -13,10 +13,10 @@ import TestSubject from '../notes.lifecycle'
* @module grease/lifecycles/tests/functional/notes
*/

jest.mock('@grease/utils/log.util')
jest.mock('@grease/utils/logger.util')

const mockSH = sh as jest.Mocked<typeof sh>
const mockLog = log as jest.MockedFunction<typeof log>
const mockLogger = logger as jest.MockedFunction<typeof logger>
const mockRunLifecycleScript = runLifecycleScript as jest.MockedFunction<
typeof runLifecycleScript
>
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('functional:lifecycles/notes', () => {
})

it('should log checkpoints', () => {
expect(mockLog).toBeCalledTimes(2)
expect(mockLogger).toBeCalledTimes(3)
})
})

Expand All @@ -58,7 +58,7 @@ describe('functional:lifecycles/notes', () => {

// Expect
expect(mockRunLifecycleScript).not.toBeCalled()
expect(mockLog).toBeCalledTimes(1)
expect(mockLogger).toBeCalledTimes(1)
expect(mockSH.exec).not.toBeCalled()
})
})
Expand Down
7 changes: 4 additions & 3 deletions packages/grease/src/lifecycles/depchecker.lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ExceptionStatusCode } from '@flex-development/exceptions/enums'
import Exception from '@flex-development/exceptions/exceptions/base.exception'
import { LogLevel } from '@flex-development/log/enums/log-level.enum'
import { DependencyCommand } from '@grease/enums/dependency-command.enum'
import type { IGreaseOptions } from '@grease/interfaces'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import sh from 'shelljs'
import runLifecycleScript from 'standard-version/lib/run-lifecycle-script'

Expand Down Expand Up @@ -37,15 +38,15 @@ const Depchecker = async (options: IGreaseOptions = {}): Promise<void> => {
const commands = Object.keys(DependencyCommand)

// Log checkpoint
log(options, 'checking commands:', commands, 'info')
logger(options, 'checking commands:', commands, LogLevel.INFO)

// Check if required dependencies are installed
Object.values(DependencyCommand).forEach(command => {
if (!sh.which(command)) {
throw new Exception(ExceptionStatusCode.NOT_FOUND, `${command} not found`)
}

log(options, command)
logger(options, command)
return
})

Expand Down
11 changes: 7 additions & 4 deletions packages/grease/src/lifecycles/greaser.lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LogLevel } from '@flex-development/log/enums/log-level.enum'
import { GH_RELEASE_CREATE } from '@grease/config/constants.config'
import CreateReleaseDTO from '@grease/dtos/create-release.dto'
import type { ICreateReleaseDTO, IGreaseOptions } from '@grease/interfaces'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import validate from '@grease/utils/validate.util'
import ch from 'chalk'
import { classToPlain } from 'class-transformer'
Expand Down Expand Up @@ -35,7 +36,7 @@ const Greaser = async (
await runLifecycleScript(options, 'pregreaser')

// Log release start checkpoint
log(options, 'starting github release...', [], 'info')
logger(options, 'starting github release...', [], LogLevel.INFO)

// Validate release data
dto = await validate(CreateReleaseDTO, dto, false)
Expand All @@ -51,9 +52,11 @@ const Greaser = async (
// Execute GitHub release
if (!options.dryRun) sh.exec(command, { silent: options.silent })
else {
log(options, GH_RELEASE_CREATE)
logger(options, GH_RELEASE_CREATE)

if (!options.silent) console.log(`\n---\n${ch.gray(command)}\n---\n`)
if (!options.silent) {
logger(options, `\n---\n${ch.gray(command)}\n---\n`, [], LogLevel.DEBUG)
}
}

// Run `postgreaser` script
Expand Down
24 changes: 15 additions & 9 deletions packages/grease/src/lifecycles/notes.lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExceptionStatusCode } from '@flex-development/exceptions/enums'
import Exception from '@flex-development/exceptions/exceptions/base.exception'
import { LogLevel } from '@flex-development/log/enums/log-level.enum'
import type { NullishString } from '@flex-development/tutils'
import {
GREASER_NOTES_BIRTHDAY,
Expand All @@ -11,7 +12,7 @@ import { NotesType } from '@grease/enums/notes-type.enum'
import type { IGreaseOptions } from '@grease/interfaces'
import type { SemanticVersion } from '@grease/types'
import changelogVersions from '@grease/utils/changelog-versions.util'
import log from '@grease/utils/log.util'
import logger from '@grease/utils/logger.util'
import ch from 'chalk'
import fs from 'fs'
import indexOf from 'lodash/indexOf'
Expand Down Expand Up @@ -45,7 +46,7 @@ const Notes = async (
): Promise<NullishString> => {
// Skip lifecycle
if (options.skip?.notes || options.notesType === NotesType.NULL) {
log(options, 'skipping release notes', [], 'error')
logger(options, 'skipping release notes', [], LogLevel.ERROR)
return GREASER_NOTES_NULL
}

Expand All @@ -54,34 +55,39 @@ const Notes = async (

// Generate blank notes
if (options.notesType === NotesType.BLANK) {
log(options, 'created blank release notes')
logger(options, 'created blank release notes')
return GREASER_NOTES_BLANK
}

// Generate birthday notes
if (options.firstRelease || options.notesType === NotesType.BIRTHDAY) {
log(options, 'created birthday format release notes')
logger(options, 'created birthday format release notes')
return GREASER_NOTES_BIRTHDAY
}

// Skip notes if no changelog is missing
if (!options.infile || !fs.existsSync(options.infile as fs.PathLike)) {
const args = [`${options.infile} does not exist`]

log(options, 'skipping release notes', args, 'error')
logger(options, 'skipping release notes', args, LogLevel.ERROR)
return GREASER_NOTES_NULL
}

// Skip notes if missing package version
if (!version) {
const args = ['package version is', version]

log(options, 'skipping release notes', args, 'error')
logger(options, 'skipping release notes', args, LogLevel.ERROR)
return GREASER_NOTES_NULL
}

// Log validation checkpoint
log(options, 'creating release notes from %s', [options.infile], 'info')
logger(
options,
'creating release notes from %s',
[options.infile],
LogLevel.INFO
)

// Get changelog content and versions (in descending order)
const content = fs.readFileSync(options.infile as fs.PathLike, 'utf8')
Expand Down Expand Up @@ -119,11 +125,11 @@ const Notes = async (
notes = notes.substring(notes.indexOf(BR), notes.lastIndexOf(BR)).trim()

// Log notes checkpoint
log(options, 'created release notes')
logger(options, 'created release notes')

// Log notes if dry run is enabled
if (options.dryRun && !options.silent) {
console.log(`\n---\n${ch.gray(notes)}\n---\n`)
logger(options, `\n---\n${ch.gray(notes)}\n---\n`, [], LogLevel.DEBUG)
}

// Run `postnotes` script
Expand Down
5 changes: 3 additions & 2 deletions packages/grease/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExceptionStatusCode } from '@flex-development/exceptions/enums'
import Exception from '@flex-development/exceptions/exceptions/base.exception'
import { LogLevel } from '@flex-development/log/enums/log-level.enum'
import type { ObjectPlain } from '@flex-development/tutils'
import anymatch from 'anymatch'
import fs from 'fs'
Expand All @@ -20,7 +21,7 @@ import notes from './lifecycles/notes.lifecycle'
import type { SemanticVersionTag } from './types'
import cacheOptions from './utils/cache-options.util'
import getPrerelease from './utils/get-prerelease.util'
import log from './utils/log.util'
import logger from './utils/logger.util'
import readPackageFiles from './utils/read-package-files.util'

/**
Expand Down Expand Up @@ -124,7 +125,7 @@ const main = async (args: IGreaseOptions | ObjectPlain = {}): Promise<void> => {
stack
} = error as Exception

log(args, message, [], 'error', true)
logger(args, message, [], LogLevel.ERROR, true)

throw new Exception(code, message, { ...data, errors }, stack).toJSON()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file User Module Mock - log
* @module config/mocks/log
* @file User Module Mock - logger
* @module config/mocks/logger
* @see https://jestjs.io/docs/next/manual-mocks#mocking-user-modules
*/

Expand Down

0 comments on commit 787ac1d

Please sign in to comment.