mirror of
https://github.com/SuperClaude-Org/SuperClaude_Framework.git
synced 2025-12-29 16:16:08 +00:00
✨ Add automatic update checking for PyPI and NPM packages
- Check for updates on startup (once per 24h) - Show update banner when new version available - Support --no-update-check and --auto-update flags - Add SUPERCLAUDE_AUTO_UPDATE environment variable - Implement for both Python (PyPI) and Node.js (NPM)
This commit is contained in:
276
bin/checkUpdate.js
Normal file
276
bin/checkUpdate.js
Normal file
@@ -0,0 +1,276 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Auto-update checker for SuperClaude NPM package
|
||||
* Checks npm registry for newer versions and offers automatic updates
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { spawnSync } = require('child_process');
|
||||
const https = require('https');
|
||||
|
||||
const CACHE_FILE = path.join(process.env.HOME || process.env.USERPROFILE, '.claude', '.npm_update_check');
|
||||
const CHECK_INTERVAL = 86400000; // 24 hours in milliseconds
|
||||
const TIMEOUT = 2000; // 2 seconds
|
||||
const PACKAGE_NAME = '@bifrost_inc/superclaude';
|
||||
|
||||
/**
|
||||
* Get the current package version from package.json
|
||||
*/
|
||||
function getCurrentVersion() {
|
||||
try {
|
||||
const packagePath = path.join(__dirname, '..', 'package.json');
|
||||
const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
|
||||
return packageData.version;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we should perform an update check based on last check time
|
||||
*/
|
||||
function shouldCheckUpdate(force = false) {
|
||||
if (force) return true;
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(CACHE_FILE)) return true;
|
||||
|
||||
const data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'));
|
||||
const lastCheck = data.lastCheck || 0;
|
||||
|
||||
// Check if 24 hours have passed
|
||||
return Date.now() - lastCheck > CHECK_INTERVAL;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current timestamp as last check time
|
||||
*/
|
||||
function saveCheckTimestamp() {
|
||||
const cacheDir = path.dirname(CACHE_FILE);
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
if (!fs.existsSync(cacheDir)) {
|
||||
fs.mkdirSync(cacheDir, { recursive: true });
|
||||
}
|
||||
|
||||
let data = {};
|
||||
try {
|
||||
if (fs.existsSync(CACHE_FILE)) {
|
||||
data = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'));
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
data.lastCheck = Date.now();
|
||||
fs.writeFileSync(CACHE_FILE, JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Query npm registry for the latest version
|
||||
*/
|
||||
function getLatestVersion() {
|
||||
return new Promise((resolve) => {
|
||||
const options = {
|
||||
hostname: 'registry.npmjs.org',
|
||||
path: `/${PACKAGE_NAME}/latest`,
|
||||
method: 'GET',
|
||||
timeout: TIMEOUT,
|
||||
headers: {
|
||||
'User-Agent': 'SuperClaude-Updater'
|
||||
}
|
||||
};
|
||||
|
||||
const req = https.request(options, (res) => {
|
||||
let data = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const packageData = JSON.parse(data);
|
||||
resolve(packageData.version);
|
||||
} catch {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', () => resolve(null));
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
resolve(null);
|
||||
});
|
||||
|
||||
req.setTimeout(TIMEOUT);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare version strings
|
||||
*/
|
||||
function isNewerVersion(current, latest) {
|
||||
if (!current || !latest) return false;
|
||||
|
||||
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 (latestPart > currentPart) return true;
|
||||
if (latestPart < currentPart) return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if npm or yarn is being used globally
|
||||
*/
|
||||
function detectPackageManager() {
|
||||
// Check if installed globally with npm
|
||||
const npmResult = spawnSync('npm', ['list', '-g', PACKAGE_NAME], {
|
||||
encoding: 'utf8',
|
||||
shell: true
|
||||
});
|
||||
|
||||
if (npmResult.status === 0 && npmResult.stdout.includes(PACKAGE_NAME)) {
|
||||
return 'npm';
|
||||
}
|
||||
|
||||
// Check if installed globally with yarn
|
||||
const yarnResult = spawnSync('yarn', ['global', 'list'], {
|
||||
encoding: 'utf8',
|
||||
shell: true
|
||||
});
|
||||
|
||||
if (yarnResult.status === 0 && yarnResult.stdout.includes(PACKAGE_NAME)) {
|
||||
return 'yarn';
|
||||
}
|
||||
|
||||
return 'npm'; // Default to npm
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate update command
|
||||
*/
|
||||
function getUpdateCommand() {
|
||||
const pm = detectPackageManager();
|
||||
|
||||
if (pm === 'yarn') {
|
||||
return `yarn global upgrade ${PACKAGE_NAME}`;
|
||||
}
|
||||
|
||||
return `npm update -g ${PACKAGE_NAME}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show update banner
|
||||
*/
|
||||
function showUpdateBanner(currentVersion, latestVersion, autoUpdate = false) {
|
||||
const updateCmd = getUpdateCommand();
|
||||
|
||||
console.log('\n\x1b[36m╔════════════════════════════════════════════════╗\x1b[0m');
|
||||
console.log(`\x1b[36m║\x1b[33m 🚀 Update Available: ${currentVersion} → ${latestVersion} \x1b[36m║\x1b[0m`);
|
||||
console.log(`\x1b[36m║\x1b[32m Run: ${updateCmd.padEnd(30)} \x1b[36m║\x1b[0m`);
|
||||
console.log('\x1b[36m╚════════════════════════════════════════════════╝\x1b[0m\n');
|
||||
|
||||
return autoUpdate || process.env.SUPERCLAUDE_AUTO_UPDATE === 'true';
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the update
|
||||
*/
|
||||
function performUpdate() {
|
||||
const updateCmd = getUpdateCommand();
|
||||
console.log('\x1b[36m🔄 Updating SuperClaude...\x1b[0m');
|
||||
|
||||
const cmdParts = updateCmd.split(' ');
|
||||
const result = spawnSync(cmdParts[0], cmdParts.slice(1), {
|
||||
stdio: 'inherit',
|
||||
shell: true
|
||||
});
|
||||
|
||||
if (result.status === 0) {
|
||||
console.log('\x1b[32m✅ Update completed successfully!\x1b[0m');
|
||||
console.log('\x1b[33mPlease restart SuperClaude to use the new version.\x1b[0m');
|
||||
return true;
|
||||
} else {
|
||||
console.log('\x1b[33m⚠️ Update failed. Please run manually:\x1b[0m');
|
||||
console.log(` ${updateCmd}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function to check and notify for updates
|
||||
*/
|
||||
async function checkAndNotify(options = {}) {
|
||||
const { force = false, autoUpdate = false, silent = false } = options;
|
||||
|
||||
// Check environment variables
|
||||
if (process.env.SUPERCLAUDE_NO_UPDATE_CHECK === 'true') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if enough time has passed
|
||||
if (!shouldCheckUpdate(force)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get current version
|
||||
const currentVersion = getCurrentVersion();
|
||||
if (!currentVersion) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get latest version
|
||||
const latestVersion = await getLatestVersion();
|
||||
if (!latestVersion) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Save timestamp
|
||||
saveCheckTimestamp();
|
||||
|
||||
// Compare versions
|
||||
if (!isNewerVersion(currentVersion, latestVersion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Show banner unless silent
|
||||
if (!silent) {
|
||||
const shouldUpdate = showUpdateBanner(currentVersion, latestVersion, autoUpdate);
|
||||
|
||||
if (shouldUpdate) {
|
||||
return performUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Export functions for use in other modules
|
||||
module.exports = {
|
||||
checkAndNotify,
|
||||
getCurrentVersion,
|
||||
getLatestVersion,
|
||||
isNewerVersion
|
||||
};
|
||||
|
||||
// If run directly, perform check
|
||||
if (require.main === module) {
|
||||
checkAndNotify({
|
||||
force: process.argv.includes('--force'),
|
||||
autoUpdate: process.argv.includes('--auto-update')
|
||||
});
|
||||
}
|
||||
22
bin/cli.js
22
bin/cli.js
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
const { spawnSync } = require("child_process");
|
||||
const { detectPython, detectPip } = require("./checkEnv");
|
||||
const { checkAndNotify } = require("./checkUpdate");
|
||||
|
||||
let pythonCmd = detectPython();
|
||||
if (!pythonCmd) {
|
||||
@@ -10,12 +11,33 @@ if (!pythonCmd) {
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
// Parse command line arguments for update control
|
||||
const noUpdateCheck = args.includes('--no-update-check');
|
||||
const autoUpdate = args.includes('--auto-update');
|
||||
const isQuiet = args.includes('--quiet') || args.includes('-q');
|
||||
|
||||
// Special case: update command
|
||||
if (args[0] === "update") {
|
||||
require("./update");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Check for updates unless disabled
|
||||
if (!noUpdateCheck && !isQuiet) {
|
||||
// Run update check asynchronously to avoid blocking
|
||||
checkAndNotify({
|
||||
autoUpdate: autoUpdate,
|
||||
silent: false
|
||||
}).then(updated => {
|
||||
if (updated) {
|
||||
console.log("\n🔄 SuperClaude was updated. Please restart to use the new version.");
|
||||
process.exit(0);
|
||||
}
|
||||
}).catch(() => {
|
||||
// Silently ignore update check errors
|
||||
});
|
||||
}
|
||||
|
||||
// Forward everything to Python SuperClaude
|
||||
const result = spawnSync(pythonCmd, ["-m", "SuperClaude", ...args], { stdio: "inherit", shell: true });
|
||||
process.exit(result.status);
|
||||
|
||||
Reference in New Issue
Block a user