UNPKG

@stoar/cli

Version:

CLI tool for STOAR - decentralized file storage on Arweave

122 lines 4.19 kB
import { execSync } from 'node:child_process'; import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { homedir } from 'node:os'; import chalk from 'chalk'; import { compareVersions } from './version.js'; const UPDATE_CHECK_FILE = join(homedir(), '.stoar', 'update-check.json'); const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours export async function checkForUpdates(currentVersion, options = {}) { // Skip if explicitly disabled if (options.skipCheck || process.env.STOAR_SKIP_UPDATE_CHECK === 'true') { return; } // Skip in CI environments if (process.env.CI || process.env.CONTINUOUS_INTEGRATION) { return; } try { // Ensure config directory exists const configDir = join(homedir(), '.stoar'); if (!existsSync(configDir)) { mkdirSync(configDir, { recursive: true }); } // Read last check data let checkData = { lastCheck: 0, lastVersion: currentVersion, dismissed: [] }; if (existsSync(UPDATE_CHECK_FILE)) { try { const data = readFileSync(UPDATE_CHECK_FILE, 'utf-8'); checkData = JSON.parse(data); } catch { } } // Check if we should run the check const now = Date.now(); const shouldCheck = now - checkData.lastCheck > CHECK_INTERVAL; if (!shouldCheck) { return; } // Perform check in background setImmediate(async () => { try { const latestVersion = await getLatestVersion(); if (!latestVersion) return; // Update check data checkData.lastCheck = now; checkData.lastVersion = latestVersion; writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(checkData, null, 2)); // Compare versions if (compareVersions(currentVersion, latestVersion) < 0) { // Check if this version was dismissed if (checkData.dismissed?.includes(latestVersion)) { return; } // Show update notification showUpdateNotification(currentVersion, latestVersion); } } catch { // Silently fail - don't interrupt user } }); } catch { // Silently fail - don't interrupt user } } async function getLatestVersion() { try { const npmInfo = execSync('npm view @stoar/cli version', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], timeout: 5000 // 5 second timeout }).trim(); return npmInfo; } catch { return null; } } function showUpdateNotification(currentVersion, latestVersion) { console.log(); console.log(chalk.bgYellow.black(' UPDATE AVAILABLE ')); console.log(chalk.yellow(`A new version of @stoar/cli is available: ${currentVersion}${latestVersion}`)); console.log(chalk.gray('Run `stoar update` to update')); console.log(); } export function dismissUpdate(version) { try { const configDir = join(homedir(), '.stoar'); if (!existsSync(configDir)) { mkdirSync(configDir, { recursive: true }); } let checkData = { lastCheck: Date.now(), lastVersion: version, dismissed: [] }; if (existsSync(UPDATE_CHECK_FILE)) { try { const data = readFileSync(UPDATE_CHECK_FILE, 'utf-8'); checkData = JSON.parse(data); } catch { } } if (!checkData.dismissed) { checkData.dismissed = []; } if (!checkData.dismissed.includes(version)) { checkData.dismissed.push(version); } writeFileSync(UPDATE_CHECK_FILE, JSON.stringify(checkData, null, 2)); } catch { // Silently fail } } //# sourceMappingURL=update-check.js.map