-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·89 lines (79 loc) · 3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node
// run the commit till a post commit hook
const inquirer = require('inquirer');
const shell = require('shelljs');
const chalk = require('chalk');
function getPair() {
const foo = shell.exec('git about | grep "git user"', { silent: true }) || '';
return chalk.cyan(foo.split(':')[1].trim());
}
async function isCommitAmend() {
const gitDiff = await shell.exec('git diff --cached', { silent: true });
return gitDiff.trim() === '';
}
async function isInteractiveRebase() {
const branchName = await shell.exec('git branch | grep "*" | sed "s/* //"')
return branchName === '(no branch)'
}
async function shouldIRun() {
const isamend = await isCommitAmend();
const isIntRebase = await isInteractiveRebase();
return (!isamend || !isIntRebase); // neither interactive rebase nor commit amend
}
let originalPair, currentPair;
currentPair = originalPair = getPair();
if (originalPair.split(' ').includes('and')) {
currentPair = `pair ${currentPair}`;
}
let shouldICommitPrompt = {
type: 'confirm',
name: 'shouldICommit',
message: `Are you sure you want to commit as ${currentPair}?`,
};
let initialsInput = {
type: 'input',
name: 'initials',
message: 'What initial(s) would you like to use?',
validate(value = '') {
const initials = value.split(' ').filter(i => i.length === 2); // checks initials are exactly 2 characters long
const pass = initials.length === 1 || initials.length === 2; // if there is 1 or 2 sets of initials
if (pass) {
console.error('\n');
return true
} else {
console.log('\nSorry please enter valid initials.');
}
},
};
async function executeHook() {
if (await shouldIRun()) {
inquirer.prompt(shouldICommitPrompt).then((answer) => {
if (answer.shouldICommit) {
console.error('Great!');
shell.exit(0); // Success
}
inquirer.prompt(initialsInput).then((input) => {
shell.exec(`git pair ${input.initials}`);
currentPair = getPair();
let modifier = (currentPair !== originalPair) ? 'set to' : 'remains';
shell.echo(`\nGit pair ${modifier} ${currentPair}\n`);
}).then(() => {
inquirer.prompt({
type: 'confirm',
name: 'confirmPair',
message: `Proceed with pair ${currentPair}?`,
}).then((answer) => {
if (answer.confirmPair) {
const commitCommand = 'git commit --amend --reset-author --no-edit -n';
shell.echo(chalk.cyan(`${commitCommand}\n`));
shell.exec(commitCommand);
shell.exit(0); // Success
} else {
console.log('That\'s ok. Execute', chalk.cyan('npm run pairs-hook'), 'to try again.')
}
})
})
});
}
}
executeHook();