-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (154 loc) · 4.41 KB
/
index.js
File metadata and controls
165 lines (154 loc) · 4.41 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
153
154
155
156
157
158
159
160
161
162
163
164
165
const axios = require("axios");
const mysql = require("mysql2");
const moment = require("moment");
const chalk = require("chalk");
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "terminal",
});
class DB {
constructor(con) {
this.con = con;
}
all() {
this.con.connect((err) => {
if (err) throw err;
this.con.query("SELECT * FROM git", (err, result, fields) => {
if (err) throw err;
console.table(result);
process.exit(0);
});
});
}
create(branch, jira_link, type, active) {
const errors = [];
if (!branch) errors.push("Branch is required");
if (!jira_link) errors.push("Jira Link is required");
if (!type) errors.push("Type of ticket is required");
if (errors.length) {
console.log(chalk.red(errors.join(", \n")));
return process.exit(0);
}
this.con.connect((err) => {
if (err) throw err;
this.con.query(
`INSERT INTO git (branch, jira_link, type, active) values('${branch}', '${jira_link}', '${type}', ${
active ? active : 0
})`,
(err, result, fields) => {
if (err) throw err;
console.log(chalk.bold.green("Branch Info added successfully!"));
process.exit(0);
}
);
});
}
toggle(branch) {
if (!branch) {
console.log(chalk.bold.red("Please enter a branch name"));
return process.exit(0);
}
this.con.connect((err) => {
if (err) throw err;
this.findByBranch(branch)
.then((result) => {
const currentState = result[0].active === 1 ? 0 : 1;
this.con.query(
`UPDATE git SET active = ${currentState} WHERE branch = '${branch}'`,
(err, result, fields) => {
console.log(
`${branch} is now ${
currentState === 1
? chalk.bold.green("Active")
: chalk.bold.red("Deactivated")
}`
);
process.exit(0);
}
);
})
.catch((e) => {
console.log(e);
process.exit(0);
});
});
}
time(branch) {
if (!branch) {
console.log(chalk.bold.red("Please enter a branch name"));
return process.exit(0);
}
this.con.connect((err) => {
if (err) throw err;
this.findByBranch(branch, "created_at", "updated_at")
.then((result) => {
const start = moment(result[0].created_at);
const end = moment(result[0].updated_at);
var duration = moment.duration(end.diff(start));
var hours = duration.asHours();
console.log(chalk.bold.red(hours.toFixed(2)));
process.exit(0);
})
.catch((e) => {
console.log(e);
process.exit(0);
});
});
}
find(branch) {
if (!branch) {
console.log(chalk.bold.red("Please enter a branch name"));
return process.exit(0);
}
this.con.connect((err) => {
if (err) throw err;
this.findByBranch(branch, "jira_link", "type")
.then((result) => {
const jira_link = result[0].jira_link;
const type = result[0].type;
console.log(`Jira Link: ${jira_link} \nType: ${type}`);
process.exit(0);
})
.catch((e) => {
console.log(e);
});
});
}
hash(branch, githash) {
this.con.connect((err) => {
if (err) throw err;
this.findByBranch(branch.split("\n").join(""), "branch")
.then((result) => {
const existingBranch = result[0].branch;
this.con.query(
`INSERT INTO hash (branch, hash) values('${existingBranch}', '${githash}')`,
(err, result, fields) => {
if (err) throw err;
console.log(`Hash: ${githash} saved`);
process.exit(0);
}
);
})
.catch((e) => {
console.log(e);
});
});
}
// Private method and more comments
findByBranch(branch, ...columns) {
return new Promise((resolve, reject) => {
const targetColumns = columns.length ? columns.join(", ") : "*";
this.con.query(
`SELECT ${targetColumns} FROM git WHERE branch = '${branch}'`,
(err, result, fields) => {
if (err) reject(err);
resolve(result);
}
);
});
}
}
const db = new DB(con);
exports.db = db;