Skip to content

Commit

Permalink
feat(questions): add the ability to write custom questions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnelson committed Feb 24, 2017
1 parent 525bf55 commit 1578c68
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import autocomplete from 'inquirer-autocomplete-prompt';
import Repository from 'lerna/lib/Repository';
import PackageUtilities from 'lerna/lib/PackageUtilities';

import questions from './questions';
import makeDefaultQuestions from './make-default-questions';
import autocompleteQuestions from './autocomplete-questions';

function getAllPackages () {
Expand Down Expand Up @@ -46,22 +46,35 @@ function getCommitTypeMessage (type) {
}
return {
patch: '🛠 This commit indicates a patch release (0.0.X)',
minor: '✨ This commit indiates a minor release (0.X.0)',
minor: '✨ This commit indicates a minor release (0.X.0)',
major: '💥 This commit indicates a major release (X.0.0)',
}[type];
}

module.exports = {
prompter: function(cz, commit) {
function mergeQuestions(defaultQuestions, customQuestions) {
const questions = [];
defaultQuestions.forEach(question => {
const matchingCustomQuestions = customQuestions.filter(({ name: customQuestionName }) => (customQuestionName === question.name));
const customQuestion = matchingCustomQuestions.length > 0 && matchingCustomQuestions[0]
questions.push(customQuestion || question);
});
return questions;
}

function makePrompter(makeCustomQuestions = () => []) {
return function(cz, commit) {
const allPackages = getAllPackages().map((pkg) => pkg.name);
const changedPackages = getChangedPackages();
const rawQuestions = questions(allPackages, changedPackages);

cz.registerPrompt('autocomplete', autocomplete);
const defaultQuestions = makeDefaultQuestions(allPackages, changedPackages);
const customQuestions = makeCustomQuestions(allPackages, changedPackages);
const questions = mergeQuestions(defaultQuestions, customQuestions);

console.log('\n\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

cz.registerPrompt('autocomplete', autocomplete);
cz.prompt(
autocompleteQuestions(rawQuestions)
autocompleteQuestions(questions)
).then((answers) => {
const affectsLine = makeAffectsLine(answers);
if (affectsLine) {
Expand All @@ -81,4 +94,9 @@ module.exports = {
});
});
}
}

module.exports = {
prompter: makePrompter(),
makePrompter: makePrompter,
};
7 changes: 1 addition & 6 deletions src/questions.js → src/make-default-questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ module.exports = (allPackages, changedPackages) => ([
],
},
{
type: 'autocomplete',
type: 'input',
name: 'scope',
message: 'Denote the scope of this change:',
choices: [
{value: 'build', name: 'build: 🔨 Changes to the build system'},
{value: 'package', name: 'package: 📦 Updated dependencies or changed a package.json file'},
{value: 'docs', name: 'docs: 📖 Changes to documentation'},
]
},
{
type: 'input',
Expand Down
43 changes: 37 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import shell from 'shelljs';
import PackageUtilities from 'lerna/lib/PackageUtilities';

import stub from './_stub';
import { prompter } from '../src/index';
import { prompter, makePrompter } from '../src/index';


const createMockCommitizenCli = (answers) => ({
Expand All @@ -27,12 +27,12 @@ const createMockCommitizenCli = (answers) => ({


describe('cz-lerna-changelog', () => {
stub(shell, 'exec', () => ({ stdout: '' }));
stub(PackageUtilities, 'getPackages', () => ([{
name: 'test-package',
location: 'packages/test-package'
}]));
it('should generate correct commit message from prompt answers', (done) => {
stub(shell, 'exec', () => ({ stdout: '' }));
stub(PackageUtilities, 'getPackages', () => ([{
name: 'test-package',
location: 'packages/test-package'
}]));

const answers = {
'Select the type of change that you\'re committing:': 'feat',
Expand All @@ -56,4 +56,35 @@ describe('cz-lerna-changelog', () => {
}
})
});
it('allows questions to be overriden', (done) => {
const answers = {
'Select the type of change that you\'re committing:': 'feat',
'***Custom question for scope:***': 'Fake scope',
'Write a short, imperative tense description of the change:\n': 'Test commit',
'Provide a longer description of the change (optional). Use "|" to break new line:\n': 'This commit is a fake one',
'List any BREAKING CHANGES (optional):\n': '',
'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n': '',
'The packages that this commit has affected (0 detected)\n': ['test-package']
};

const makeCustomQuestions = () => ([
{
type: 'input',
name: 'scope',
message: '***Custom question for scope:***',
},
])

makePrompter(makeCustomQuestions)(createMockCommitizenCli(answers), (commitMessage) => {
try {
assert.equal(
commitMessage.trim(),
'feat(Fake scope): Test commit\n\naffects: test-package\n\nThis commit is a fake one'
);
done();
} catch (e) {
done(e);
}
})
});
});

0 comments on commit 1578c68

Please sign in to comment.