Skip to content

Commit

Permalink
Fix error in window, add SHE_BANG
Browse files Browse the repository at this point in the history
  • Loading branch information
kokocan12 committed Jun 29, 2022
1 parent 26f66c4 commit b60e1d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <dlgudwnsl1234@gmail.com>",
"license": "MIT",
Expand Down
12 changes: 9 additions & 3 deletions src/git-commit-msg-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit b60e1d1

Please # to comment.