@hivetechs/hive-ai
Version:
Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API
148 lines • 4.91 kB
JavaScript
/**
* Cache Management Utility
*
* Centralized cache clearing to prevent NPX caching issues and
* ensure users always get the latest version of Hive AI tools.
*/
import { execSync } from 'child_process';
import { existsSync, rmSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
/**
* Clear all system caches that could cause version conflicts
*/
export async function clearAllCaches(options = {}) {
const opts = {
npm: true,
npx: true,
hive: true,
verbose: false,
...options
};
const results = [];
try {
// Clear NPM cache
if (opts.npm) {
if (opts.verbose)
console.log('🧹 Clearing NPM cache...');
try {
execSync('npm cache clean --force', { stdio: 'pipe' });
results.push('✅ NPM cache cleared');
}
catch (error) {
results.push('⚠️ NPM cache clear failed (non-critical)');
}
}
// Clear NPX cache (most important for our use case)
if (opts.npx) {
if (opts.verbose)
console.log('🧹 Clearing NPX cache...');
// Method 1: Manually remove npx cache directory (most reliable)
try {
const npxCacheDir = join(homedir(), '.npm', '_npx');
if (existsSync(npxCacheDir)) {
rmSync(npxCacheDir, { recursive: true, force: true });
results.push('✅ NPX cache directory removed');
}
// Also try the newer NPX cache location
const npxCacheDir2 = join(homedir(), '.npm', '_cacache');
if (existsSync(npxCacheDir2)) {
rmSync(npxCacheDir2, { recursive: true, force: true });
results.push('✅ NPX cacache directory removed');
}
}
catch (dirError) {
// Method 2: Try clearing with npx clear command as fallback
try {
execSync('npx clear-npx-cache', { stdio: 'pipe' });
results.push('✅ NPX cache cleared via command');
}
catch (error) {
results.push('⚠️ NPX cache clear failed (non-critical)');
}
}
}
// Clear Hive-specific caches
if (opts.hive) {
if (opts.verbose)
console.log('🧹 Clearing Hive AI caches...');
await clearHiveCaches();
results.push('✅ Hive AI caches cleared');
}
if (opts.verbose) {
console.log('\n' + results.join('\n'));
}
}
catch (error) {
if (opts.verbose) {
console.error('Cache clearing encountered issues:', error);
}
// Don't throw - cache clearing should be non-blocking
}
}
/**
* Clear Hive-specific caches (analytics, knowledge base, etc.)
*/
async function clearHiveCaches() {
try {
// Clear analytics caches
const { globalAnalyticsEngine } = await import('../tools/analytics-engine.js');
globalAnalyticsEngine.clearCaches();
// Clear pending sync data
const { clearPendingSyncData } = await import('../storage/unified-database.js');
await clearPendingSyncData();
// Clear any other Hive-specific cache directories
const hiveCacheDir = join(homedir(), '.hive', 'cache');
if (existsSync(hiveCacheDir)) {
rmSync(hiveCacheDir, { recursive: true, force: true });
}
}
catch (error) {
// Silent fail for internal caches
}
}
/**
* Clear only NPX cache (most critical for version issues)
*/
export async function clearNPXCache() {
try {
await clearAllCaches({ npm: false, npx: true, hive: false, verbose: false });
return true;
}
catch (error) {
return false;
}
}
/**
* Force fresh download of Hive AI package
*/
export async function forceFreshHiveDownload() {
try {
// Clear NPX cache first
await clearNPXCache();
// Force download latest version (will bypass cache)
execSync('npx -y @hivetechs/hive-ai@latest --version', { stdio: 'pipe' });
}
catch (error) {
// Non-critical if this fails
}
}
/**
* Detect if we're running from cached NPX vs fresh install
*/
export function isRunningFromNPXCache() {
try {
const processPath = process.argv[1] || '';
return processPath.includes('_npx') || processPath.includes('.npm/registry');
}
catch (error) {
return false;
}
}
/**
* Get cache clearing command for users to run manually
*/
export function getCacheClearCommand() {
return 'npm cache clean --force && npx clear-npx-cache && rm -rf ~/.npm/_npx';
}
//# sourceMappingURL=cache-manager.js.map