Skip to content

Commit

Permalink
Merge pull request #10 from wizardzloy/feature/use-mainline-dependencies
Browse files Browse the repository at this point in the history
feat(dependencies): Use official version of cz-conventional-changelog
  • Loading branch information
jpnelson authored Jan 23, 2017
2 parents 94b927c + c4d0283 commit d7039b5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "babel src -d lib",
"commit": "git-cz",
"test": "echo 'Tests need to be setup!'",
"test": "mocha --compilers js:babel-register test/**/*.js",
"prepublish": "npm run build",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
Expand All @@ -16,7 +16,7 @@
"author": "Joshua Nelson <jonelson@atlassian.com>, Joscha Feth <jfeth@atlassian.com>",
"license": "MIT",
"dependencies": {
"cz-conventional-changelog": "jpnelson/cz-conventional-changelog#a9a5468d",
"cz-conventional-changelog": "^1.2.0",
"shelljs": "0.7.0"
},
"peerDependencies": {
Expand All @@ -25,16 +25,18 @@
"devDependencies": {
"babel-cli": "6.8.0",
"babel-preset-es2015": "6.6.0",
"commitizen": "1.0.5",
"babel-register": "^6.18.0",
"commitizen": "^2.9.5",
"lerna": "^2.0.0-beta.31",
"mocha": "^3.2.0",
"semantic-release": "^4.3.5"
},
"directories": {
"lib": "lib"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
"path": "cz-conventional-changelog"
}
}
}
30 changes: 11 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import conventionalPrompt from 'cz-conventional-changelog/prompt';
import conventionalFormat from 'cz-conventional-changelog/format';
import conventionalChangelog from 'cz-conventional-changelog';

import PackageUtilities from 'lerna/lib/PackageUtilities';
import Repository from 'lerna/lib/Repository';
Expand Down Expand Up @@ -31,18 +30,12 @@ function getChangedPackages () {
}

module.exports = {
prompter: function(cz, options, commit) {
if (typeof options === 'function') {
commit = options;
options = {};
}

console.log('\n' + conventionalFormat.help + '\n');
prompter: function(cz, commit) {

const allPackages = getAllPackages().map((pkg) => pkg.name);

conventionalPrompt(cz, options, (conventionalAnswers) => {
const conventionalChangelogEntry = conventionalFormat.format(conventionalAnswers);
conventionalChangelog.prompter(cz, (commitMessage) => {
const [messageHead, ...restOfMessageParts] = commitMessage.split('\n\n');

cz.prompt({
type: 'checkbox',
Expand All @@ -51,29 +44,28 @@ module.exports = {
choices: allPackages,
message: `The packages that this commit has affected (${getChangedPackages().length} detected)\n`,
validate: function (input) {
const type = conventionalAnswers.type;
const isRequired = ['feat', 'fix'].indexOf(type) > -1;
const type = commitMessage.type;
const isRequired = ['feat', 'fix'].some((type) => messageHead.indexOf(type) === 0);
const isProvided = input.length > 0;
return isRequired ? (isProvided ? true : `Commit type "${type}" must affect at least one component`) : true;
}
}).then(function (packageAnswers) {
const messages = [
conventionalChangelogEntry.head
messageHead
];

const selectedPackages = packageAnswers.packages;
if (selectedPackages && selectedPackages.length) {
messages.push('affects: ' + selectedPackages.join(', '));
}

messages.push(conventionalChangelogEntry.body);
messages.push(conventionalChangelogEntry.footer);
messages.push(...restOfMessageParts);

const commitMessage = messages.join('\n\n');
const modifiedCommitMessage = messages.join('\n\n');

console.log(commitMessage);
console.log(modifiedCommitMessage);

commit(commitMessage);
commit(modifiedCommitMessage);
});
});
}
Expand Down
13 changes: 13 additions & 0 deletions test/_stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const stubs = [];

afterEach(() => {
stubs.forEach((release) => release());
});

export default function stub(obj, method, fn) {
const prev = obj[method];
obj[method] = fn;
fn.__stubbed__ = true;
stubs.push(() => obj[method] = prev);
}

57 changes: 57 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import assert from 'assert';
import shell from 'shelljs';
import PackageUtilities from 'lerna/lib/PackageUtilities';

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


const createMockCommitizenCli = (answers) => ({
prompt(questions) {
let qs = questions;

if (!Array.isArray(qs)) {
qs = [qs];
}

return Promise.resolve(
qs.reduce((acc, question) => {
acc[question.name] = answers[question.message];

return acc;
}, {})
);
}
});


describe('cz-lerna-changelog', () => {
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',
'Denote the scope of this change ($location, $browser, $compile, etc.):\n': 'Fake Scope',
'Write a short, imperative tense description of the change:\n': 'Test commit',
'Provide a longer description of the change:\n': 'This commit is a fake one',
'List any breaking changes or issues closed by this change:\n': '',
'The packages that this commit has affected (0 detected)\n': ['test-package']
};

prompter(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 d7039b5

Please sign in to comment.