-
Notifications
You must be signed in to change notification settings - Fork 0
✨🌏Add global #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
✨🌏Add global #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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.") | ||
| }); | ||
| }); | ||
| } | ||
| } | ||

There was a problem hiding this comment.
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-toplevelof $PWD to get the .git directory of a project.