From 80176cd3735c6a8988335964cfb6dbbaccce4703 Mon Sep 17 00:00:00 2001 From: Alexander Bolotskov Date: Tue, 10 Dec 2019 21:30:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20adds=20git=20hooks=20sup?= =?UTF-8?q?port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .eslintrc.json | 1 + lib/cli.js | 16 +++++++++++++++- package.json | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index e101d1e5..cb6cf5dc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,6 +3,7 @@ "rules": { "import/unambiguous": "off", "import/no-commonjs": "off", + "no-process-env": "off", "promise/prefer-await-to-then": "off", "strict": "off" } diff --git a/lib/cli.js b/lib/cli.js index a6441294..6c97abdc 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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) => { @@ -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 @@ -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 diff --git a/package.json b/package.json index 576f24cb..a9d5b9c8 100644 --- a/package.json +++ b/package.json @@ -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": {