Skip to content

Commit

Permalink
Merge pull request #28 from leonardoanalista/feat-26
Browse files Browse the repository at this point in the history
feat: add option to specify config file via package.json
  • Loading branch information
leonardoanalista committed Mar 29, 2016
2 parents 4658dfb + 95ea005 commit 9db46d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,24 @@ Suitable for large teams working with multiple projects with their own commit sc
}
```

* Run `cp ./node_modules/cz-customizable/cz-config-EXAMPLE.js ./.cz-config.js` in a project root directory to get a template.
## You have two options to configure `cz-customizable`:
* Option 1: Config block in your `package.json` (recommended):
```
...
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
},
"cz-customizable": {
"config": "config/path/to/my/config.js"
}
}
```
Note: option one allows you to have your config away from root directory. It also gives you a change to define any name to your `cz-config.js`.

* Option 2: Run `cp ./node_modules/cz-customizable/cz-config-EXAMPLE.js ./.cz-config.js` in a project root directory to get a template.
Note: if you chose option 2, config file has to be called `.cz-config.js`.


**Notes:**
* you should commit your `.cz-config.js` file to your git.
Expand Down
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,37 @@ var path = require('path');

/* istanbul ignore next */
function readConfigFile() {
// this function is replaced in test.
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) + '"');
// First try to find config block in the nearest package.json
var pkg = findConfig.require('package.json', {home: false});
if (pkg) {
if (pkg.config && pkg.config['cz-customizable'] && pkg.config['cz-customizable'].config) {
var pkgPath = path.resolve(pkg.config['cz-customizable'].config);

console.info('>>> Using cz-customizable config specified in your package.json: ', pkgPath);

config = require(CZ_CONFIG_EXAMPLE_LOCATION);
config = require(pkgPath);
return config;
}
}
return config;

// Second attempt is the nearest .cz-config.js.
var config = findConfig.require(CZ_CONFIG_NAME, {home: false});

if (config) {
console.info('>>> Using cz-customizable file ".cz-config.js"');
return config;
}

log.warn('Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "');

// 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);
// }
}

function buildCommit(answers) {
Expand Down

0 comments on commit 9db46d3

Please sign in to comment.