@jashg91/gitpusher
Version:
š AI-powered Git auto commit & push CLI with smart commit messages
102 lines (89 loc) ⢠3.61 kB
JavaScript
import chalk from "chalk";
import inquirer from "inquirer";
import { verifyAndSaveToken, deleteApiKeyFromDB } from "./apikey.js";
import { getLimitInfo, forceSyncCache } from "./usageTracker.js";
export async function handleConfigCommand() {
console.log(chalk.cyan("\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā®"));
console.log(chalk.cyan("ā āļø GitPusher Config ā"));
console.log(chalk.cyan("ā°āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāÆ\n"));
const { action } = await inquirer.prompt([
{
type: "list",
name: "action",
message: "What would you like to do?",
choices: [
{ name: chalk.green("š Add Token"), value: "add" },
{ name: chalk.blue("š View Usage Info"), value: "info" },
{ name: chalk.cyan("š Sync Usage Cache"), value: "sync" },
{ name: chalk.red("šļø Delete Configuration"), value: "delete" },
{ name: chalk.gray("ā Exit"), value: "exit" },
],
},
]);
if (action === "add") {
await handleAddToken();
} else if (action === "info") {
await handleViewInfo();
} else if (action === "sync") {
await handleSyncCache();
} else if (action === "delete") {
await handleDeleteKey();
} else {
console.log(chalk.gray("\nš Exiting config..."));
process.exit(0);
}
}
async function handleAddToken() {
const { token } = await inquirer.prompt([
{
type: "input",
name: "token",
message: chalk.cyan("š Enter your token: "),
validate: (input) => (input ? true : "Token cannot be empty"),
},
]);
await verifyAndSaveToken(token);
}
async function handleViewInfo() {
const info = getLimitInfo();
if (!info) {
console.log(chalk.red("\nā No configuration found."));
console.log(chalk.yellow("š” Run 'gitpusher config' and add a token first.\n"));
return;
}
console.log(chalk.cyan("\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā®"));
console.log(chalk.cyan("ā š Usage Information ā"));
console.log(chalk.cyan("ā°āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāÆ\n"));
console.log(chalk.cyan("āāā Token: ") + chalk.white(info.token));
console.log(chalk.cyan("āāā API Key: ") + chalk.white(info.apikey));
console.log(chalk.cyan("āāā Remaining Limit: ") + chalk.white(info.limit));
if (info.limit <= 10) {
console.log(chalk.yellow("\nā ļø Warning: Low usage limit remaining!"));
}
console.log();
}
async function handleSyncCache() {
console.log(chalk.cyan("\nš Attempting to sync cached usage data...\n"));
await forceSyncCache();
console.log();
}
async function handleDeleteKey() {
const { confirm } = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: chalk.yellow("ā ļø Are you sure you want to delete your configuration?"),
default: false,
},
]);
if (confirm) {
const result = await deleteApiKeyFromDB();
if (result) {
console.log(chalk.green("\nā
Configuration deleted successfully!"));
} else {
console.log(chalk.red("\nā Failed to delete configuration."));
}
} else {
console.log(chalk.gray("\nš Cancelled."));
}
}