-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add interactive option to manually select commits (#81)
- Loading branch information
1 parent
1c4c422
commit d13ca2e
Showing
6 changed files
with
363 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { get, isEmpty, cloneDeep } = require('lodash') | ||
const inquirer = require('inquirer') | ||
const { set } = require('immutadot') | ||
|
||
const interactiveMode = { | ||
buildFormattedChoices, | ||
getFilteredChangelog, | ||
executeInteractiveMode, | ||
} | ||
|
||
function buildFormattedChoices(changelog) { | ||
const formattedChoices = [] | ||
|
||
changelog.changes | ||
.forEach(change => { | ||
change.groups | ||
.forEach(group => { | ||
formattedChoices.push(new inquirer.Separator(`${group.label} (version ${change.version})`)) | ||
|
||
group.commits.forEach(commit => { | ||
formattedChoices.push({ | ||
name: `${get(commit, 'emoji', '')} ${commit.message}`, | ||
value: commit.hash, | ||
checked: true, | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
return formattedChoices | ||
} | ||
|
||
function getFilteredChangelog(changelog, selectedCommitsHashes) { | ||
const changes = changelog.changes.map(change => { | ||
const groups = change.groups.map(group => { | ||
const filteredCommits = group.commits.filter(commit => { | ||
return selectedCommitsHashes.find(hash => commit.hash === hash) | ||
}) | ||
|
||
return set(group, 'commits', filteredCommits) | ||
}).filter(group => !isEmpty(group.commits)) | ||
|
||
return set(change, 'groups', groups) | ||
}).filter(change => !isEmpty(change.groups)) | ||
|
||
return { | ||
...changelog, | ||
changes, | ||
} | ||
} | ||
|
||
async function executeInteractiveMode(initialChangelog) { | ||
const initialChangelogCopy = cloneDeep(initialChangelog) | ||
const formattedChoices = interactiveMode.buildFormattedChoices(initialChangelogCopy) | ||
const prompt = inquirer.createPromptModule() | ||
const question = { | ||
type: 'checkbox', | ||
name: 'selectedCommitsHashes', | ||
message: 'Select commits', | ||
choices: formattedChoices, | ||
pageSize: 10, | ||
} | ||
|
||
const { selectedCommitsHashes } = await prompt(question) | ||
|
||
return interactiveMode.getFilteredChangelog(initialChangelogCopy, selectedCommitsHashes) | ||
} | ||
|
||
module.exports = interactiveMode |
205 changes: 205 additions & 0 deletions
205
packages/gitmoji-changelog-cli/src/interactiveMode.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
const inquirer = require('inquirer') | ||
const interactiveMode = require('./interactiveMode') | ||
|
||
describe('interactiveMode', () => { | ||
const commitAddProcess = { | ||
hash: '00c90b7844c3d030e967721eafea1b436ee51a6b', | ||
author: 'Franck', | ||
date: '2019-05-19T10:57:22+02:00', | ||
subject: ':sparkles: Add interactive process', | ||
emojiCode: 'sparkles', | ||
emoji: '✨', | ||
message: 'Add interactive process', | ||
group: 'added', | ||
siblings: [], | ||
body: '', | ||
} | ||
|
||
const commitAddOption = { | ||
hash: '3092ffd56e35fff7e35e8a9fcb7fff53005eac8a', | ||
author: 'Franck', | ||
date: '2019-05-18T18:39:44+02:00', | ||
subject: ':sparkles: Add interactive option', | ||
emojiCode: 'sparkles', | ||
emoji: '✨', | ||
message: 'Add interactive option', | ||
group: 'added', | ||
siblings: [], | ||
body: '', | ||
} | ||
|
||
const commitUpgradeDeps = { | ||
hash: 'b77199f96c8570b827dfcb11907d6f4edac98823', | ||
author: 's n', | ||
date: '2019-04-23T15:55:19+02:00', | ||
subject: ':arrow_up: Update handlebar to 4.0.14 (#78)', | ||
emojiCode: 'arrow_up', | ||
emoji: '⬆️', | ||
message: 'Update handlebar to 4.0.14 (#78)', | ||
group: 'changed', | ||
siblings: [], | ||
body: '', | ||
} | ||
|
||
const commitAddAuthor = { | ||
hash: '979da30f5e52385b99bd4a58e1a946793bd1196d', | ||
author: 'Benjamin Petetot', | ||
date: '2018-10-30T09:33:52+01:00', | ||
subject: ':sparkles: Add the author in changelog lines (#56)', | ||
emojiCode: 'sparkles', | ||
emoji: '✨', | ||
message: 'Add the author in changelog lines (#56)', | ||
group: 'added', | ||
siblings: [], | ||
body: '', | ||
} | ||
|
||
const groupAddedVersionNext = { | ||
group: 'added', | ||
label: 'Added', | ||
commits: [ | ||
commitAddProcess, | ||
commitAddOption, | ||
], | ||
} | ||
|
||
const groupChangedVersionNext = { | ||
group: 'changed', | ||
label: 'Changed', | ||
commits: [ | ||
commitUpgradeDeps, | ||
], | ||
} | ||
|
||
const groupAddedVersionOne = { | ||
group: 'added', | ||
label: 'Added', | ||
commits: [ | ||
commitAddAuthor, | ||
], | ||
} | ||
|
||
const versionNext = { | ||
version: 'next', | ||
groups: [ | ||
groupAddedVersionNext, | ||
groupChangedVersionNext, | ||
], | ||
} | ||
|
||
const versionOne = { | ||
version: '1.1.0', | ||
date: '2018-11-15', | ||
groups: [ | ||
groupAddedVersionOne, | ||
], | ||
} | ||
|
||
const initialChangelog = { | ||
changes: [ | ||
versionNext, | ||
versionOne, | ||
], | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('buildFormattedChoices', () => { | ||
it('should return a list of formatted choices from the given changelog', () => { | ||
const result = interactiveMode.buildFormattedChoices(initialChangelog) | ||
|
||
const expectedResult = [ | ||
new inquirer.Separator(`${groupAddedVersionNext.label} (version ${versionNext.version})`), | ||
{ | ||
name: `${commitAddProcess.emoji} ${commitAddProcess.message}`, | ||
value: commitAddProcess.hash, | ||
checked: true, | ||
}, | ||
{ | ||
name: `${commitAddOption.emoji} ${commitAddOption.message}`, | ||
value: commitAddOption.hash, | ||
checked: true, | ||
}, | ||
new inquirer.Separator(`${groupChangedVersionNext.label} (version ${versionNext.version})`), | ||
{ | ||
name: `${commitUpgradeDeps.emoji} ${commitUpgradeDeps.message}`, | ||
value: commitUpgradeDeps.hash, | ||
checked: true, | ||
}, | ||
new inquirer.Separator(`${groupAddedVersionOne.label} (version ${versionOne.version})`), | ||
{ | ||
name: `${commitAddAuthor.emoji} ${commitAddAuthor.message}`, | ||
value: commitAddAuthor.hash, | ||
checked: true, | ||
}, | ||
] | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) | ||
|
||
describe('getFilteredChangelog', () => { | ||
it('should return a new filtered changelog from the given inital changelog and selected commits', () => { | ||
const selectedCommitsHashes = [commitAddProcess.hash, commitAddAuthor.hash] | ||
|
||
const result = interactiveMode.getFilteredChangelog(initialChangelog, selectedCommitsHashes) | ||
|
||
const expectedResult = { | ||
changes: [ | ||
{ | ||
version: 'next', | ||
groups: [{ | ||
group: 'added', | ||
label: 'Added', | ||
commits: [ | ||
commitAddProcess, | ||
], | ||
}], | ||
}, | ||
{ | ||
version: '1.1.0', | ||
date: '2018-11-15', | ||
groups: [ | ||
groupAddedVersionOne, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
expect(result).toEqual(expectedResult) | ||
}) | ||
}) | ||
|
||
describe('executeInteractiveMode', () => { | ||
it('should call buildFormattedChoices, createPromptModule and getFilteredChangelog with correct parameters', async () => { | ||
const formattedChoices = [{ | ||
name: `${commitAddProcess.emoji} ${commitAddProcess.message}`, | ||
value: commitAddProcess.hash, | ||
checked: true, | ||
}] | ||
const selectedCommitsHashes = [commitAddProcess.hash, commitAddOption.hash] | ||
const prompt = jest.fn(() => Promise.resolve({ selectedCommitsHashes })) | ||
|
||
interactiveMode.buildFormattedChoices = jest.fn(() => formattedChoices) | ||
interactiveMode.getFilteredChangelog = jest.fn() | ||
inquirer.createPromptModule = jest.fn() | ||
inquirer.createPromptModule.mockReturnValueOnce(prompt) | ||
|
||
await interactiveMode.executeInteractiveMode(initialChangelog) | ||
|
||
expect(interactiveMode.buildFormattedChoices).toHaveBeenNthCalledWith(1, initialChangelog) | ||
expect(inquirer.createPromptModule).toHaveBeenCalledTimes(1) | ||
expect(prompt).toHaveBeenNthCalledWith(1, { | ||
type: 'checkbox', | ||
name: 'selectedCommitsHashes', | ||
message: 'Select commits', | ||
choices: formattedChoices, | ||
pageSize: 10, | ||
}) | ||
expect(interactiveMode.getFilteredChangelog) | ||
.toHaveBeenNthCalledWith(1, initialChangelog, selectedCommitsHashes) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.