Skip to content

Commit

Permalink
feat: 🎸 adds git hooks support
Browse files Browse the repository at this point in the history
To run git-cz on `prepare-commit-msg` you can basically follow this
instruction
(https://github.com/commitizen/cz-cli#optional-running-commitizen-on-git-commit)
but usestreamich's `git-cz` instead of `cz-conventional-changelog`.

✅ Closes: #79
  • Loading branch information
Alexander Bolotskov committed Dec 10, 2019
1 parent abc8479 commit 80176cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rules": {
"import/unambiguous": "off",
"import/no-commonjs": "off",
"no-process-env": "off",
"promise/prefer-await-to-then": "off",
"strict": "off"
}
Expand Down
16 changes: 15 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const {spawn} = require('child_process');
const fs = require('fs');
const shellescape = require('any-shell-escape');
const minimist = require('minimist');
const signale = require('signale');
const createState = require('./createState');
const runInteractiveQuestions = require('./runInteractiveQuestions');
const formatCommitMessage = require('./formatCommitMessage');
const getGitRootDir = require('./util/getGitRootDir');

// eslint-disable-next-line no-process-env
const executeCommand = (command, args = [], env = process.env) => {
Expand All @@ -31,7 +33,7 @@ const main = async () => {
params[arg] = true;
}

const {'dry-run': isDryRun, ...passThroughParams} = params;
const {'dry-run': isDryRun, hook: isHook, ...passThroughParams} = params;

if (isDryRun) {
// eslint-disable-next-line no-console
Expand All @@ -42,6 +44,18 @@ const main = async () => {

const message = formatCommitMessage(state);

/**
* @author https://github.com/oxyii
* @see https://github.com/streamich/git-cz/issues/79
*/
if (isHook) {
const commitMsgFile = getGitRootDir() + '/.git/COMMIT_EDITMSG';

fs.writeFileSync(commitMsgFile, message);
// eslint-disable-next-line no-process-exit
process.exit(0);
}

const appendedArgs = [];

// eslint-disable-next-line guard-for-in
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"husky": {
"hooks": {
"pre-commit": "yarn lint",
"prepare-commit-msg": "exec < /dev/tty && node ./bin/git-cz.js || true"
"prepare-commit-msg": "exec < /dev/tty && node ./bin/git-cz.js --hook || true"
}
},
"config": {
Expand Down

0 comments on commit 80176cd

Please sign in to comment.