diff --git a/package.json b/package.json index 8a06070..8237a09 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "git-commit-msg-validator", - "version": "1.1.2", + "version": "1.1.3", "description": "This is a simple git commit message validator.", "author": "kokocan12 ", "license": "MIT", diff --git a/src/git-commit-msg-validator.js b/src/git-commit-msg-validator.js index 64395ce..ca755a0 100644 --- a/src/git-commit-msg-validator.js +++ b/src/git-commit-msg-validator.js @@ -6,7 +6,8 @@ const prettier = require('prettier'); const START_TEXT = "# GIT_COMMIT_MSG_VALIDATOR_START"; const END_TEXT = "# GIT_COMMIT_MSG_VALIDATOR_END"; const RUN_HOOK = "./node_modules/.bin/git-commit-msg-validator-run-hook"; -const VERSION = "1.1.2" +const SHE_BANG = "#!/bin/sh"; +const VERSION = "1.1.3" function getPackageJson(projectPath = process.cwd()) { if (typeof projectPath !== "string") { @@ -131,11 +132,16 @@ function setHooks() { const HOOK_TEXT = "\n" + START_TEXT + "\n" + RUN_HOOK + "\n" + END_TEXT + "\n"; if(fs.existsSync(commitMsgFilePath)) { - const commitMsgContent = fs.readFileSync(commitMsgFilePath, {encoding: "utf-8"}); + let commitMsgContent = fs.readFileSync(commitMsgFilePath, {encoding: "utf-8"}); const startIdx = commitMsgContent.indexOf(START_TEXT); const endIdx = commitMsgContent.indexOf(END_TEXT); + // If there is no SHE_BANG. + if(commitMsgContent.indexOf(SHE_BANG) === -1) { + commitMsgContent = SHE_BANG + '\n' + commitMsgContent; + } + if(startIdx !== -1 && endIdx !== -1) { const innerText = commitMsgContent.slice(startIdx + START_TEXT.length, endIdx); const newContent = commitMsgContent.replace(innerText, "\n" + RUN_HOOK + "\n"); @@ -144,7 +150,7 @@ function setHooks() { fs.writeFileSync(commitMsgFilePath, commitMsgContent + HOOK_TEXT, {encoding: 'utf-8'}); } } else { - fs.writeFileSync(commitMsgFilePath, HOOK_TEXT, {encoding: 'utf-8'}); + fs.writeFileSync(commitMsgFilePath, SHE_BANG + HOOK_TEXT, {encoding: 'utf-8'}); } fs.chmodSync(commitMsgFilePath, 0o0755);