@tastekim/chat-cli
Version:
π¬Connect with developers worldwide through an interactive terminal chat experience while you code!π»
140 lines β’ 7.55 kB
JavaScript
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