logggai
Version:
AI-powered CLI for transforming your development work into professional content
136 lines • 4.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleLimitExceeded = handleLimitExceeded;
exports.handlePotentialLimitError = handlePotentialLimitError;
const chalk = require("chalk");
const inquirer_1 = require("inquirer");
const errors_1 = require("./errors");
/**
* Affiche un message d'upgrade élégant quand les limites sont atteintes
*/
async function handleLimitExceeded(error) {
console.log();
console.log(chalk.default.red('Limit Exceeded'));
console.log('─'.repeat(50));
// Display limit details
console.log(chalk.default.yellow(`${(0, errors_1.formatLimitType)(error.limitType)}`));
if (error.currentUsage !== undefined && error.limit !== undefined) {
const percentage = Math.round((error.currentUsage / error.limit) * 100);
const progressBar = createProgressBar(error.currentUsage, error.limit);
console.log(chalk.default.gray(` Usage: ${error.currentUsage} / ${(0, errors_1.formatLimit)(error.limit)} (${percentage}%)`));
console.log(chalk.default.gray(` ${progressBar}`));
}
console.log();
console.log(chalk.default.red(error.message));
console.log();
// Current plan information
if (error.planId) {
const planName = getPlanDisplayName(error.planId);
console.log(chalk.default.blue(`Current plan: ${planName}`));
}
else {
console.log(chalk.default.blue('Current plan: Personal Free'));
}
console.log();
console.log(chalk.default.green.bold('Available options:'));
console.log();
console.log(chalk.default.cyan('1. Upgrade your plan'));
console.log(chalk.default.gray(' - Personal Pro, Team or Enterprise plans'));
console.log(chalk.default.gray(' - Higher or unlimited limits'));
console.log(chalk.default.gray(' - Access to all integrations'));
console.log();
console.log(chalk.default.cyan('2. Manage your subscription'));
console.log(chalk.default.gray(` - Dashboard: ${error.upgradeUrl}`));
console.log(chalk.default.gray(' - View detailed usage'));
console.log(chalk.default.gray(' - Compare plans'));
console.log();
// Ask if user wants to open browser
const openBrowser = await inquirer_1.default.prompt([
{
type: 'confirm',
name: 'open',
message: 'Open billing page in your browser?',
default: true
}
]);
if (openBrowser.open) {
await openUpgradeUrl(error.upgradeUrl);
}
else {
console.log();
console.log(chalk.default.yellow('Tip: Visit your dashboard to view detailed usage'));
console.log(chalk.default.gray(` ${error.upgradeUrl}`));
}
console.log();
}
/**
* Ouvre l'URL d'upgrade dans le navigateur
*/
async function openUpgradeUrl(url) {
try {
const { spawn } = await Promise.resolve().then(() => require('child_process'));
// Détecter l'OS et utiliser la bonne commande
const platform = process.platform;
let command;
let args;
if (platform === 'darwin') {
command = 'open';
args = [url];
}
else if (platform === 'win32') {
command = 'start';
args = ['', url];
}
else {
command = 'xdg-open';
args = [url];
}
spawn(command, args, { detached: true, stdio: 'ignore' }).unref();
console.log();
console.log(chalk.default.green('Opening browser...'));
console.log(chalk.default.gray(` ${url}`));
}
catch (error) {
console.log();
console.log(chalk.default.yellow('Could not automatically open browser'));
console.log(chalk.default.gray(`Copy this URL: ${url}`));
}
}
/**
* Crée une barre de progression visuelle
*/
function createProgressBar(current, total, width = 20) {
const percentage = Math.min(100, (current / total) * 100);
const filled = Math.round((percentage / 100) * width);
const empty = width - filled;
let color = chalk.default.green;
if (percentage >= 90)
color = chalk.default.red;
else if (percentage >= 75)
color = chalk.default.yellow;
const bar = color('█'.repeat(filled)) + chalk.default.gray('░'.repeat(empty));
return `[${bar}]`;
}
/**
* Convertit l'ID de plan en nom lisible
*/
function getPlanDisplayName(planId) {
const planNames = {
'PERSONAL_FREE': 'Personal Free',
'PERSONAL_PRO': 'Personal Pro',
'TEAM_STARTER': 'Team Starter',
'ENTERPRISE': 'Enterprise'
};
return planNames[planId] || planId;
}
/**
* Vérifie si une erreur est une LimitExceededError et la gère
*/
async function handlePotentialLimitError(error) {
if (error instanceof errors_1.LimitExceededError || error?.type === 'LIMIT_EXCEEDED') {
await handleLimitExceeded(error);
return true;
}
return false;
}
//# sourceMappingURL=upgrade-handler.js.map