Skip to content

Commit 3dc39c7

Browse files
yashk2000Preet Shah
authored andcommitted
✨ cli: Add cli commands for config and startup
Convert the project into a module that can be installed globally using npm Add configuration for the module to be used as a cli app using the command gg Add fuctionality for gg start and gg config
1 parent 7fa66a1 commit 3dc39c7

File tree

6 files changed

+179
-39
lines changed

6 files changed

+179
-39
lines changed

bin/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
3+
const clear = require('clear');
4+
const figlet = require('figlet');
5+
const cowsay = require('cowsay');
6+
const files = require('../lib/files.js');
7+
const program = require('commander');
8+
const { getQuestions, getConfigQuestions } = require('../lib/inquirer.js');
9+
10+
clear();
11+
12+
program
13+
.command('start')
14+
.alias('s')
15+
.action(function () {
16+
// displays Gitg0 on start
17+
if (files.directoryExists('.git')) {
18+
console.log(figlet.textSync('Gitg0', {
19+
horizontalLayout: 'default',
20+
verticalLayout: 'default',
21+
}), '\n');
22+
// asks task based questions
23+
getQuestions();
24+
} else {
25+
// checks if the directory is a git based repo or not
26+
console.log(cowsay.say({
27+
text: 'Not a git repository!',
28+
T: 'U '
29+
}
30+
));
31+
process.exit();
32+
}
33+
});
34+
35+
program
36+
.command('config')
37+
.alias('c')
38+
.action(function () {
39+
// displays Gitg0 on start
40+
if (files.directoryExists('.git')) {
41+
console.log(figlet.textSync('Gitg0', {
42+
horizontalLayout: 'default',
43+
verticalLayout: 'default',
44+
}), '\n');
45+
// asks task based questions
46+
getConfigQuestions();
47+
} else {
48+
// checks if the directory is a git based repo or not
49+
console.log(cowsay.say({
50+
text: 'Not a git repository!',
51+
T: 'U '
52+
}
53+
));
54+
process.exit();
55+
}
56+
});
57+
58+
program.parse(process.argv);

index.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/funcs/generate.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,3 @@ function remove_stopwords(str) {
9191
}
9292
return (res.join(' '))
9393
}
94-
95-
96-
branchName();
97-
suggestCommitMsg();
98-
suggestCommitMsg();
99-
suggestCommitMsg();
100-
101-
102-
103-
104-
105-

lib/inquirer.js

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var inquirer = require('inquirer');
2-
var emoji = require('node-emoji')
2+
var emoji = require('node-emoji');
3+
const fs = require('fs');
34
const { getIssue } = require('./issue.js');
5+
const { jsonReader } = require('./funcs/jsonReader.js');
46

57
module.exports = {
68
getQuestions: async () => {
@@ -49,5 +51,114 @@ module.exports = {
4951
// Something else went wrong
5052
}
5153
});
54+
},
55+
56+
getConfigQuestions: async () => {
57+
58+
inquirer.prompt([{
59+
type: 'list',
60+
message: 'What commit guidelines do you follow?',
61+
name: 'guidelines',
62+
choices: [
63+
`fix:, feat:, chore:`,
64+
`fix #`,
65+
`...(fix #)`,
66+
`Type your own, if multiple, separate with commas`,
67+
]
68+
}]).then(
69+
ans => {
70+
if (ans['guidelines'] === "Type your own, if multiple, separate with commas") {
71+
inquirer.prompt([
72+
{
73+
// expects issue number as response
74+
type: 'string', message: 'Type your own guidelines, if multiple, separate with commas\n', name: 'commit'
75+
}
76+
]).then(
77+
ans1 => {
78+
jsonReader('./.gitgo', (err, conf) => {
79+
if (err) {
80+
console.log('Error reading file:', err)
81+
return
82+
}
83+
conf.commit_guidelines = ans1['commit'].split(',');
84+
fs.writeFile('./.gitgo', JSON.stringify(conf), (err) => {
85+
if (err) console.log('Error writing file:', err)
86+
})
87+
})
88+
inquirer.prompt([
89+
{
90+
// expects issue number as response
91+
type: 'string', message: 'Would you be using emojis in your commit messages?(y/n)', name: 'emojis'
92+
}
93+
]).then(
94+
ans2 => {
95+
var emo = true;
96+
if (ans2['emojis'] === "y") {
97+
console.log("\nWe have an awesome set of emojis in the .gitgo file which will be suggested to you in the commit messages.\n")
98+
} else {
99+
emo = false;
100+
}
101+
jsonReader('./.gitgo', (err, conf) => {
102+
if (err) {
103+
console.log('Error reading file:', err)
104+
return
105+
}
106+
conf.use_emojis = emo;
107+
fs.writeFile('./.gitgo', JSON.stringify(conf), (err) => {
108+
if (err) console.log('Error writing file:', err)
109+
})
110+
})
111+
console.log("\nSettings for your repo have been stored. Run gg start before working on an issue to get the branch name and commit title automatically. If you would like to change any settings manually, please edit the .gitgo file.\n")
112+
}
113+
)
114+
}
115+
)
116+
} else {
117+
jsonReader('./.gitgo', (err, conf) => {
118+
if (err) {
119+
console.log('Error reading file:', err)
120+
return
121+
}
122+
conf.commit_guidelines = ans['guidelines'].split(',');
123+
fs.writeFile('./.gitgo', JSON.stringify(conf), (err) => {
124+
if (err) console.log('Error writing file:', err)
125+
})
126+
})
127+
inquirer.prompt([
128+
{
129+
// expects issue number as response
130+
type: 'string', message: 'Would you be using emojis in your commit messages?(y/n)', name: 'emojis'
131+
}
132+
]).then(
133+
ans2 => {
134+
var emo = true;
135+
if (ans2['emojis'] === "y") {
136+
console.log("\nWe have an awesome set of emojis in the .gitgo file which will be suggested to you in the commit messages.\n")
137+
} else {
138+
emo = false;
139+
}
140+
jsonReader('./.gitgo', (err, conf) => {
141+
if (err) {
142+
console.log('Error reading file:', err)
143+
return
144+
}
145+
conf.use_emojis = emo;
146+
fs.writeFile('./.gitgo', JSON.stringify(conf), (err) => {
147+
if (err) console.log('Error writing file:', err)
148+
})
149+
})
150+
console.log("\nSettings for your repo have been stored. Run gg start before working on an issue to get the branch name and commit title automatically. If you would like to change any settings manually, please edit the .gitgo file.\n")
151+
}
152+
)
153+
}
154+
}
155+
)
156+
.catch(error => {
157+
if (error.isTtyError) {
158+
// Prompt couldn't render in the current environment
159+
} else {
160+
// Something else went wrong
161+
}
162+
});
52163
}
53164
};

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "gitg0",
33
"version": "1.0.0",
44
"description": "a magnificent tool to auto-suggest everything you need before pushing a git commit.",
5-
"main": "index.js",
5+
"bin": {
6+
"gg": "./bin/index.js"
7+
},
68
"scripts": {
79
"test": "echo \"Error: no test specified\" && exit 1"
810
},
@@ -24,6 +26,7 @@
2426
"axios": "^0.21.0",
2527
"chalk": "^4.1.0",
2628
"clear": "^0.1.0",
29+
"commander": "^6.2.0",
2730
"cowsay": "^1.4.0",
2831
"figlet": "^1.5.0",
2932
"git-remote-origin-url": "^3.1.0",

0 commit comments

Comments
 (0)