Add update-check command

This commit is contained in:
Aaron Hood 2025-08-10 19:52:06 -04:00 committed by manjaroblack
parent 3efcfd54d4
commit f57a72f448
1 changed files with 54 additions and 0 deletions

View File

@ -6,13 +6,17 @@ const fs = require('fs').promises;
const yaml = require('js-yaml'); const yaml = require('js-yaml');
const chalk = require('chalk'); const chalk = require('chalk');
const inquirer = require('inquirer'); const inquirer = require('inquirer');
const semver = require('semver');
const https = require('https');
// Handle both execution contexts (from root via npx or from installer directory) // Handle both execution contexts (from root via npx or from installer directory)
let version; let version;
let installer; let installer;
let packageName;
try { try {
// Try installer context first (when run from tools/installer/) // Try installer context first (when run from tools/installer/)
version = require('../package.json').version; version = require('../package.json').version;
packageName = require('../package.json').name;
installer = require('../lib/installer'); installer = require('../lib/installer');
} catch (e) { } catch (e) {
// Fall back to root context (when run via npx from GitHub) // Fall back to root context (when run via npx from GitHub)
@ -86,6 +90,56 @@ program
} }
}); });
// Command to check if updates are available
program
.command('update-check')
.description('Check for BMad Update')
.action(async () => {
console.log('Checking for updates...');
// Make HTTP request to npm registry for latest version info
const req = https.get(`https://registry.npmjs.org/${packageName}/latest`, res => {
// Check for HTTP errors (non-200 status codes)
if (res.statusCode !== 200) {
console.error(chalk.red(`Update check failed: Received status code ${res.statusCode}`));
return;
}
// Accumulate response data chunks
let data = '';
res.on('data', chunk => data += chunk);
// Process complete response
res.on('end', () => {
try {
// Parse npm registry response and extract version
const latest = JSON.parse(data).version;
// Compare versions using semver
if (semver.gt(latest, version)) {
console.log(`${packageName} update available: ${version}${latest}`);
} else {
console.log(`${packageName} is up to date.`);
}
} catch (error) {
// Handle JSON parsing errors
console.error(chalk.red('Failed to parse npm registry data:'), error.message);
}
});
});
// Handle network/connection errors
req.on('error', error => {
console.error(chalk.red('Update check failed:'), error.message);
});
// Set 30 second timeout to prevent hanging
req.setTimeout(30000, () => {
req.destroy();
console.error(chalk.red('Update check timed out'));
});
});
program program
.command('list:expansions') .command('list:expansions')
.description('List available expansion packs') .description('List available expansion packs')