Skip to content
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

✨ Add possibility to remove hook #53

Merged
merged 5 commits into from
Jun 17, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const cli = meow(`
$ gitmoji
Options
--init, -i Initialize gitmoji as a commit hook
--remove -r Remove a previously initialized commit hook
--config, -g Setup gitmoji-cli preferences.
--commit, -c Interactively commit using the prompts
--list, -l List all the available gitmojis
Expand All @@ -26,6 +27,7 @@ const cli = meow(`
`, {
alias: {
i: 'init',
r: 'remove',
c: 'commit',
l: 'list',
s: 'search',
Expand All @@ -50,6 +52,7 @@ const commands = {
config: () => gitmojiCli.config(),
search: () => cli.input.map(element => gitmojiCli.search(element)),
init: () => gitmojiCli.init(),
remove: () => gitmojiCli.remove(),
hook: () => gitmojiCli.ask('hook'),
version: () => console.log(gitmojiCli.version(pkg.version)),
commit: () => gitmojiCli.ask('client'),
Expand Down
23 changes: 19 additions & 4 deletions src/gitmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))

const config = new Conf();

const commitHookPath = `${process.env.PWD}/.git/hooks/prepare-commit-msg`;

class GitmojiCli {

constructor(gitmojiApiClient, gitmojis) {
Expand Down Expand Up @@ -56,12 +58,10 @@ class GitmojiCli {
}

init() {
const hookFile = 'prepare-commit-msg';
const path = `${process.env.PWD}/.git/hooks/${hookFile}`;
const fileContents = `#!/bin/sh\n# gitmoji as a commit hook\nexec < /dev/tty\ngitmoji --hook $1`;

if (this._isAGitRepo('.git')) {
fs.writeFile(path, fileContents, {mode: 0o775}, err => {
fs.writeFile(commitHookPath, fileContents, {mode: 0o775}, err => {
if (err) {
this._errorMessage(err);
}
Expand All @@ -70,11 +70,26 @@ class GitmojiCli {
}

return {
path,
commitHookPath,
fileContents
};
}

remove() {
if (this._isAGitRepo('.git')) {
fs.unlink(commitHookPath, err => {
if (err) {
this._errorMessage(err);
}
console.log(`${chalk.yellow('gitmoji')} commit hook unlinked successfully.`);
});
}

return {
commitHookPath
};
}

version(number) {
return number;
}
Expand Down
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ describe('gitmoji', function() {

describe('init', function() {
it('path should be set to .git/hooks/prepare-commit-msg', function() {
gitmojiCli.init().path.should.containEql('.git/hooks/prepare-commit-msg');
gitmojiCli.init().commitHookPath.should.containEql('.git/hooks/prepare-commit-msg');
});

it('prepare-commit-msg should contain the hook script', function() {
gitmojiCli.init().fileContents.should.containEql('exec < /dev/tty\ngitmoji --hook $1');
});
});

describe('remove', function() {
it('path should be set to .git/hooks/prepare-commit-msg', function() {
gitmojiCli.remove().commitHookPath.should.containEql('.git/hooks/prepare-commit-msg');
});
});

describe('version', function() {
it('should return the version number equal to the package.json one', function() {
gitmojiCli.version(pkg.version).should.be.equal(pkg.version);
Expand Down