Skip to content

Commit

Permalink
♻️ Change core function parameters from "mode" to "from" and "to" (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
frinyvonnick authored Oct 30, 2019
1 parent d22699b commit 42af500
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 40 deletions.
8 changes: 8 additions & 0 deletions packages/gitmoji-changelog-cli/src/cli.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,12 @@ describe('generate changelog', () => {
const updatedContent = content.replace(version, to)
fs.writeFileSync(pkg, updatedContent)
}

/*
* This function is useful to print cli ouput when debugging tests
*/
// eslint-disable-next-line no-unused-vars
function logOutput(ouput) {
console.log(ouput.toString('utf8'))
}
})
12 changes: 11 additions & 1 deletion packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs')
const { get } = require('lodash')
const { set } = require('immutadot')
const libnpm = require('libnpm')
const semver = require('semver')
Expand Down Expand Up @@ -84,7 +85,16 @@ async function getChangelog(options) {
release,
}

let changelog = await generateChangelog(enhancedOptions)
// let changelog = await generateChangelog(enhancedOptions)
let changelog
if (options.mode === 'init') {
changelog = await generateChangelog('', release, enhancedOptions)
} else {
const { meta } = options
const lastVersion = get(meta, 'lastVersion')

changelog = await generateChangelog(lastVersion, release, enhancedOptions)
}

