UNPKG

kira-crud

Version:

Intelligent CRUD Generator for Laravel and Angular

173 lines (150 loc) 5.48 kB
/** * Update Checker * Vérifies if a new version of Kira is available */ const https = require('https'); const chalk = require('chalk'); const semver = require('semver'); const { exec } = require('child_process'); const util = require('util'); const execPromise = util.promisify(exec); const pkg = require('../package.json'); const inquirer = require('inquirer'); /** * Check for updates by querying the npm registry * @returns {Promise<Object>} Update information */ async function checkForUpdates() { return new Promise((resolve, reject) => { const options = { hostname: 'registry.npmjs.org', path: `/kira-crud`, method: 'GET', headers: { 'Accept': 'application/json' } }; const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { if (res.statusCode === 200) { const npmData = JSON.parse(data); const latestVersion = npmData['dist-tags'].latest; const currentVersion = pkg.version; const updateAvailable = semver.gt(latestVersion, currentVersion); resolve({ currentVersion, latestVersion, updateAvailable }); } else { // Non-blocking error, just indicate no update is available resolve({ currentVersion: pkg.version, latestVersion: null, updateAvailable: false, error: `Error checking for updates: Status ${res.statusCode}` }); } } catch (error) { // Non-blocking error, just indicate no update is available resolve({ currentVersion: pkg.version, latestVersion: null, updateAvailable: false, error: `Error parsing update information: ${error.message}` }); } }); }); req.on('error', (error) => { // Non-blocking error, just indicate no update is available resolve({ currentVersion: pkg.version, latestVersion: null, updateAvailable: false, error: `Error checking for updates: ${error.message}` }); }); req.end(); }); } /** * Display update notification if an update is available * @returns {Promise<void>} */ async function displayUpdateNotification() { try { const updateInfo = await checkForUpdates(); if (updateInfo.error) { // Silently fail for better user experience console.log(chalk.dim(`\nVérification des mises à jour ignorée: ${updateInfo.error}`)); return; } if (updateInfo.updateAvailable) { console.log('\n'); console.log(chalk.blue('╔════════════════════════════════════════════════╗')); console.log(chalk.blue('║ MISE À JOUR DISPONIBLE ║')); console.log(chalk.blue('╚════════════════════════════════════════════════╝')); console.log(`\nUne nouvelle version de Kira est disponible!`); console.log(`Version actuelle: ${chalk.yellow(updateInfo.currentVersion)}`); console.log(`Nouvelle version: ${chalk.green(updateInfo.latestVersion)}`); console.log(`\nPour mettre à jour, exécutez:`); console.log(chalk.cyan(`npm install -g kira-crud@latest`)); console.log(); } } catch (error) { // Silently fail for better user experience } } /** * Update Kira to the latest version * @returns {Promise<boolean>} Success status */ async function updateKira() { try { const updateInfo = await checkForUpdates(); if (updateInfo.error) { console.log(chalk.red(`\nErreur lors de la vérification des mises à jour: ${updateInfo.error}`)); return false; } if (!updateInfo.updateAvailable) { console.log(chalk.green(`\nKira est déjà à jour (version ${updateInfo.currentVersion})!`)); return true; } console.log(`\nMise à jour de Kira de la version ${updateInfo.currentVersion} vers ${updateInfo.latestVersion}...`); // Ask for confirmation const { confirm } = await inquirer.prompt([ { type: 'confirm', name: 'confirm', message: 'Voulez-vous mettre à jour Kira maintenant?', default: true } ]); if (!confirm) { console.log(chalk.yellow('\nMise à jour annulée.')); return false; } // Run npm update console.log(chalk.yellow('\nExécution de la mise à jour...')); const { stdout, stderr } = await execPromise('npm install -g kira-crud@latest'); console.log(chalk.green(`\nKira a été mis à jour avec succès vers la version ${updateInfo.latestVersion}!`)); console.log('\nRedémarrez Kira pour utiliser la nouvelle version.'); return true; } catch (error) { console.error(chalk.red(`\nErreur lors de la mise à jour: ${error.message}`)); console.log('\nVous pouvez mettre à jour manuellement avec:'); console.log(chalk.cyan('npm install -g kira-crud@latest')); return false; } } module.exports = { checkForUpdates, displayUpdateNotification, updateKira };