Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const initializeGitRepo = require("./lib/initializeRepo");
const initializeGitGlobal = require("./lib/globalRepos");
const emojifyCommit = require("./lib/emojifyCommit");
const getCommitMessage = require("./lib/getCommitMessage");
const writeMessage = require("./lib/writeMessage");
Expand Down
4 changes: 3 additions & 1 deletion lib/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const cosmiconfig = require("cosmiconfig");
const explorer = cosmiconfig("commoji");
const path = require("path");
const fs = require("fs");

module.exports = getConfig = () => {
const result = explorer.searchSync();
Expand All @@ -8,5 +10,5 @@ module.exports = getConfig = () => {
return result.config;
}

return explorer.loadSync("./.defaultrc.json").config;
return explorer.loadSync(path.resolve(__dirname, "../.defaultrc.json")).config;
};
102 changes: 102 additions & 0 deletions lib/globalRepos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const { exec } = require('child_process');
const homedir = require('os').homedir();

// Takes the commit message and runs it through commoji
const commojiHook = `#!/bin/sh

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it wouldn't work if the user isn't at the root of a project.
You might want to use git rev-parse --show-toplevel of $PWD to get the .git directory of a project.

prepareCommitMsgPath="$PWD/.git/hooks/prepare-commit-msg"
preCommitPath="$PWD/.git/hooks/pre-commit"

if [ -f "$preCommitPath" ]
then
$preCommitPath
fi

if [ -f "$prepareCommitMsgPath" ]
then
$prepareCommitMsgPath $1
fi

exec < /dev/tty
commoji $1
`;
// Where the global hook will be located
const gitHooksPath = "/usr/local/share/git-core/templates/hooks";
// Command to run to set up global hooks path (git 2.9)
const cmdConfigCoreHooksPath = "git config --global core.hooksPath";
// Path where prepare-commit-msg will reside (has our commojiHook)
const prepareMessageHook = path.join(gitHooksPath, "prepare-commit-msg");

/**
*/

module.exports = globalRepos = async () => {
let hookExists;

try {
// Synchronously test the user's permissions for the file
// F_OK: Constant for fs.access(). File is visible to the calling process
fs.accessSync(gitHooksPath, fs.constants.F_OK);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest against checking a fixed path for git templates. Many users reinstall git on macos to update from the built-in version of git.

Instead, running git config --global core.hooksPath without arguments would return a pre-existing hooks path if there is one already defined and used:

image

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.hooksPath doesn't exist on certain versions of git, the given gitHooksPath is the default one where hooks are made from what I understood, we can tell the user to specify it.

hookExists = true;
} catch(_) {
hookExists = false;
}

if (hookExists) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question(`
A prepare-commit-msg hook already exists at ${gitHooksPath}.
Would you like to overwite the existing hook? (y/N)
`, async (answer) => {
// Exit if user does not want to override their hook
if (!answer.match(/^(y|yes)$/gi)) {
rl.close();
return;
}

// Close readline
rl.close();

// Create folders where hook will reside if does not exist
await exec(`mkdir -p ${gitHooksPath}`, err => {
if (err) {
console.log(`Unable to make folders ${gitHooksPath}: ${err}`);
return;
}
});

// Create prepare-commit-msg file if does not exist
await exec(`touch ${prepareMessageHook}`, err => {
if (err) {
console.log(`Unable to create file ${prepareMessageHook}: ${err}`);
return;
}
});

await exec(`chmod +x ${prepareMessageHook}`, err => {
if (err) {
console.log(`Unable to add executionable permission to ${prepareMessageHook}`)
return;
}
});

// Write hook to folder (replace any existing)
fs.writeFileSync(prepareMessageHook, commojiHook, {mode: 0o777});

// Set up global config so commit will look at that hook by default (git 2.9)
await exec(`${cmdConfigCoreHooksPath} ${gitHooksPath}`, err => {
if (err) {
console.log(`Unable to set up global core.hooks_path '${cmdConfigCoreHooksPath} ${gitHooksPath}': ${err}`);
return;
}
console.log("✨ Successfully installed commoji globally 🌏 ! You can now commit as you normally would.")
});
});
}
}
1 change: 1 addition & 0 deletions lib/initializeRepo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const path = require("path");

// Runs it with the location of the commit message
const commojiHook = `#!/bin/sh
exec < /dev/tty
commoji $1
Expand Down