61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
// updater.js - Auto-updater for Autoscale Bot
|
||
// Checks the GitHub repository for a newer release and updates the bot.
|
||
// Requires node-fetch (added to dependencies).
|
||
|
||
const fetch = require('node-fetch');
|
||
const { execSync } = require('child_process');
|
||
const fs = require('fs');
|
||
|
||
// Configuration - replace with actual repo owner/name if different.
|
||
const REPO = 'your-github-username/autoscale';
|
||
const GITHUB_API = `https://api.github.com/repos/${REPO}/releases/latest`;
|
||
|
||
function getCurrentVersion() {
|
||
try {
|
||
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
|
||
return pkg.version;
|
||
} catch (e) {
|
||
console.error('Failed to read package.json:', e);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
async function getLatestRelease() {
|
||
try {
|
||
const resp = await fetch(GITHUB_API, { headers: { 'User-Agent': 'autoscale-updater' } });
|
||
if (!resp.ok) throw new Error(`GitHub API error ${resp.status}`);
|
||
const data = await resp.json();
|
||
return { version: data.tag_name.replace(/^v/, ''), tarball_url: data.tarball_url };
|
||
} catch (e) {
|
||
console.error('Failed to fetch latest release:', e);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
async function runUpdate() {
|
||
const current = getCurrentVersion();
|
||
const latest = await getLatestRelease();
|
||
if (!current || !latest) return;
|
||
|
||
if (current === latest.version) {
|
||
console.log(`✅ Already up‑to‑date (v${current}).`);
|
||
return;
|
||
}
|
||
|
||
console.log(`🔄 Updating from v${current} to v${latest.version}…`);
|
||
try {
|
||
// Pull latest code from git (assumes the repo is a git clone)
|
||
execSync('git fetch --all', { stdio: 'inherit' });
|
||
execSync(`git checkout v${latest.version}`, { stdio: 'inherit' });
|
||
// Install any new dependencies
|
||
execSync('npm install', { stdio: 'inherit' });
|
||
console.log('✅ Update complete. Restart the bot (e.g., pm2 restart autoscale).');
|
||
} catch (e) {
|
||
console.error('Update failed:', e);
|
||
}
|
||
}
|
||
|
||
if (require.main === module) {
|
||
runUpdate();
|
||
}
|