-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.js
More file actions
152 lines (144 loc) · 4.42 KB
/
cli.js
File metadata and controls
152 lines (144 loc) · 4.42 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
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
import inquirer from "inquirer";
import chalk from "chalk";
import {
commitFiles,
getStagedFiles,
checkIfRepoisGit,
getUnstagedFiles,
stageFiles
} from "./commands.js";
async function cli() {
const [isGit, stagedFiles] = await Promise.all([
checkIfRepoisGit(),
getStagedFiles()
]);
if (isGit && !stagedFiles) {
await promptToCommit();
} else if (isGit && stagedFiles) {
await addCommit();
}
}
const promptToCommit = async () => {
const files = await getUnstagedFiles();
if (files.trim().length === 0) {
console.log(chalk.bgRed("⛔️ Whops! You haven't made any changes."));
process.exit(0);
}
const styledList = files
.trim()
.split("\n")
.map((file) => {
/*
Possible States
??: Untracked
A: Added
M: Modified
D: Deleted
R: Renamed
C: Copied
U: Updated but unmerged
!: Git doesn't know about the file or folder (it's not in the repository)
*/
const fileName = file.trim();
if (fileName.startsWith("??")) return fileName.replace("??", "🆕");
if (fileName.startsWith("A")) return fileName.replace("A", "📝");
if (fileName.startsWith("M")) return fileName.replace("M", "🔄️");
if (fileName.startsWith("D")) return fileName.replace("D", "🗑️");
if (fileName.startsWith("R")) return fileName.replace("R", "🔄️");
if (fileName.startsWith("C")) return fileName.replace("C", "📝");
if (fileName.startsWith("U")) return fileName.replace("U", "⚠️");
if (fileName.startsWith("!")) return fileName.replace("!", "❓");
return fileName;
});
if (styledList.length < 1) {
console.log(chalk.bgRed("No changes made since last commit!"));
}
await inquirer
.prompt([
{
name: "list",
message:
"Select the files you want to add with, and use (CTRL + D to exit)",
type: "checkbox",
choices: styledList
}
])
.then((answer) => {
if (answer.list.length < 1) {
console.log(chalk.bgRed("⛔️ Oops, You forgot to select files"));
process.exit(0);
}
const filesToStage = answer.list.map((f) => {
if (f.startsWith("🆕")) return f.replace("🆕", "").trim();
if (f.startsWith("📝")) return f.replace("📝", "").trim();
if (f.startsWith("🔄️")) return f.replace("🔄️", "").trim();
if (f.startsWith("🗑️")) return f.replace("🗑️", "").trim();
if (f.startsWith("🔄️")) return f.replace("🔄️", "").trim();
if (f.startsWith("📝")) return f.replace("📝", "").trim();
if (f.startsWith("⚠️")) return f.replace("⚠️", "").trim();
if (f.startsWith("❓")) return f.replace("❓", "").trim();
});
stageFiles(filesToStage.join(" "));
})
.then(() => {
console.log(chalk.bgGreenBright("✅ Files Added"));
addCommit();
})
.catch(() => {
console.log(
chalk.bgRed("⛔️ Oops, that was not supposed to happen") +
chalk.bgGrey("If that happens again, please raise an issue")
);
process.exit(0);
});
};
async function addCommit() {
await inquirer
.prompt([
{
name: "type",
message: "Howdy 👋, What type of changes have you made?",
type: "list",
choices: [
"✨ feat",
"🐛 fix",
"💥 break",
"♻️ ref",
"🧪 test",
"🔖 ver",
"📝 docs",
"🎨 style",
"🛠 config",
"📦 misc"
]
},
{
name: "message",
message: "Write a commit message ✍️ : ",
type: "input"
}
])
.then((answer) => {
const message = `${answer.type}: ${answer.message}`;
console.log("\n");
if (answer.message === "") {
console.log(chalk.bgRed("⛔️ Message can't be empty"));
process.exit(0);
} else if (!answer.message.includes(" ")) {
console.log(chalk.bgRed("⛔️ Message is too short"));
process.exit(0);
}
// it includes the prefix as well
console.log(message, message.length);
if (message.length > 72) {
console.log(
chalk.bgRed("⛔️ Message is too long") +
"\n" +
chalk.bgYellow("⚠️ Please keep the message below 72 characters")
);
process.exit(0);
}
commitFiles(message);
});
}
export default cli;