forked from JPeer264/node-semantic-git-commit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·63 lines (54 loc) · 1.9 KB
/
Copy pathcli.js
File metadata and controls
executable file
·63 lines (54 loc) · 1.9 KB
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
#!/usr/bin/env node
import chalk from 'chalk';
import commitCount from 'git-commit-count';
import execa from 'execa';
import inquirer from 'inquirer';
import isAdded from 'is-git-added';
import isGit from 'is-git-repository';
import updateNotifier from 'update-notifier';
import yargs from 'yargs';
import getConfig from './getConfig';
import combineTypeScope from './helpers/combineTypeScope';
import pkg from '../package.json';
import questions, {
initMessage,
initQuestion,
} from './questions';
const config = getConfig();
const questionsList = questions(config);
const question = initQuestion(config);
const gitCommitExeca = message => (
execa('git', ['commit', '-m', message], { stdio: 'inherit' })
.catch(() => {
console.error(chalk.red('\nAn error occured. Try to resolve the previous error and run following commit message again:'));
console.error(chalk.green(`git commit -m "${message}"`));
})
);
const argv = yargs
.usage('Usage: $0')
.alias('v', 'version')
.describe('v', 'Version number')
.help('h')
.alias('h', 'help')
.argv;
updateNotifier({ pkg }).notify();
if (argv.v) {
console.log(`v${pkg.version}`);
} else if (!isGit()) {
console.error('fatal: Not a git repository (or any of the parent directories): .git');
} else if (!isAdded()) {
console.error(chalk.red('Please', chalk.bold('git add'), 'some files first before you commit.'));
} else if (commitCount() === 0 &&
typeof config.initialCommit === 'object' &&
config.initialCommit.isEnabled) {
const message = initMessage(config);
inquirer.prompt(question).then(answers => (
answers.initCommit ? gitCommitExeca(message) : undefined
));
} else {
inquirer.prompt(questionsList).then((answers) => {
const typeScope = combineTypeScope(answers.type, answers.scope);
const message = answers.body ? `${answers.editor}` : `${typeScope} ${answers.description}`;
return gitCommitExeca(message);
});
}