Skip to content

Commit

Permalink
Merge pull request #7 from uglow/feature/add-husky-support
Browse files Browse the repository at this point in the history
feat(husky): add husky support and better docs
  • Loading branch information
uglow authored Feb 1, 2017
2 parents 1ab6726 + 308a342 commit fd2b69e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 34 deletions.
9 changes: 2 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,8 @@ Command | Description

Command | Description
:------ | :----------
<pre>npm run pre-release</pre> | Verify code, run unit tests, check test coverage, build software. This task is designed to be run before
the `semantic-release` task.
<ul><li>Run `semantic-release-cli setup` once you have a remote repository. See https://github.com/semantic-release/cli for details.</li><li>Semantic-release integrates with Travis CI (or similar tools) to generate release notes
for each release (which appears in the "Releases" section in GitHub) and
publishes the package to NPM (when all the tests are successful) with a semantic version number.
</li></ul>
<pre>npm run upload-coverage</pre> | Uploads code-coverage metrics to Coveralls.io<ul><li>Setup - https://coveralls.zendesk.com/hc/en-us/articles/201347419-Coveralls-currently-supports</li><li>Define an environment variable called COVERALLS_REPO_TOKEN in your build environment with the repo token from https://coveralls.io/github/<repo-name>/settings</li></ul>
<pre>npm run pre-release</pre> | Verify code, run unit tests, check test coverage, build software. This task is designed to be run before the `semantic-release` task. <ul><li>Run `semantic-release-cli setup` once you have a remote repository. See https://github.com/semantic-release/cli for details.</li><li>Semantic-release integrates with Travis CI (or similar tools) to generate release notes for each release (which appears in the "Releases" section in GitHub) and publishes the package to NPM (when all the tests are successful) with a semantic version number.</li></ul>
<pre>npm run upload-coverage</pre> | Uploads code-coverage metrics to Coveralls.io<ul><li>Setup - https://coveralls.zendesk.com/hc/en-us/articles/201347419-Coveralls-currently-supports</li><li>Define an environment variable called COVERALLS_REPO_TOKEN in your build environment with the repo token from https://coveralls.io/github/<repo-name>/settings</li><li>In your CI configuration (e.g. `travis.yml`), call `npm run upload-coverage` if the build is successful.</li></ul>



Expand Down
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cz-customizable-ghooks

