-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·100 lines (82 loc) · 2.99 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
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
const commitMessageChercher = require('./src/commit-message-checker/commit-message-checker')
const shortCut = require('./src/gitShortCut');
const messageComposer = require('./src/message-composer/message-composer')
const chalk = require('chalk');
const argv = require('yargs')
.alias('c', 'checkCommit')
.alias('o', 'output')
.alias('p', 'prettyPrint')
.alias('a', 'addAll')
.alias('m', 'messageComposer')
.alias('t', 'test')
.alias('l', 'last')
.alias('g', 'groupBy')
.alias('bu', 'bump')
.alias('b', 'branch')
.alias('re', 'rePrepare')
.argv
const { prettyPrint, write } = require('./src/commit-prettier/commit-prettier');
// Check Last commit
if (argv.checkCommit) {
const numberOfCommitToCheck = argv.last || 1;
for (var i = 1; i <= numberOfCommitToCheck; i++) {
const commitMessage = shortCut.messageLastCommit(i);
const commitHash = shortCut.hashLastCommit(i);
const isValid = commitMessageChercher(commitMessage);
console.log(chalk.bgGray.white(`### START ANALYZING`))
console.log(chalk.black(`### hash:${commitHash}`))
if (isValid) {
console.log(chalk.green(commitMessage))
console.log(chalk.green('commit message is valid'));
} else {
console.log(chalk.red(commitMessage))
console.log(chalk.red('commit message is NOT valid'));
throw new Error()
}
console.log('')
}
}
if (argv.bump) {
console.log(argv.bump);
}
if (argv.prettyPrint) {
console.log('Fetching tags from origin');
shortCut.ex("git fetch origin --tags");
const groupBy = argv.groupBy === 'scope' ? 'scope' : 'type';
const tags = shortCut.getTagsOrderedByDate().split('\n').reverse();
const numOftag = parseInt(argv.tag);
const getFromTag = (tagNumber) => tags[+tagNumber];
const commitFrom = argv.tag ? getFromTag(numOftag) : shortCut.firstCommitAllTime().trim();
let title = `From ${getFromTag(numOftag)} to ${tags[0]}`;
const filters = argv.filters ? argv.filters.split('|') : undefined;
prettyPrint(commitFrom, groupBy, argv.output, title, filters);
if (argv.addAll) {
console.log('Adding files to previous commit');
shortCut.ex('git add .')
shortCut.ex('git commit --amend --no-edit')
}
}
if (argv.messageComposer) {
const isTest = argv.test || false;
if (isTest) {
console.log("exectued as test. No commit will be executed");
}
if (argv.addAll) {
messageComposer(isTest, true);
} else {
messageComposer(isTest, false);
}
}
if (argv.force) {
const branch = argv.branch || 'main';
shortCut.ex('git add .', true)
shortCut.ex('git commit --amend --no-edit', true);
shortCut.ex('git fetch ', true);
shortCut.ex(`git pull origin ${branch} --rebase`, true);
shortCut.ex('git push origin HEAD --force', true);
}
if (argv.cane) {
shortCut.ex('git add .', true)
shortCut.ex('git commit --amend --no-edit', true)
}