UNPKG

@tastekim/chat-cli

Version:

πŸ’¬Connect with developers worldwide through an interactive terminal chat experience while you code!πŸ’»

140 lines β€’ 7.55 kB
import https from 'https'; import chalk from 'chalk'; export class VersionChecker { constructor(packageName, currentVersion) { this.packageName = packageName; this.currentVersion = currentVersion; } async checkLatestVersion() { try { const latest = await this.fetchLatestVersion(); const needsUpdate = this.compareVersions(this.currentVersion, latest) < 0; return { current: this.currentVersion, latest, needsUpdate }; } catch (error) { // λ„€νŠΈμ›Œν¬ 였λ₯˜ λ“±μœΌλ‘œ 체크 μ‹€νŒ¨ μ‹œ μ—…λ°μ΄νŠΈκ°€ ν•„μš”ν•˜μ§€ μ•Šλ‹€κ³  κ°€μ • return { current: this.currentVersion, latest: this.currentVersion, needsUpdate: false }; } } async fetchLatestVersion() { return new Promise((resolve, reject) => { const url = `https://registry.npmjs.org/${this.packageName}/latest`; const request = https.get(url, { timeout: 5000, // 5초 νƒ€μž„μ•„μ›ƒ headers: { 'User-Agent': 'chat-cli-version-checker' } }, (response) => { let data = ''; response.on('data', (chunk) => { data += chunk; }); response.on('end', () => { try { const packageInfo = JSON.parse(data); resolve(packageInfo.version); } catch (error) { reject(new Error('Failed to parse npm registry response')); } }); }); request.on('error', (error) => { reject(error); }); request.on('timeout', () => { request.destroy(); reject(new Error('Request timeout')); }); }); } compareVersions(current, latest) { const currentParts = current.split('.').map(Number); const latestParts = latest.split('.').map(Number); for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) { const currentPart = currentParts[i] || 0; const latestPart = latestParts[i] || 0; if (currentPart < latestPart) return -1; if (currentPart > latestPart) return 1; } return 0; } static displayForceUpdateMessage(versionInfo, packageName) { if (!versionInfo.needsUpdate) return; const boxWidth = 55; const currentVersionText = ` Current version: ${versionInfo.current}`; const latestVersionText = ` Latest version: ${versionInfo.latest}`; const updateCommandText = ` npm install -g ${packageName}@latest`; // 각 μ€„μ˜ 길이λ₯Ό κ³„μ‚°ν•΄μ„œ νŒ¨λ”© μΆ”κ°€ const currentPadding = ' '.repeat(boxWidth - currentVersionText.length); const latestPadding = ' '.repeat(boxWidth - latestVersionText.length); const commandPadding = ' '.repeat(boxWidth - updateCommandText.length); console.log(); console.log(chalk.red('β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”')); console.log(chalk.red('β”‚') + chalk.bold.white(' UPDATE REQUIRED! ') + chalk.red('β”‚')); console.log(chalk.red('β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€')); console.log(chalk.red('β”‚') + chalk.white(' Your version is outdated and incompatible. ') + chalk.red('β”‚')); console.log(chalk.red('β”‚') + chalk.white(' Please update to the latest version to continue. ') + chalk.red('β”‚')); console.log(chalk.red('β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€')); console.log(chalk.red('β”‚') + ` Current version: ${chalk.red.bold(versionInfo.current)}${currentPadding}` + chalk.red('β”‚')); console.log(chalk.red('β”‚') + ` Latest version: ${chalk.green.bold(versionInfo.latest)}${latestPadding}` + chalk.red('β”‚')); console.log(chalk.red('β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€')); console.log(chalk.red('β”‚') + chalk.white(' Run this command to update: ') + chalk.red('β”‚')); console.log(chalk.red('β”‚') + chalk.cyan.bold(updateCommandText) + commandPadding + chalk.red('β”‚')); console.log(chalk.red('β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€')); console.log(chalk.red('β”‚') + chalk.yellow.bold(' Application will exit in 5 seconds... ') + chalk.red('β”‚')); console.log(chalk.red('β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜')); console.log(); } static async checkAndForceUpdate(packageName, currentVersion) { try { const checker = new VersionChecker(packageName, currentVersion); const versionInfo = await checker.checkLatestVersion(); if (versionInfo.needsUpdate) { VersionChecker.displayForceUpdateMessage(versionInfo, packageName); // 5초 μΉ΄μš΄νŠΈλ‹€μš΄ for (let i = 5; i > 0; i--) { process.stdout.write(`\r${chalk.red('⚠️ Exiting in')} ${chalk.bold.yellow(i)} ${chalk.red('seconds...')}`); await new Promise(resolve => setTimeout(resolve, 1000)); } console.log(`\r${chalk.red('❌ Application terminated. Please update and try again.')}`); process.exit(1); } } catch (error) { // λ„€νŠΈμ›Œν¬ 였λ₯˜ λ“±μœΌλ‘œ 버전 체크 μ‹€νŒ¨ μ‹œ 경고만 ν‘œμ‹œν•˜κ³  계속 μ§„ν–‰ if (process.env.DEBUG === 'true') { console.warn(chalk.yellow('⚠️ Could not check for updates. Continuing...')); console.error('Version check failed:', error); } } } // κΈ°μ‘΄ ν•¨μˆ˜λŠ” ν˜Έν™˜μ„±μ„ μœ„ν•΄ μœ μ§€ (μ˜΅μ…˜ λͺ…λ Ήμ–΄μ—μ„œ μ‚¬μš©) static async checkAndNotify(packageName, currentVersion) { try { const checker = new VersionChecker(packageName, currentVersion); const versionInfo = await checker.checkLatestVersion(); if (versionInfo.needsUpdate) { VersionChecker.displayForceUpdateMessage(versionInfo, packageName); } } catch (error) { // 버전 체크 μ‹€νŒ¨λŠ” 쑰용히 λ¬΄μ‹œ (μ‚¬μš©μž κ²½ν—˜ λ°©ν•΄ν•˜μ§€ μ•ŠμŒ) if (process.env.DEBUG === 'true') { console.error('Version check failed:', error); } } } } //# sourceMappingURL=version-checker.js.map