UNPKG

zlocalz

Version:

ZLocalz - TUI Locale Guardian for Flutter ARB l10n/i18n validation and translation with AI-powered fixes

257 lines 11.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZLocalzUpdater = void 0; const update_notifier_1 = __importDefault(require("update-notifier")); const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const chalk_1 = __importDefault(require("chalk")); class ZLocalzUpdater { packageJson; notifier; autoUpdateEnabled = true; constructor(autoUpdateEnabled = true) { this.autoUpdateEnabled = autoUpdateEnabled; this.initializeUpdater(); } async initializeUpdater() { try { try { this.packageJson = require('../../package.json'); } catch (error) { const packagePath = path.join(__dirname, '../../package.json'); const packageContent = await fs.readFile(packagePath, 'utf-8'); this.packageJson = JSON.parse(packageContent); } if (!this.packageJson?.name || !this.packageJson?.version) { this.packageJson = { name: 'zlocalz', version: '1.0.2' }; } this.notifier = (0, update_notifier_1.default)({ pkg: this.packageJson, updateCheckInterval: 1000 * 60 * 60 * 24, shouldNotifyInNpmScript: false }); this.setupAutoUpdate(); } catch (error) { console.debug('Update notifier setup failed:', error); } } setupAutoUpdate() { if (this.notifier?.update) { if (this.autoUpdateEnabled) { this.performSilentUpdate(); } else { this.displayUpdateNotification(); } } } async performSilentUpdate() { try { const { latest, type } = this.notifier.update; if (type === 'major') { this.displayUpdateNotification(); return; } console.log(chalk_1.default.blue(`🔄 Auto-updating ZLocalz to ${latest}...`)); const success = await this.performAutoUpdate(true); if (success) { console.log(chalk_1.default.green('✅ ZLocalz updated successfully in the background!')); } } catch (error) { this.displayUpdateNotification(); } } displayUpdateNotification() { const { current, latest, type } = this.notifier.update; console.log(chalk_1.default.yellow('┌─────────────────────────────────────────────────────┐')); console.log(chalk_1.default.yellow('│') + ' 📦 ZLocalz Update Available! ' + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ` Current: ${chalk_1.default.red(current)} ` + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ` Latest: ${chalk_1.default.green(latest)} ` + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ` Type: ${this.getUpdateTypeEmoji(type)} ${type} ` + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ' ' + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ' Run the following to update: ' + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ` ${chalk_1.default.cyan('npm install -g zlocalz@latest')} ` + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ' ' + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ' Or use our auto-update command: ' + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('│') + ` ${chalk_1.default.cyan('zlocalz --update')} ` + chalk_1.default.yellow('│')); console.log(chalk_1.default.yellow('└─────────────────────────────────────────────────────┘')); console.log(''); } getUpdateTypeEmoji(type) { switch (type) { case 'major': return '🚀'; case 'minor': return '✨'; case 'patch': return '🐛'; default: return '📦'; } } async checkForUpdates() { await this.initializeUpdater(); } async performAutoUpdate(silent = false) { try { if (!silent) { console.log(chalk_1.default.blue('🔄 Checking for ZLocalz updates...')); } this.notifier = (0, update_notifier_1.default)({ pkg: this.packageJson, updateCheckInterval: 0 }); if (!this.notifier.update) { if (!silent) { console.log(chalk_1.default.green('✅ ZLocalz is already up to date!')); } return true; } const { latest } = this.notifier.update; if (!silent) { console.log(chalk_1.default.yellow(`📦 Update available: ${latest}`)); console.log(chalk_1.default.blue('🔄 Updating ZLocalz...')); } const { spawn } = require('child_process'); return new Promise((resolve, reject) => { const updateProcess = spawn('npm', ['install', '-g', `zlocalz@${latest}`], { stdio: silent ? 'pipe' : 'inherit' }); let output = ''; if (silent) { updateProcess.stdout?.on('data', (data) => { output += data.toString(); }); updateProcess.stderr?.on('data', (data) => { output += data.toString(); }); } updateProcess.on('close', (code) => { if (code === 0) { if (!silent) { console.log(chalk_1.default.green('✅ ZLocalz updated successfully!')); console.log(chalk_1.default.yellow('🔄 Please restart your terminal or run `zlocalz --version` to verify.')); } this.saveUpdateInfo(latest); resolve(true); } else { if (!silent) { console.log(chalk_1.default.red('❌ Update failed. Please run manually: npm install -g zlocalz@latest')); } reject(new Error(`Update process exited with code ${code}`)); } }); updateProcess.on('error', (error) => { if (!silent) { console.log(chalk_1.default.red('❌ Update failed:', error.message)); console.log(chalk_1.default.yellow('Please run manually: npm install -g zlocalz@latest')); } reject(error); }); }); } catch (error) { if (!silent) { console.log(chalk_1.default.red('❌ Auto-update failed:', error)); console.log(chalk_1.default.yellow('Please run manually: npm install -g zlocalz@latest')); } return false; } } getChangelogUrl() { return `https://github.com/bllfoad/zlocalz/releases/tag/v${this.packageJson?.version}`; } async saveUpdateInfo(version) { try { const os = require('os'); const updateInfoPath = path.join(os.homedir(), '.zlocalz-update'); const updateInfo = { lastUpdate: new Date().toISOString(), version, autoUpdated: true }; await fs.writeFile(updateInfoPath, JSON.stringify(updateInfo, null, 2)); } catch (error) { } } async getUpdateInfo() { try { const os = require('os'); const updateInfoPath = path.join(os.homedir(), '.zlocalz-update'); const content = await fs.readFile(updateInfoPath, 'utf-8'); return JSON.parse(content); } catch (error) { return null; } } async showReleaseNotes() { if (!this.notifier?.update) { console.log(chalk_1.default.green('No updates available.')); return; } console.log(chalk_1.default.blue('📋 Recent Changes:')); console.log(chalk_1.default.gray('─'.repeat(50))); console.log(chalk_1.default.yellow(`🔗 Full changelog: ${this.getChangelogUrl()}`)); console.log(''); const updateInfo = await this.getUpdateInfo(); if (updateInfo?.autoUpdated) { console.log(chalk_1.default.green(`✅ Last auto-update: ${updateInfo.lastUpdate} (v${updateInfo.version})`)); } } async showWelcomeMessage() { const updateInfo = await this.getUpdateInfo(); if (updateInfo?.autoUpdated && updateInfo.version) { console.log(chalk_1.default.green(`✨ ZLocalz auto-updated to v${updateInfo.version}!`)); console.log(chalk_1.default.blue('🔗 What\'s new: ') + this.getChangelogUrl()); console.log(''); updateInfo.autoUpdated = false; const os = require('os'); const updateInfoPath = path.join(os.homedir(), '.zlocalz-update'); await fs.writeFile(updateInfoPath, JSON.stringify(updateInfo, null, 2)).catch(() => { }); } } } exports.ZLocalzUpdater = ZLocalzUpdater; //# sourceMappingURL=updater.js.map