Skip to content

Commit

Permalink
feat: init command add commit message hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Bengt Brodersen committed Jan 27, 2023
1 parent 8c58449 commit 6db327d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## **2.5.0** <sub><sup>2023-01-27 ([5d6f92f...5d6f92f](https://github.com/qoomon/git-conventional-commits/compare/5d6f92f...5d6f92f?diff=split))</sup></sub>

### Features
* init command add commit message hook ([5d6f92f](https://github.com/qoomon/git-conventional-commits/commit/5d6f92f))

## **2.4.1** <sub><sup>2023-01-17 ([ea1c647...ea1c647](https://github.com/qoomon/git-conventional-commits/compare/ea1c647...ea1c647?diff=split))</sup></sub>

### Bug Fixes
Expand Down
8 changes: 8 additions & 0 deletions commit-msg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

if command -v git-conventional-commits > /dev/null 2>&1
then
# fix for windows systems
PATH="/c/Program Files/nodejs:$HOME/AppData/Roaming/npm/:$PATH"
git-conventional-commits commit-msg-hook "$1"
fi
24 changes: 16 additions & 8 deletions lib/commands/commandInit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const Config = require("./config");
const fs = require("fs");

const gitCommitMsgHookDefaultPath = './.git/hooks/commit-msg'
const gitCommitMsgHookTemplatePath = `${__dirname}/../../commit-msg.sh`;
const LOG_WARN = '\x1b[1m\x1b[33m[WARN]\x1b[0m';

exports.command = 'init'
exports.desc = `Creates config file template ${Config.defaultPath}`
exports.desc = `Creates config file and commit message hook`
exports.builder = function (yargs) {
yargs.option('config', {
alias: 'c',
Expand All @@ -13,13 +17,17 @@ exports.builder = function (yargs) {
}

exports.handler = async function (argv) {
try {
if (!fs.existsSync(argv.config)) {
console.info(`Creating config file ${argv.config}.`)
fs.copyFileSync(Config.templatePath, argv.config, fs.constants.COPYFILE_EXCL);
} catch (e) {
if (e.syscall === 'copyfile' && e.code === 'EEXIST') {
throw Error(`Config file '${argv.config}' already exists.`);
} else {
throw e;
}
} else {
console.warn(`${LOG_WARN} Could not creat config file ${argv.config}, because it already exists.`)
}

if (!fs.existsSync(gitCommitMsgHookDefaultPath)) {
console.info(`Creating commit message hook ${gitCommitMsgHookDefaultPath}.`)
fs.copyFileSync(gitCommitMsgHookTemplatePath, gitCommitMsgHookDefaultPath, fs.constants.COPYFILE_EXCL);
} else {
console.warn(`${LOG_WARN} Could not create message hook ${gitCommitMsgHookDefaultPath}, because it already exists.`)
}
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-conventional-commits",
"version": "2.4.1",
"version": "2.5.0",
"description": "git conventional commits util",
"licence": "GPLv3",
"main": "cli.js",
Expand Down

0 comments on commit 6db327d

Please sign in to comment.