Skip to content

Commit

Permalink
Merge pull request #20 from leonardoanalista/issue-10
Browse files Browse the repository at this point in the history
feat(config): use find-config package to locate config
  • Loading branch information
Guria committed Jan 11, 2016
2 parents 15cf07e + c3dfe81 commit 2916bf9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 74 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a customizable Commitizen plugin. You can specify the commit types, scop


## Steps:
- install commitizen case you don't have it: `npm install -g commitizen`
- install commitizen in case you don't have it: `npm install -g commitizen`
- install the cz-customizable: `npm install cz-customizable --save-dev`
- configure `commitizen` to use `cz-customizable` as plugin. Add those lines to your `package.json`:
```
Expand All @@ -18,9 +18,7 @@ This is a customizable Commitizen plugin. You can specify the commit types, scop
}
```

- the `postinstall` script will automatically create a `.cz-config` in the root of your project *if it doesn't exsit*. It also creates a symlink inside `node_modules/cz-customizable` to point to your config file.

- you should commit your `.cz-config.js` file to your git.
- you should commit your `.cz-config.js` file to your git. Run `cp ./node_modules/cz-customizable/cz-config-EXAMPLE.js ./.cz-config.js` in a project root directory to get a template.

* if you don't provide a config file, this adapter will use the contents of the default file `node_modules/cz-customizable/cz-config-EXAMPLE.js`