Integrate [cz-customizable](https://github.com/leonardoanalista/cz-customizable) config with [ghooks](https://github.com/gtramontina/ghooks) to use a single configuration for commit message generation AND commit message hooks.
Integrate [cz-customizable](https://github.com/leonardoanalista/cz-customizable) config with [ghooks](https://github.com/gtramontina/ghooks) or [husky](https://github.com/typicode/husky) to use a single configuration for commit message generation AND commit message validation.

[![Build Status](https://travis-ci.org/uglow/cz-customizable-ghooks.svg?branch=master)](https://travis-ci.org/uglow/cz-customizable-ghooks)
<!--[RM_BADGES]-->
Expand All @@ -9,38 +9,86 @@ Integrate [cz-customizable](https://github.com/leonardoanalista/cz-customizable)
[![Coverage Status](https://coveralls.io/repos/github/uglow/cz-customizable-ghooks/badge.svg?branch=master)](https://coveralls.io/github/uglow/cz-customizable-ghooks?branch=master)
[![Dependencies status](https://david-dm.org/uglow/cz-customizable-ghooks/status.svg?theme=shields.io)](https://david-dm.org/uglow/cz-customizable-ghooks#info=dependencies)
[![Dev-dependencies status](https://david-dm.org/uglow/cz-customizable-ghooks/dev-status.svg?theme=shields.io)](https://david-dm.org/uglow/cz-customizable-ghooks#info=devDependencies)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)


<!--[]-->
## Purpose

This package validates that a git commit message matches the rules defined in your `cz-customizable` config file (see [cz-customizable](https://github.com/leonardoanalista/cz-customizable)).
[Example commit message rules](test/fixtures/fullCommitMessageConfig.js).

## Prerequisites

- git
- Node >= 4.x
- [commitizen](https://github.com/commitizen/cz-cli)
- [cz-customizable](https://github.com/leonardoanalista/cz-customizable)
- [ghooks](https://github.com/gtramontina/ghooks)
- [ghooks](https://github.com/gtramontina/ghooks) or [husky](https://github.com/typicode/husky)

Make sure you have a git repository (`git init`) BEFORE installing ghooks, otherwise you have to take extra steps if you install ghooks before running `git init`.

## Usage
## Installation

This package is designed to be used in conjunction with `commitizen`, `cz-customizable` and either `ghooks` or `husky`.


1. Install pre-requisites (if not already installed)
```
npm i cz-customizable ghooks cz-customizable-ghooks
npm i commitizen -g
npm i cz-customizable cz-customizable-ghooks
```

Then configure your package.json:
2. Configure cz-customizable in `package.json`:
```
"config": {
"cz-customizable": {
"config": "path/to/your/cz-customizable-rules.js"
}
}
```


3. Install ONE of these git hook packages:

<details>
<summary>ghooks</summary>

1. Install ghooks:
```
npm i ghooks
```
2. Configure `package.json`:
```
// inside package.json
...
"config": {
"cz-customizable": {
"config": "path/to/your/cz-customizable-rules.js"
},
"ghooks": {
"commit-msg": "./node_modules/cz-customizable-ghooks/lib/index.js $2"
"commit-msg": "cz-customizable-ghooks $2"
}
}
...
```
</details>


<details>
<summary>husky</summary>

1. Install husky:
```
npm i ghooks
```
2. Configure `package.json`:
```
"scripts": {
"commitmsg": "cz-customizable-ghooks"
}
```
</details>


## Usage

Commit your changes to git as normal. If the commit message entered is invalid, the commit will be rejected with an error message (according to the rules specified in your cz-customizable config).
Works with git command-line and visual Git tools (such as SourceTree).
10 changes: 5 additions & 5 deletions config/verify/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# START_CONFIT_GENERATED_CONTENT
confit:
extends: &confit-extends
extends: &confit-extends
- google
- plugin:node/recommended

plugins: &confit-plugins
plugins: &confit-plugins
- node

env: &confit-env
commonjs: true # For Webpack, CommonJS
node: true
mocha: true
es6: true

globals: &confit-globals {}
parser: &confit-parser espree

Expand Down
15 changes: 10 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
'use strict';
/**
* Git COMMIT-MSG hook for validating commit message
Expand Down Expand Up @@ -200,6 +201,10 @@ function appendIssueToCommit(lines, issue) {
// hacky start if not run by jasmine :-D

function processCLI(commitMsgFileName, cb) {
if (typeof commitMsgFileName !== 'string' || !commitMsgFileName) {
throw new Error('commitMsgFileName must be the path to the "COMMIT_EDITMSG" file (usually ".git/COMMIT_EDITMSG")');
}

let incorrectLogFileName = commitMsgFileName.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');
let callback = cb || (() => {}); // Used for testing

Expand All @@ -211,23 +216,23 @@ function processCLI(commitMsgFileName, cb) {

if (!validateMessage(lines[0], msg)) {
fs.appendFile(incorrectLogFileName, msg + '\n', () => {
process.exit(1);
process.exit(1); // eslint-disable-line
callback();
});
} else {
console.info(chalk.bold.white.bgGreen('Commit message is valid.'));

// Make it optional to append the branch name to the commit message
if (!appendBranchNameToCommitMessage) {
process.exit(0);
process.exit(0); // eslint-disable-line
callback();
} else {
// If valid, add the issue/branch name to the last line
exec('git rev-parse --abbrev-ref HEAD', (err, stdout/* , stderr*/) => {
let lastline = appendIssueToCommit(lines, getIssueFromBranch(stdout));

fs.appendFile(commitMsgFileName, lastline, () => {
process.exit(0);
process.exit(0); // eslint-disable-line
callback();
});
});
Expand All @@ -239,9 +244,9 @@ function processCLI(commitMsgFileName, cb) {
// Only run this when we are not doing mocha testing
if (process.argv.join('').indexOf('mocha') === -1) {
if (!readConfigFile()) {
process.exit(1);
process.exit(1); // eslint-disable-line
}
processCLI(process.argv[2]);
processCLI(process.argv[2] || process.env.GIT_PARAMS); // GIT_PARAMS are made available when using husky
} else {
console.log('Running in mocha');
}
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "cz-customizable-ghooks",
"version": "0.0.0-semantically-released",
"description": "integrate ghooks with cz-customizable configuration",
"description": "Integrate [cz-customizable](https://github.com/leonardoanalista/cz-customizable) config with [ghooks](https://github.com/gtramontina/ghooks) or [husky](https://github.com/typicode/husky) to use a single configuration for commit message generation AND commit message validation.",
"keywords": [
"cz-customizable",
"ghooks"
"ghooks",
"husky",
"commitizen",
"commit",
"validation",
"git"
],
"homepage": "https://github.com/uglow/cz-customizable-ghooks#readme",
"bugs": {
Expand All @@ -17,6 +22,7 @@
"*.md"
],
"main": "lib/index.js",
"bin": "lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/uglow/cz-customizable-ghooks.git"
Expand Down Expand Up @@ -61,16 +67,17 @@
"devDependencies": {
"chokidar-cli": "1.2.0",
"coveralls": "2.11.15",
"cross-env": "3.1.3",
"cross-env": "3.1.4",
"cz-customizable": "4.0.0",
"eslint": "3.12.2",
"cz-customizable-ghooks": "1.4.0",
"eslint": "3.14.1",
"eslint-config-defaults": "8.0.2",
"eslint-config-google": "0.7.0",
"eslint-plugin-node": "2.0.0",
"eslint-config-google": "0.7.1",
"eslint-plugin-node": "3.0.5",
"ghooks": "1.3.2",
"istanbul": "0.4.4",
"istanbul": "0.4.5",
"mocha": "3.2.0",
"npm-run-all": "3.1.0",
"npm-run-all": "4.0.1",
"rewire": "2.5.1",
"rimraf": "2.5.4",
"semantic-release": "4.3.5"
Expand Down
13 changes: 13 additions & 0 deletions test/ccg.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ describe('cz-customizable-ghooks', () => {
assert.equal(msgPassedToValidateMessage, 'foo');
done();
}

module.processCLI(commitMsgFileName, cb);
});

Expand All @@ -476,6 +477,7 @@ describe('cz-customizable-ghooks', () => {
assert.equal(fileDataThatIsAppended, 'foo\n');
done();
}

module.processCLI(commitMsgFileName, cb);
});

Expand Down Expand Up @@ -544,4 +546,15 @@ describe('cz-customizable-ghooks', () => {
module.processCLI(commitMsgFileName, cb);
});
});

describe('processCLI()', () => {
it('should throw an exception if the commitMsgFileName is not a string', () => {
let module = require('../lib/index');
let expectedError = /Error\: commitMsgFileName must be the path to the "COMMIT_EDITMSG" file \(usually ".git\/COMMIT_EDITMSG"\)/;

assert.throws(() => module.processCLI(undefined), expectedError);
assert.throws(() => module.processCLI(null), expectedError);
assert.throws(() => module.processCLI(''), expectedError);
});
});
});

0 comments on commit fd2b69e

Please sign in to comment.