if (options.interactive) {
changelog = await executeInteractiveMode(changelog)
Expand Down
15 changes: 8 additions & 7 deletions packages/gitmoji-changelog-cli/src/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { main } = require('./cli')

describe('cli', () => {
const realExitFunction = process.exit
const options = { preset: 'node' }
beforeEach(() => {
process.exit = jest.fn(() => {})
jest.clearAllMocks()
Expand All @@ -15,13 +16,13 @@ describe('cli', () => {
it('should throw an error if changelog generation fails', async () => {
generateChangelog.mockRejectedValue(new Error())

await main()
await main(options)

expect(logger.error).toHaveBeenCalled()
})

it('should call process.exit explicitly so promises are not waited for', async () => {
await main()
await main(options)

expect(process.exit).toHaveBeenCalledTimes(1)
})
Expand All @@ -31,34 +32,34 @@ describe('cli', () => {

it('should print a warning about a new version', async () => {
manifest.mockReturnValueOnce(Promise.resolve({ version: '2.0.0' }))
await main()
await main(options)

expect(findOutdatedMessage()).toBeTruthy()
})

it('should NOT print a warning about a new version', async () => {
// older version in npm registry
manifest.mockReturnValueOnce(Promise.resolve({ version: '0.5.0' }))
await main()
await main(options)

// same version in npm registry
manifest.mockReturnValueOnce(Promise.resolve({ version: '1.0.0' }))
await main()
await main(options)

expect(manifest).toHaveBeenCalledTimes(2)
expect(findOutdatedMessage()).toBeFalsy()
})

it('should NOT print a warning about a new version when request took to much time', async () => {
manifest.mockImplementationOnce(() => new Promise((resolve) => { setTimeout(resolve, 1000, { version: '2.0.0' }) }))
await main()
await main(options)

expect(findOutdatedMessage()).toBeFalsy()
})

it('should NOT print a warning about a new version when request is on error', async () => {
manifest.mockReturnValueOnce(Promise.reject(new Error('faked error (manifest)')))
await main()
await main(options)

expect(findOutdatedMessage()).toBeFalsy()
})
Expand Down
31 changes: 9 additions & 22 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const gitSemverTags = require('git-semver-tags')
const semverCompare = require('semver-compare')
const through = require('through2')
const concat = require('concat-stream')
const { get, isEmpty } = require('lodash')
const { isEmpty } = require('lodash')
const { promisify } = require('util')

const { parseCommit } = require('./parser')
Expand Down Expand Up @@ -121,29 +121,16 @@ async function generateVersions({
return changes
}

async function generateChangelog(options = {}) {
const {
mode,
release,
groupSimilarCommits,
} = options

async function generateChangelog(from, to, { groupSimilarCommits } = {}) {
const gitTags = await gitSemverTagsAsync()
let tagsToProcess = [...gitTags]
const hasNext = hasNextVersion(gitTags, release)
let lastVersion
const hasNext = hasNextVersion(gitTags, to)

if (mode === 'init') {
lastVersion = release
if (from === TAIL) {
tagsToProcess = [...tagsToProcess, TAIL]
} else {
const { meta } = options
lastVersion = get(meta, 'lastVersion')

if (lastVersion !== undefined) {
const lastVersionIndex = tagsToProcess.findIndex(tag => semver.eq(tag, lastVersion))
tagsToProcess.splice(lastVersionIndex + 1)
}
const fromIndex = tagsToProcess.findIndex(tag => semver.eq(tag, from))
tagsToProcess.splice(fromIndex + 1)

if (hasNext && isEmpty(tagsToProcess)) {
tagsToProcess.push(HEAD)
Expand All @@ -153,17 +140,17 @@ async function generateChangelog(options = {}) {
const changes = await generateVersions({
tags: tagsToProcess,
hasNext,
release,
release: to,
groupSimilarCommits,
})

if (mode === 'update' && changes.length === 1 && isEmpty(changes[0].groups)) {
if (from !== TAIL && changes.length === 1 && isEmpty(changes[0].groups)) {
throw new Error('No changes found. You may need to fetch or pull the last changes.')
}

return {
meta: {
lastVersion: sanitizeVersion(lastVersion),
lastVersion: sanitizeVersion(from),
},
changes: changes.filter(({ groups }) => groups.length),
}
Expand Down
23 changes: 13 additions & 10 deletions packages/gitmoji-changelog-core/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const gitSemverTags = require('git-semver-tags')

const { generateChangelog } = require('./index')

const TAIL = ''
const HEAD = ''

const uselessCommit = {
hash: '460b79497ae7e791bc8ba8475bda8f0b93630dd3',
date: '2018-09-14T21:00:18+02:00',
Expand Down Expand Up @@ -86,7 +89,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, []))

const { changes } = await generateChangelog({ mode: 'init', release: 'next' })
const { changes } = await generateChangelog(TAIL, 'next')

expect(changes).toEqual([
{
Expand All @@ -107,7 +110,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, []))

const { changes } = await generateChangelog({ mode: 'update', release: 'next' })
const { changes } = await generateChangelog('v1.0.0', 'next')

expect(changes).toEqual([
{
Expand All @@ -129,7 +132,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, ['v1.0.0']))

const { changes } = await generateChangelog({ mode: 'init' })
const { changes } = await generateChangelog(TAIL, HEAD)

expect(changes).toEqual([
{
Expand Down Expand Up @@ -168,7 +171,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, ['v1.0.0']))

const { changes } = await generateChangelog({ mode: 'update', meta: { lastVersion: 'v1.0.0' } })
const { changes } = await generateChangelog('v1.0.0', 'next')

expect(changes).toEqual([
{
Expand All @@ -195,7 +198,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, ['v1.0.0']))

const { changes } = await generateChangelog({ mode: 'init', groupSimilarCommits: true })
const { changes } = await generateChangelog(TAIL, HEAD, { groupSimilarCommits: true })

expect(changes[0].groups[0].commits).toEqual([
secondRecycleCommit,
Expand All @@ -213,7 +216,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, []))

const { changes } = await generateChangelog({ mode: 'init' })
const { changes } = await generateChangelog(TAIL, HEAD)

expect(changes).toEqual([
expect.objectContaining({
Expand All @@ -233,7 +236,7 @@ describe('changelog', () => {

let message = false
try {
await generateChangelog({ mode: 'update', release: 'next' })
await generateChangelog('v1.0.0', 'next')
} catch (e) {
message = e.message
}
Expand All @@ -247,7 +250,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, ['v1.0.1', 'v1.0.0']))

await generateChangelog({ mode: 'init' })
await generateChangelog(TAIL, HEAD)

expect(gitRawCommits).toHaveBeenCalledWith(expect.objectContaining({ from: 'v1.0.1', to: '' }))
expect(gitRawCommits).toHaveBeenCalledWith(
Expand All @@ -264,7 +267,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, ['v1.0.0', '1.0.0', 'v1.1.1']))

const { changes } = await generateChangelog({ mode: 'init' })
const { changes } = await generateChangelog(TAIL, HEAD)

// inputs has 4 group (4 versions)
// but output should only has 3, since the 3rd is empty
Expand All @@ -286,7 +289,7 @@ describe('changelog', () => {

gitSemverTags.mockImplementation(cb => cb(null, []))

const { changes } = await generateChangelog({ mode: 'init' })
const { changes } = await generateChangelog(TAIL, HEAD)

expect(changes[0].groups[0].commits.map(({ date, body }) => ({ date, body })))
.toEqual([
Expand Down

0 comments on commit 42af500

Please sign in to comment.