From 6db327dd153579ddfeb57ae332313bceffc9f921 Mon Sep 17 00:00:00 2001 From: Bengt Brodersen Date: Fri, 27 Jan 2023 15:39:12 +0100 Subject: [PATCH] feat: init command add commit message hook --- CHANGELOG.md | 5 +++++ commit-msg.sh | 8 ++++++++ lib/commands/commandInit.js | 24 ++++++++++++++++-------- package-lock.json | 16 ++++++++-------- package.json | 2 +- 5 files changed, 38 insertions(+), 17 deletions(-) create mode 100755 commit-msg.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ff5370..ed1541d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## **2.5.0** 2023-01-27 ([5d6f92f...5d6f92f](https://github.com/qoomon/git-conventional-commits/compare/5d6f92f...5d6f92f?diff=split)) + +### Features +* init command add commit message hook ([5d6f92f](https://github.com/qoomon/git-conventional-commits/commit/5d6f92f)) + ## **2.4.1** 2023-01-17 ([ea1c647...ea1c647](https://github.com/qoomon/git-conventional-commits/compare/ea1c647...ea1c647?diff=split)) ### Bug Fixes diff --git a/commit-msg.sh b/commit-msg.sh new file mode 100755 index 0000000..8fec0a6 --- /dev/null +++ b/commit-msg.sh @@ -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 \ No newline at end of file diff --git a/lib/commands/commandInit.js b/lib/commands/commandInit.js index 67bbfb6..a7ac50c 100644 --- a/lib/commands/commandInit.js +++ b/lib/commands/commandInit.js @@ -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', @@ -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.`) } } diff --git a/package-lock.json b/package-lock.json index 488a4e9..3003f97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-conventional-commits", - "version": "2.4.1", + "version": "2.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "git-conventional-commits", - "version": "2.4.1", + "version": "2.5.0", "dependencies": { "yaml": "^2.1.3", "yargs": "^17.6.2" @@ -2545,9 +2545,9 @@ "dev": true }, "node_modules/json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "bin": { "json5": "lib/cli.js" @@ -5389,9 +5389,9 @@ "dev": true }, "json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, "kleur": { diff --git a/package.json b/package.json index f845cb6..ece3b2a 100644 --- a/package.json +++ b/package.json @@ -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",