Skip to content

Commit

Permalink
refactor(config): Rename commitTypes config to releaseRules 📦
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Configuration change: rename `commitTypes` to `releaseRules`
  • Loading branch information
pvdlg committed Aug 23, 2017
1 parent 594741d commit 775dc38
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 140 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Additionnal options can be set within the plugin definition in `package.json` to
"analyzeCommits": {
"path": "sr-commit-analyzer",
"preset": "angular",
"commitTypes": [
"releaseRules": [
{"type": "docs", "scope":"README", "release": "patch"},
{"type": "refactor", "release": "patch"},
{"type": "style", "release": "patch"}
Expand All @@ -52,12 +52,12 @@ Additionnal options can be set within the plugin definition in `package.json` to
}
```

| Option | Description | Default |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| `preset` | [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset (possible values: `angular`, `atom`, `codemirror`, `ember`, `eslint`, `express`, `jquery`, `jscs`, `jshint`). | `angular` |
| `config` | NPM package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - |
| `commitTypes` | An external module, a path to a module or an `Array` of rules. See [Commit types](#commit-types). | See [Commit types](#commit-types) |
| `parserOpts` | Additional [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#conventionalcommitsparseroptions) options that will extends ones loaded by `preset` or `config`. See [Parser options](#parser-options). | - |
| Option | Description | Default |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| `preset` | [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset (possible values: `angular`, `atom`, `codemirror`, `ember`, `eslint`, `express`, `jquery`, `jscs`, `jshint`). | `angular` |
| `config` | NPM package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - |
| `releaseRules` | An external module, a path to a module or an `Array` of rules. See [Commit types](#release-rules). | See [Commit types](#release-rules) |
| `parserOpts` | Additional [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#conventionalcommitsparseroptions) options that will extends ones loaded by `preset` or `config`. See [Parser options](#parser-options). | - |

**NOTE:** `config` will be overwritten by the values of `preset`. You should use either `preset` or `config`, but not both. Individual properties of `parserOpts` will overwrite ones loaded with `preset` or `config`.

Expand All @@ -70,7 +70,7 @@ This is an `Array` of rule objects. A rule object has a `release` property and 1
"release": {
"analyzeCommits": {
"preset": "angular",
"commitTypes": [
"releaseRules": [
{"type": "docs", "scope": "README", "release": "patch"},
{"type": "refactor", "scope": "/core-.*/", "release": "minor"},
{"type": "refactor", "release": "patch"}
Expand All @@ -92,7 +92,7 @@ With the previous example:

#### Default rules matching

If a commit doesn't match any rule in `commitTypes` it will be evaluated agaisnt the [default commit types](lib/default/commit-types.js).
If a commit doesn't match any rule in `releaseRules` it will be evaluated agaisnt the [default commit types](lib/default/release-rules.js).

With the previous example:
* Commits with a breaking change will be associated with a `minor` release.
Expand All @@ -102,7 +102,7 @@ With the previous example:

#### No rules matching

If a commit doesn't match any rules in `commitTypes` or in [default commit types](lib/default/commit-types.js) then no release type will be associated with the commit.
If a commit doesn't match any rules in `releaseRules` or in [default commit types](lib/default/release-rules.js) then no release type will be associated with the commit.

With the previous example:
* Commits with `type` 'style' will not be associated with a release type.
Expand All @@ -129,7 +129,7 @@ For example with `eslint` preset:
"release": {
"analyzeCommits": {
"preset": "eslint",
"commitTypes": [
"releaseRules": [
{"tag": "Docs", "message":"/README/", "release": "patch"},
{"type": "New", "release": "patch"}
]
Expand All @@ -140,28 +140,28 @@ For example with `eslint` preset:
With this configuration:
* Commits with `tag` 'Docs', that contains 'README' in their header message will be associated with a `patch` release.
* Commits with `tag` 'New' will be associated with a `patch` release.
* Commits with `tag` 'Breaking' will be associated with a `major` release (per [default commit types](lib/default/commit-types.js)).
* Commits with `tag` 'Fix' will be associated with a `patch` release (per [default commit types](lib/default/commit-types.js)).
* Commits with `tag` 'Update' will be associated with a `minor` release (per [default commit types](lib/default/commit-types.js)).
* Commits with `tag` 'New' will be associated with a `minor` release (per [default commit types](lib/default/commit-types.js)).
* Commits with `tag` 'Breaking' will be associated with a `major` release (per [default commit types](lib/default/release-rules.js)).
* Commits with `tag` 'Fix' will be associated with a `patch` release (per [default commit types](lib/default/release-rules.js)).
* Commits with `tag` 'Update' will be associated with a `minor` release (per [default commit types](lib/default/release-rules.js)).
* Commits with `tag` 'New' will be associated with a `minor` release (per [default commit types](lib/default/release-rules.js)).
* All other commits will not be associated with a release type.

#### External package / file

`commitTypes` can also reference a module, either by it's `npm` name or path:
`releaseRules` can also reference a module, either by it's `npm` name or path:
```json
{
"release": {
"analyzeCommits": {
"path": "sr-commit-analyzer",
"preset": "angular",
"commitTypes": "config/commit-types.js"
"releaseRules": "./config/release-rules.js"
}
}
}
```
```js
// File: config/commit-types.js
// File: config/release-rules.js
module.exports = [
{type: 'docs', scope: 'README', release: 'patch'},
{type: 'refactor', scope: /core-.*/, release: 'minor'},
Expand Down
6 changes: 3 additions & 3 deletions lib/analyze-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const compareReleaseTypes = require('./compare-release-types');
/**
* Find all the rules matching and return the highest release type of the matching rules.
*
* @param {Array} commitTypes the rules to match the commit against.
* @param {Array} releaseRules the rules to match the commit against.
* @param {Commit} commit a parsed commit.
* @return {string} the highest release type of the matching rules or `undefined` if no rule match the commit.
*/
module.exports = (commitTypes, commit) => {
module.exports = (releaseRules, commit) => {
let releaseType;

commitTypes
releaseRules
.filter(
rule =>
(!rule.breaking || (commit.notes && commit.notes.length > 0)) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Default `commitTypes` rules for common commit formats, following conventions.
* Default `releaseRules` rules for common commit formats, following conventions.
*
* @type {Array}
*/
Expand Down
18 changes: 9 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const loadParserConfig = require('./load/parser-config');
const loadCommitTypes = require('./load/commit-types');
const loadReleaseRules = require('./load/release-rules');
const parse = require('./parse');
const analyzeCommit = require('./analyze-commit');
const compareReleaseTypes = require('./compare-release-types');
const RELEASE_TYPES = require('./default/release-types');
const DEFAULT_COMMIT_TYPES = require('./default/commit-types');
const DEFAULT_RELEASE_RULES = require('./default/release-rules');

/**
* @callback commitAnalyzerCallback
Expand All @@ -18,29 +18,29 @@ const DEFAULT_COMMIT_TYPES = require('./default/commit-types');
* @param {Object} pluginConfig semantic-release configuration
* @param {string} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
* @param {string} pluginConfig.config requierable npm package with a custom conventional-changelog preset
* @param {string|Array} pluginConfig.commitTypes a string to load an external module or an `Array` of rules.
* @param {string|Array} pluginConfig.releaseRules a string to load an external module or an `Array` of rules.
* @param {Object} pluginConfig.parserOpts additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} options semantic-release options
* @param {Array} options.commits array of commits
* @param {commitAnalyzerCallback} callback The callback called with the release type.
*/
module.exports = async (pluginConfig, {commits}, callback) => {
try {
const commitTypes = loadCommitTypes(pluginConfig);
const releaseRules = loadReleaseRules(pluginConfig);
const config = await loadParserConfig(pluginConfig);
let releaseType = null;

commits.every(rawCommit => {
const commit = parse(rawCommit.message, config);
let commitReleaseType;

// Determine release type based on custom commitTypes
if (commitTypes) {
commitReleaseType = analyzeCommit(commitTypes, commit);
// Determine release type based on custom releaseRules
if (releaseRules) {
commitReleaseType = analyzeCommit(releaseRules, commit);
}
// If no custom commitTypes or none matched the commit, try with default commitTypes
// If no custom releaseRules or none matched the commit, try with default releaseRules
if (!commitReleaseType) {
commitReleaseType = analyzeCommit(DEFAULT_COMMIT_TYPES, commit);
commitReleaseType = analyzeCommit(DEFAULT_RELEASE_RULES, commit);
}
// Set releaseType if commit's release type is higher
if (commitReleaseType && compareReleaseTypes(releaseType, commitReleaseType)) {
Expand Down
39 changes: 0 additions & 39 deletions lib/load/commit-types.js

This file was deleted.

39 changes: 39 additions & 0 deletions lib/load/release-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const importCwd = require('import-cwd');
const SemanticReleaseError = require('@semantic-release/error');
const RELEASE_TYPES = require('../default/release-types');

/**
* Load and validate the `releaseRules` rules.
*
* If `releaseRules` parameter is a `string` then load it as an external module with `require`.
* Verifies that the loaded/parameter `releaseRules` is an `Array` and each element has a valid `release` attribute.
*
* @param {string|Array} releaseRules a string to load an external module or an `Array` of rules.
* @return {Array} the loaded and validated `releaseRules`.
*/
module.exports = ({releaseRules}) => {
let loadedReleaseRules;

if (releaseRules) {
loadedReleaseRules = typeof releaseRules === 'string' ? importCwd(releaseRules) : releaseRules;

if (!Array.isArray(loadedReleaseRules)) {
throw new SemanticReleaseError(
'Error in sr-commit-analyzer configuration: "releaseRules" must be an array of rules',
'EINVALIDCONFIG'
);
}

loadedReleaseRules.forEach(rule => {
if (RELEASE_TYPES.indexOf(rule.release) === -1) {
throw new SemanticReleaseError(
`Error in sr-commit-analyzer configuration: "${rule.release}" is not a valid release type. Valid values are: ${JSON.stringify(
RELEASE_TYPES
)}`,
'EINVALIDRELEASE'
);
}
});
}
return loadedReleaseRules;
};
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 775dc38

Please sign in to comment.