valzyysdk
Version:
CLI dan modul untuk mengakses data dari API JKT48Connect, termasuk data member, teater, event, pembayaran, dan lainnya.
97 lines (85 loc) • 3.96 kB
JavaScript
// scripts/preinstall-check.js
const https = require('https');
const semver = require('semver');
const fs = require('fs');
const path = require('path');
class PreInstallChecker {
constructor() {
this.packageName = '@jkt48connect-corp/sdk';
this.packageJsonPath = path.join(__dirname, '..', 'package.json');
}
getCurrentVersion() {
try {
const packageJson = JSON.parse(fs.readFileSync(this.packageJsonPath, 'utf8'));
return packageJson.version;
} catch (error) {
return null;
}
}
async getLatestVersion() {
return new Promise((resolve, reject) => {
const url = `https://registry.npmjs.org/${this.packageName}/latest`;
https.get(url, (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'));
}
});
}).on('error', (error) => {
reject(error);
});
});
}
displayInstallationBlockedMessage(currentVersion, latestVersion) {
console.error(`
╔══════════════════════════════════════════════════════════════╗
║ 🚫 INSTALLATION BLOCKED 🚫 ║
╠══════════════════════════════════════════════════════════════╣
║ Package: ${this.packageName.padEnd(48)} ║
║ Requested Version: ${currentVersion.padEnd(36)} ║
║ Latest Version: ${latestVersion.padEnd(36)} ║
╠══════════════════════════════════════════════════════════════╣
║ You are trying to install an outdated version of this ║
║ package. This version has been deprecated and is no ║
║ longer supported. ║
║ ║
║ Please install the latest version instead: ║
║ ║
║ npm install ${this.packageName}@latest${' '.repeat(Math.max(0, 30 - this.packageName.length))} ║
║ ║
║ For the latest features, consider using: ║
║ npm install @jkt48/core ║
║ ║
║ Documentation: https://docs.jkt48connect.my.id ║
╚══════════════════════════════════════════════════════════════╝
`);
}
async checkInstallation() {
try {
const currentVersion = this.getCurrentVersion();
if (!currentVersion) {
console.warn('Warning: Could not determine package version');
return;
}
const latestVersion = await this.getLatestVersion();
if (semver.lt(currentVersion, latestVersion)) {
this.displayInstallationBlockedMessage(currentVersion, latestVersion);
process.exit(1);
}
console.log('✅ Installing latest version...');
} catch (error) {
// If we can't check (network issues, first publish, etc.), allow installation
console.warn('Warning: Could not verify version, proceeding with installation...');
}
}
}
// Run the check
const checker = new PreInstallChecker();
checker.checkInstallation();