-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·196 lines (179 loc) · 5.7 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
const { exec, execSync } = require('child_process');
const colors = require('colors');
const { log, clear } = require('console');
const readlineSync = require('readline-sync');
const readline = require('readline');
const prompt = require('prompt-sync')();
let message = '';
const choices = [
{
name: 'build: Changes that affect the build system or external dependencies',
value: 'build',
},
{
name: 'ci: Changes to our CI configuration files and scripts',
value: 'ci',
},
{
name: 'chore: Update tasks that do not cause code changes',
value: 'chore',
},
{ name: 'docs: Documentation only changes', value: 'docs' },
{ name: 'feat: A new feature', value: 'feat' },
{ name: 'fix: A bug fix', value: 'fix' },
{
name: 'improvement: Improves a current implementation without adding a new feature or fixing a bug',
value: 'improvement',
},
{ name: 'perf: A code change that improves performance', value: 'perf' },
{
name: 'refactor: A code change that neither fixes a bug nor adds a feature',
value: 'refactor',
},
{ name: 'revert: Reverts a previous commit', value: 'revert' },
{
name: 'style: Changes that do not affect the meaning of the code',
value: 'style',
},
{
name: 'test: Adding missing tests or correcting existing tests',
value: 'test',
},
{ name: "other: Doesn't fit any of the suggested types?", value: 'other' },
];
const listFilesCommand = process.platform === 'win32' ? 'dir' : 'ls -a';
const sleep = (milliseconds) => {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
};
handleGitIgnoreFile = () => {
exec(`touch .gitignore`, (error, stdout, stderr) => {
if (error) {
return;
}
});
exec(`echo "node_modules/" >> .gitignore`, (error, stdout, stderr) => {
if (error) {
return;
}
log('\n Created .gitignore file'.yellow.bold);
});
};
const isGitRepo = execSync(listFilesCommand, {
encoding: 'utf-8',
}).includes('.git');
if (!isGitRepo) {
log('No git repository found'.red.bold);
sleep(200);
log('Initializing git repository'.yellow.bold);
sleep(200);
exec(`git init`, (error, stdout, stderr) => {
if (error) {
log('Something went wrong. Please try again.'.red);
return;
}
log('\n Initialized git repository'.yellow.bold);
});
}
const flag = execSync(listFilesCommand, { encoding: 'utf-8' }).includes(
'.gitignore'
);
if (!flag) {
log('No .gitignore file found'.red.bold);
sleep(200);
log('Creating .gitignore file'.yellow.bold);
sleep(200);
log('Adding node_modules/ to .gitignore'.yellow.bold);
sleep(200);
readlineSync.question('Press enter to continue...'.yellow.bold);
handleGitIgnoreFile();
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let selectedIndex = 0;
const displayChoices = () => {
clear();
log('Select the type of commit you want to make:'.inverse);
log('Use arrow keys to navigate. Press enter to select.'.inverse);
choices.forEach((c, idx) => {
if (idx === selectedIndex) {
console.log(
colors.bgYellow.bold.green(`
> ${idx + 1}. ${c.name} `)
);
}
});
};
displayChoices();
const handleUnStagedFiles = (files) => {
execSync(`git add .`);
execSync(`git commit -m "${message}"`);
log(`Commit successful`.green.bold);
};
const commit = () => {
exec(`git commit -m "${message}"`, (error, stdout, stderr) => {
if (error) {
if (error.code === 1) {
log('You have un staged files.'.red);
let color = execSync('git status --porcelain', { encoding: 'utf-8' });
color = color.split(' ');
color = color.map((c) => c.replace('M', ' M '.white.bgYellow.bold));
color = color.map((c) => c.replace('??', ' ?? '.white.bgRed.bold));
color = color.map((c) => c.replace('A', ' A '.white.bgGreen.bold));
color = color.map((c) => c.replace('D', ' D '.white.bgRed.bold));
color = color.map((c) => c.replace('R', ' R '.white.bgBlue.bold));
log(color.join(' '));
log(` Would you like to add them? (y/n)`.red);
const answer = readlineSync.question('y/n: ', {
limit: ['y', 'n'],
limitMessage: 'Please enter y or n',
hideEchoBack: false,
});
if (answer === 'y') {
handleUnStagedFiles();
} else {
log('Exiting...'.red);
log('Please stage your files and try again.'.red);
log('use: git add <file-name>'.red);
process.exit(0);
}
}
} else log(`Commit successful`.green.bold);
rl.close();
});
};
rl.input.on('keypress', (_, key) => {
if (key.name === 'up') {
selectedIndex = Math.max(selectedIndex - 1, 0);
displayChoices();
} else if (key.name === 'down') {
selectedIndex = Math.min(selectedIndex + 1, choices.length - 1);
displayChoices();
} else if (key.name === 'return') {
const choice = choices[selectedIndex];
console.log(`You selected: ${choice.name}`);
message += choice.value;
const scope = prompt(`Enter the `+ `scope `.blue.bold +`of the work you done: `);
message += `(${scope}): `;
const description = prompt('Enter a description of the work you done: ');
message += `${description}`;
const issueNumber = prompt('Enter the issue number:(leave blank if none)');
if (Number.isInteger(issueNumber)) {
message += ` Relates #${issueNumber}`;
if (readlineSync.keyInYN('Do you want to close the issue?')) {
message += `
Closed #${issueNumber}`;
}
}
log(`\n Your commit message is: `.yellow + `${message}`.green);
if (readlineSync.keyInYN('Do you want to commit?')) {
commit();
}
}
});