Expand All @@ -38,20 +36,6 @@ Hopefully this will help you to have consistent commit messages and have a fully
It prompts for [conventional changelog](https://github.com/ajoslin/conventional-changelog/blob/master/conventions/angular.md) standard.


## Troubleshooting:
### you can't see the file `.cz-config` in the root of your porject.
- you can manually copy from `node_modules/cz-customizable/cz-config-EXAMPLE.js` to your project root (where your package.json is) and rename to `.cz-config.js`

### you edited the contents of `.cz-config.js` but `git cz` still doesn't show your values
- probably the post install script didn't create the symlink properly.
- Manual symlink creation:
- copy the file `cz-config-EXAMPLE.js` to the root of your project.
- rename the file to `.cz-config.js` and modify the options and scopes as you like.
- Now create a symlink to your config file:
- Linux ```ln -nsf ../../.cz-config.js node_modules/cz-customizable/.cz-config.js```
- Windows (something like this): ```mklink /D node_modules\cz-customizable\.cz-config.js ..\..\.cz-config.js```


## CONTRIBUTING

Please refer to the [Contributor Guidelines](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md) and [Conduct of Code](https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md) from [AngularJs](https://github.com/angular/angular.js) project.
Expand Down
57 changes: 40 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

// Inspired by: https://github.com/commitizen/cz-conventional-changelog and https://github.com/commitizen/cz-cli

var CZ_CONFIG_NAME = '.cz-config.js';
var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js';
var wrap = require('word-wrap');
var SYMLINK_CONFIG_NAME = 'cz-config';
var findConfig = require('find-config');
var log = require('winston');
var editor = require('editor');
var temp = require('temp').track();
var fs = require('fs');
var path = require('path');

/* istanbul ignore next */
function readConfigFile() {
// this function is replaced in test.
var config;
try {
// Try to find a customized version for the project
// This file is a symlink to the real one usually placed in the root of your project.
config = require('./' + SYMLINK_CONFIG_NAME);
} catch (err) {
log.warn('You don\'t have a file "' + SYMLINK_CONFIG_NAME + '" in your project root directory. We will use the default configuration file inside this directory: ' + __dirname);
log.warn('You should go to your "node_modules/cz-customizable" and run "npm run postinstall" to fix it up. Please report on Github if this doenst work.');

config = require('./cz-config-EXAMPLE');
var config = findConfig.require(CZ_CONFIG_NAME, {home: false});
if (!config) {
log.warn('Unable to find a config file "' + CZ_CONFIG_NAME + '". Default configuration would be used.');
log.warn('Copy and use it as template by running in a project root directory:\n "cp '
+ path.resolve(CZ_CONFIG_EXAMPLE_LOCATION) + ' ' + path.join('.', CZ_CONFIG_NAME) + '"');

config = require(CZ_CONFIG_EXAMPLE_LOCATION);
}
return config;
}
Expand Down Expand Up @@ -164,22 +167,42 @@ module.exports = {
when: isNotWip
},
{
type: 'confirm',
type: 'expand',
name: 'confirmCommit',
choices: [
{ key: 'y', name: 'Yes', value: 'yes' },
{ key: 'n', name: 'Abort commit', value: 'no' },
{ key: 'e', name: 'Edit message', value: 'edit' }
],
message: function(answers) {
var SEP = '###--------------------------------------------------------###';
log.info('\n' + SEP + '\n' + buildCommit(answers) + '\n' + SEP + '\n');
return 'Are you sure you want to proceed with the commit above?';
}
}
], function(answers) {
if (!answers.confirmCommit) {
if (answers.confirmCommit === 'edit') {
temp.open(null, function(err, info) {
/* istanbul ignore else */
if (!err) {
fs.write(info.fd, buildCommit(answers));
fs.close(info.fd, function(err) {
editor(info.path, function (code, sig) {
if (code === 0) {
var commitStr = fs.readFileSync(info.path, { encoding: 'utf8' });
commit(commitStr);
} else {
log.info('Editor returned non zero value. Commit message was:\n' + buildCommit(answers));
}
});
});
}
});
} else if (answers.confirmCommit === 'yes') {
commit(buildCommit(answers));
} else {
log.info('Commit has been canceled.');
return;
}

var commitStr = buildCommit(answers);
commit(commitStr);
});
}
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"test:check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
"test:watch": "node_modules/jasmine-node/bin/jasmine-node --color --autotest spec/ --watch .",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"postinstall": "node ./postinstall.js"
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"homepage": "https://github.com/leonardoanalista/cz-customizable",
"repository": {
Expand All @@ -24,8 +23,11 @@
],
"license": "MIT",
"dependencies": {
"word-wrap": "1.1.0",
"winston": "2.1.0"
"editor": "^1.0.0",
"find-config": "^0.3.0",
"temp": "^0.8.3",
"winston": "2.1.0",
"word-wrap": "1.1.0"
},
"devDependencies": {
"codecov.io": "0.1.6",
Expand Down
28 changes: 0 additions & 28 deletions postinstall.js

This file was deleted.

50 changes: 43 additions & 7 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ describe('cz-customizable', function() {

//question 8, last one
expect(getQuestion(8).name).toEqual('confirmCommit');
expect(getQuestion(8).type).toEqual('confirm');
expect(getQuestion(8).type).toEqual('expand');


var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand All @@ -110,7 +110,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature',
Expand All @@ -128,7 +128,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand All @@ -143,7 +143,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'WIP',
subject: 'this is my work-in-progress'
};
Expand All @@ -152,14 +152,50 @@ describe('cz-customizable', function() {
expect(commit).toHaveBeenCalledWith('WIP: this is my work-in-progress');
});

it('should allow edit message before commit', function(done) {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];
process.env.EDITOR = 'true';

var answers = {
confirmCommit: 'edit',
type: 'feat',
subject: 'create a new cool feature'
};

commitAnswers(answers);
setTimeout(function() {
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
done();
}, 100);
});

it('should not commit if editor returned non-zero value', function(done) {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];
process.env.EDITOR = 'false';

var answers = {
confirmCommit: 'edit',
type: 'feat',
subject: 'create a new cool feature'
};

commitAnswers(answers);
setTimeout(function() {
expect(commit.wasCalled).toEqual(false);
done();
}, 100);
});

it('should truncate first line if number of characters is higher than 200', function() {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var chars_100 = '0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789';

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: chars_100,
Expand Down Expand Up @@ -229,7 +265,7 @@ describe('cz-customizable', function() {
expect(getQuestion(6).when({type: 'FIX'})).toEqual(true);

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand Down

0 comments on commit 2916bf9

Please sign in to comment.