UNPKG

@jashg91/gitpusher

Version:

šŸš€ AI-powered Git auto commit & push CLI with smart commit messages

173 lines (148 loc) • 5.42 kB
import chalk from "chalk"; import inquirer from "inquirer"; import { getPublicIP } from "./ip.js"; import ora from "ora"; import fs from "fs"; import path from "path"; // Removed: saveApiKeyToDB() - no longer needed, using .gitpusher_config.json export async function getApiKeyFromDB() { try { const configPath = path.join(process.cwd(), ".gitpusher_config.json"); if (!fs.existsSync(configPath)) { return { apiKey: null }; } const configData = fs.readFileSync(configPath, "utf8"); const config = JSON.parse(configData); return { apiKey: config.apikey || null }; } catch (err) { console.log(chalk.red("āŒ Error reading API key from config:", err.message)); return { apiKey: null }; } } export async function deleteApiKeyFromDB() { try { const configPath = path.join(process.cwd(), ".gitpusher_config.json"); if (!fs.existsSync(configPath)) { console.log(chalk.yellow("āš ļø No config file found.")); return false; } fs.unlinkSync(configPath); console.log(chalk.yellow("šŸ—‘ļø Configuration deleted successfully.")); return true; } catch (err) { console.log(chalk.red("āŒ Error deleting config: " + err.message)); return false; } } // Removed: createNewApiKey() - no longer needed, using token-based auth // Removed: generateTokenForIP() - no longer needed, token provided by user export async function handleNoApiKey() { console.log(chalk.blue("\n╔════════════════════════════════════╗")); console.log(chalk.blue("ā•‘ šŸ”‘ API Key Required ā•‘")); console.log(chalk.blue("ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•\n")); const { choice } = await inquirer.prompt([ { type: "list", name: "choice", message: "No API key found. What would you like to do?", choices: [ { name: chalk.green("šŸ”‘ Add Token"), value: "token" }, { name: chalk.gray("āŒ Exit"), value: "exit" }, ], }, ]); if (choice === "token") { 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); } else { console.log(chalk.gray("\nšŸ‘‹ Exiting...")); process.exit(0); } } export async function handleInvalidApiKey() { console.log(chalk.red("\nāŒ Invalid API key.\n")); const { choice } = await inquirer.prompt([ { type: "list", name: "choice", message: "Choose an option:", choices: [ { name: chalk.green("šŸ”‘ Update Token"), value: "token" }, { name: chalk.gray("āŒ Exit"), value: "exit" }, ], }, ]); if (choice === "token") { 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); } else { process.exit(0); } } export async function verifyAndSaveToken(token) { const spinner = ora("Verifying token...").start(); try { const ip = await getPublicIP(); const res = await fetch("https://gitpusher-backend.vercel.app//api/keys/verify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ randomAPIkey:token , ip }), }); if (!res.ok) { spinner.fail(chalk.red("āŒ Token verification failed.")); console.log(chalk.yellow("āš ļø Please check your token and try again.")); return false; } const data = await res.json(); if (data.success && data.details) { const { apikey, limit } = data.details; // Save to local config file const configPath = path.join(process.cwd(), ".gitpusher_config.json"); // Check if config already exists and preserve it let existingConfig = {}; if (fs.existsSync(configPath)) { try { const existing = fs.readFileSync(configPath, "utf8"); existingConfig = JSON.parse(existing); } catch (err) { // Ignore parsing errors } } const config = { ...existingConfig, apikey, limit, token, ip, savedAt: new Date().toISOString(), }; fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); spinner.succeed(chalk.green("āœ… Token verified and saved successfully!")); console.log(chalk.cyan("\nšŸ“Š Configuration Details:")); console.log(chalk.cyan("ā”œā”€ā”€ API Key: ") + chalk.white(apikey)); console.log(chalk.cyan("└── Limit: ") + chalk.white(limit)); return true; } else { spinner.fail(chalk.red("āŒ Invalid token response.")); return false; } } catch (err) { spinner.fail(chalk.red("āŒ Error verifying token: " + err.message)); console.log(chalk.yellow("šŸ’” Please check your backend server and try again.")); return false; } }