@jashg91/gitpusher
Version:
š AI-powered Git auto commit & push CLI with smart commit messages
173 lines (148 loc) ⢠5.42 kB
JavaScript
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;
}
}