Skip to content

Commit ddddfcf

Browse files
committed
cli: refactor logo log and typed file
1 parent f995c46 commit ddddfcf

File tree

5 files changed

+84
-79
lines changed

5 files changed

+84
-79
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"name": "gitg0",
33
"version": "1.0.1",
44
"description": "a magnificent tool to auto-suggest everything you need before pushing a git commit.",
5-
"main": "dist/index.cjs.js",
6-
"module": "dist/index.esm.js",
5+
"main": "dist/bin/index.js",
76
"files": [
87
"dist"
98
],
109
"types": "dist/index.d.ts",
1110
"bin": {
12-
"gtg": "./dist/index.cjs.js"
11+
"gtg": "./dist/bin/index.js"
1312
},
1413
"scripts": {
1514
"test": "jest",

src/bin/index.ts

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
const clear = require("clear");
2-
const figlet = require("figlet");
32
const cowsay = require("cowsay");
43
const files = require("../lib/files.js");
54
const program = require("commander");
65
const { getQuestions, getConfigQuestions, displaySuggestions } = require("../lib/inquirer.js");
7-
const simpleGit = require("simple-git");
8-
const git = simpleGit();
6+
const logLogo = require("../lib/funcs/logLogo");
7+
const git = require("simple-git")();
98
const { jsonReader } = require("../lib/funcs/jsonReader.js");
109
const version = require("../../package.json");
1110
const chalk = require("chalk");
1211
const { exec } = require("child_process");
1312
const fs = require("fs");
13+
import { GitGoConf } from "../types";
1414

1515
clear();
1616

1717
program
1818
.command("start")
1919
.alias("s")
20-
.action(function() {
20+
.action(() => {
2121
// displays Gitg0 on start
2222
if (files.directoryExists(".git")) {
23-
console.log(
24-
figlet.textSync("Gitg0", {
25-
horizontalLayout: "default",
26-
verticalLayout: "default",
27-
}),
28-
"\n"
29-
);
23+
logLogo();
3024
getQuestions();
3125
} else {
3226
// checks if the directory is a git based repo or not
@@ -43,21 +37,15 @@ program
4337
program
4438
.command("config")
4539
.alias("c")
46-
.action(function() {
40+
.action(async () => {
4741
// displays Gitg0 on start
4842
if (files.directoryExists(".git")) {
49-
console.log(
50-
figlet.textSync("Gitg0", {
51-
horizontalLayout: "default",
52-
verticalLayout: "default",
53-
}),
54-
"\n"
55-
);
56-
fs.stat("./.gitgo", function(err, stat) {
57-
if (err == null) {
58-
// asks task based questions
59-
getConfigQuestions();
60-
} else if (err.code === "ENOENT") {
43+
try {
44+
logLogo();
45+
await fs.stat("./.gitgo");
46+
getConfigQuestions();
47+
} catch (err) {
48+
if (err.code === "ENOENT") {
6149
// file does not exist
6250
var conf = {
6351
current_issue: {
@@ -91,16 +79,17 @@ program
9179
use_emojis: false,
9280
commit_config: false,
9381
};
94-
fs.writeFile("./.gitgo", JSON.stringify(conf, null, 2), (err) => {
95-
if (err) console.log("Error writing file:", err);
96-
});
97-
getConfigQuestions();
82+
try {
83+
await fs.writeFile("./.gitgo", JSON.stringify(conf, null, 2));
84+
getConfigQuestions();
85+
} catch (e) {
86+
console.log("Error writing file: ", e);
87+
}
9888
} else {
9989
console.log("Some other error: ", err.code);
10090
}
101-
});
91+
}
10292
} else {
103-
// checks if the directory is a git based repo or not
10493
console.log(
10594
cowsay.say({
10695
text: "Not a git repository!",
@@ -114,16 +103,10 @@ program
114103
program
115104
.command("display")
116105
.alias("d")
117-
.action(function() {
106+
.action(() => {
118107
// displays Gitg0 on start
119108
if (files.directoryExists(".git")) {
120-
console.log(
121-
figlet.textSync("Gitg0", {
122-
horizontalLayout: "default",
123-
verticalLayout: "default",
124-
}),
125-
"\n"
126-
);
109+
logLogo();
127110
// asks task based questions
128111
displaySuggestions();
129112
} else {
@@ -144,14 +127,8 @@ program
144127
.action(function() {
145128
// displays Gitg0 on start
146129
if (files.directoryExists(".git")) {
147-
console.log(
148-
figlet.textSync("Gitg0", {
149-
horizontalLayout: "default",
150-
verticalLayout: "default",
151-
}),
152-
"\n"
153-
);
154-
jsonReader("./.gitgo", (err, conf) => {
130+
logLogo();
131+
jsonReader("./.gitgo", (err: Error, conf: GitGoConf) => {
155132
if (err) {
156133
console.log("Error reading file:", err);
157134
return;
@@ -178,14 +155,8 @@ program
178155
.action(function() {
179156
// displays Gitg0 on start
180157
if (files.directoryExists(".git")) {
181-
console.log(
182-
figlet.textSync("Gitg0", {
183-
horizontalLayout: "default",
184-
verticalLayout: "default",
185-
}),
186-
"\n"
187-
);
188-
jsonReader("./.gitgo", (err, conf) => {
158+
logLogo();
159+
jsonReader("./.gitgo", (err: Error, conf: GitGoConf) => {
189160
if (err) {
190161
console.log("Error reading file:", err);
191162
return;
@@ -194,17 +165,19 @@ program
194165
if (conf.commit_config) {
195166
conf.commit_config = false;
196167
conf.current_commit_message = "";
197-
conf.current_branch = [""];
168+
conf.current_branch = "";
198169
conf.existing_branches = [""];
199170
conf.selected_commit_type = "";
200-
conf.current_issue.number = "";
201-
conf.current_issue.labels = [""];
202-
conf.current_issue.title = "";
203-
fs.writeFile("./.gitgo", JSON.stringify(conf, null, 2), (err) => {
171+
conf.current_issue = {
172+
number: undefined,
173+
labels: [""],
174+
title: "",
175+
};
176+
fs.writeFile("./.gitgo", JSON.stringify(conf, null, 2), (err: Error) => {
204177
if (err) console.log("Error writing file:", err);
205178
});
206179
setTimeout(function() {
207-
exec("git add ./.gitgo", (error, stdout, stderr) => {
180+
exec("git add ./.gitgo", (error: Error, stdout: any, stderr: Error) => {
208181
if (error) {
209182
console.log(`error: ${error.message}`);
210183
return;
@@ -218,7 +191,7 @@ program
218191
console.log("Files have be commited!\nRecent commit message: " + cMsg);
219192
}, 1000);
220193
} else {
221-
exec("git reset -- ./.gitgo", (error, stdout, stderr) => {
194+
exec("git reset -- ./.gitgo", (error: Error, stdout: any, stderr: Error) => {
222195
if (error) {
223196
console.log(`error: ${error.message}`);
224197
return;
@@ -249,13 +222,7 @@ program
249222
.alias("v")
250223
.action(function() {
251224
// displays Gitg0 on start
252-
console.log(
253-
figlet.textSync("Gitg0", {
254-
horizontalLayout: "default",
255-
verticalLayout: "default",
256-
}),
257-
"\n"
258-
);
225+
logLogo();
259226
console.log("v" + version.version + "-stable");
260227
});
261228

@@ -264,13 +231,7 @@ program
264231
.alias("w")
265232
.action(function() {
266233
// displays Gitg0 on start
267-
console.log(
268-
figlet.textSync("Gitg0", {
269-
horizontalLayout: "default",
270-
verticalLayout: "default",
271-
}),
272-
"\n"
273-
);
234+
logLogo();
274235
console.log(
275236
`You just need to know 7 simple commands you and then you're ${chalk.bold.cyan(
276237
"gtg"

src/lib/funcs/logLogo.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import figlet from 'figlet'
2+
3+
const logLogo = () => {
4+
console.log(
5+
figlet.textSync('Gitg0', {
6+
horizontalLayout: 'default',
7+
verticalLayout: 'default'
8+
}),
9+
"\n"
10+
)
11+
}
12+
13+
export default logLogo

src/types.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface GitGoConf {
2+
current_issue?: {
3+
number?: number;
4+
labels?: string[];
5+
title?: string;
6+
}
7+
commit_guidelines: string[];
8+
custom_guidelines: boolean;
9+
selected_commit_type: string;
10+
emojis: {
11+
initial_commit: string;
12+
feature: string;
13+
ui: string;
14+
code_quality: string;
15+
performance: string;
16+
security: string;
17+
config: string;
18+
accessibility: string;
19+
dev_tools: string;
20+
docs: string;
21+
release: string;
22+
bug_fix: string;
23+
crash: string;
24+
cleanup: string;
25+
wip: string;
26+
}
27+
existing_branches: string[];
28+
current_branch: string;
29+
current_commit_message: string;
30+
use_emojis: boolean;
31+
commit_config: boolean;
32+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
2525

2626
/* Strict Type-Checking Options */
27-
"strict": false /* Enable all strict type-checking options. */,
27+
"strict": true /* Enable all strict type-checking options. */,
2828
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
2929
// "strictNullChecks": true, /* Enable strict null checks. */
3030
// "strictFunctionTypes": true, /* Enable strict checking of function types. */

0 commit comments

Comments
 (0)