-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·148 lines (132 loc) · 3.9 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
#!/usr/bin/env node
const { execSync } = require("child_process");
const yargs = require("yargs");
const chalk = require("chalk");
const readline = require("readline");
// colored console.log's
const log = {
success: (text) => console.log(chalk.green(text)),
info: (text) => console.log(chalk.blue(text)),
warning: (text) => console.log(chalk.yellow(text)),
error: (text) => console.log(chalk.red("✖ ") + text),
title: (text) => console.log(chalk.magenta.bold("\n" + text)),
};
// CLI flags
const argv = yargs
.option("skip-add", {
type: "boolean",
description: "Skip the <git add .> step",
default: false,
})
.option("skip-push", {
type: "boolean",
description: "Skip the <git push origin main> step",
default: false,
})
.option("branch", {
type: "string",
description: "Change <branch> name",
default: "main",
})
.option("remote", {
type: "string",
description: "Change <remote>",
default: "origin",
})
.option("msg", {
type: "string",
description: "Manually enter Commit message",
})
.option("dbg", {
type: "boolean",
description: "Add Quick Commit message: Bugs fixed",
default: false,
})
.option("auto-push", {
type: "boolean",
description: "Automatically push after committing",
default: false,
})
.help().argv;
// Ask question
const askQuestion = (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question(query, (answer) => {
rl.close();
resolve(answer.trim().toLowerCase());
})
);
};
(async () => {
try {
// Git add
if (!argv.skipAdd) {
execSync("git add .", { stdio: "inherit" });
}
// Git diff
const changes = execSync("git diff --name-only HEAD", { encoding: "utf8" });
if (!changes) {
log.warning("No changes detected. Exiting.");
process.exit(0);
}
const minimizeChanges =
changes.length > 300 ? changes.slice(0, 300) : changes;
// Git commit
const commitMessage = argv.dbg
? "Bugs fixed"
: argv.msg
? argv.msg
: `Changes made to: ${minimizeChanges.trim().replace(/\n/g, ", ")}`;
execSync(`git commit -m "${commitMessage}"`, { stdio: "inherit" });
const remote = argv.remote ? argv.remote : "origin";
const branch = argv.branch ? argv.branch : "main";
// Push phase
let pushDecision = argv.autoPush;
if (!argv.autoPush && !argv.skipPush) {
console.log("\n" + chalk.green("Changes committed"));
const answer =
(await askQuestion(
chalk.bgGreen(`\n Push to ${remote}/${branch}? (y/n): `)
)) || "y";
if (answer === "y") {
pushDecision = true;
} else {
console.log("\n" + chalk.bold.green("🎉 Tasks completed successfully"));
log.title("Info");
console.log(chalk.cyan(` Commit: ${commitMessage}`));
return;
}
}
// Git push
if (pushDecision) {
try {
log.info(`Pushing changes to ${remote}/${branch}`);
execSync(`git push ${remote} ${branch}`, { stdio: "inherit" });
} catch (error) {
if (error.message.includes("has no upstream branch")) {
log.error("No upstream branch. Setting upstream...");
execSync(`git push --set-upstream ${remote} ${branch}`, {
stdio: "inherit",
});
} else {
log.error("Pushing error:", error.message);
process.exit(1);
}
}
} else {
log.info("<Push> phase skipped.");
}
console.log("\n" + chalk.bold.green("🎉 Tasks completed successfully"));
log.title("Info");
console.log(chalk.cyan(` Remote: ${remote}`));
console.log(chalk.cyan(` Branch: ${branch}`));
console.log(chalk.cyan(` Commit: ${commitMessage}`));
} catch (error) {
log.error("\n❌ Xatolik yuz berdi:", error.message);
process.exit(1);
}
})();